Using branches with the git
command line tool¶
Learning objectives¶
By the end of this tutorial you should be familiar with the git branching workflow using the command line git program, and you will get more practice with basic git commands for adding, committing, and pushing changes to a git repository. This tutorial will likely take 15-30 minutes to complete.
Branching¶
So what is the deal with branching? Branches are used to make changes to a repo on a separate version that can exist alongside the main branch. This way you can test out new features while leaving the main branch in working order. Imagine you are developing a complex new feature of a computer program, this might involve dozens or hundreds of commits, one for each time you make a substantial change to the code. You can slowly develop this feature on a separate branch that will accumulate all of these commits. Meanwhile, maybe you also make some completely separate changes on the main branch. Later, when the feature branch is finished and has been tested you can merge it into the main branch, at which point all of the commits on the feature branch will be committed to main. The end product is exactly the same as if you had never created the separate branch. Branching is only a convenience that developers use for collaboration, or to work on multiple versions of the code at the same time.
So, as I've said, you may not actually need to use branching for quite
some time. But let's walk through a quick example anyways just so you
are familiar with it. Let's create a new branch called "feature".
Try the following code, in order, and call git status
at several
points throughout to check in on what is happening with the files
or branch names being worked on.
# create a new branch called 'feature' and
git branch feature
# switch to the new branch
git checkout branch
# make a change to an existing file.
echo "new text appended here." >> ./file-1.md
# stage the changes to this file
git add file-1.md
# commit the changes (this branch will be 1 commit ahead)
git commit -m "added appended message."
# we can now switch back to the main branch
git checkout main
# and merge the 'feature' branch into 'main'
git merge feature
# delete the feature branch (no longer needed)
git branch -d feature
# push the new changes on main to the remote (origin)
git push origin main
Review and assessment¶
You've now seen two ways in which you can make changes to your local repo and push those changes to the remote on GitHub. The first involves committing changes directly to your main branch, and overall just ignoring the branching process. This involves only the three steps add, commit, and push. The second alternative approach involves these same commands, but performs them on a separately created branch that must be merged back into the main branch.
In addition to these there are in fact many other
ways in which you can use git
to call these three steps.
However, until you become very comfortable
with this simple workflow I do not recommend trying to use
any other type of shortcut. Make sure you've committed
to memory the workflow: add, commit, push.
The GitHub Learning Lab¶
There are many great resources for learning git and its integration with services like GitHub. One that I recommend is https://github.com/apps/github-learning-lab. Feel free to explore some of the free interactive courses that they offer for further training.
Alternatives to the git command-line tool¶
Because learning the git command line tool is difficult, several alternatives have also been developed that incorporate a graphical user interface, so that you do not need to use a shell or text editor at all. My belief is that you should first learn the command line framework to really understand how these commands work before you move to a more abstract form, otherwise you are likely to encounter problems, such as conflicts, and have a much harder time figuring out how to resolve them.
Assessment¶
Try to complete the following action by figuring out the appropriate code to do so. This may be challenging, but all of the answers you need to complete the task are described above. Remember, git works like a time machine, so it is unlikely that you will mess anything up that cannot be undone. If you get a strange or unexpected message, try to google it to find how to resolve it. And if you are still stuck, come visit the course chatroom.
Alert
Action: Complete the following steps.
- Create a new file called
file-4.md
in your hack-2-shell/ directory and write one of the following two messages in it about how you feel about learning git: 'crazy confusing' or 'easy peazy' - call git add on
file-4.md
to add it to your staging area for commit. - call git commit to commit your changes with an informative message (-m ...).
- call git push to push your changes to origin main.
- Look on GitHub to make sure your changes have appeared.
For your grade we will look for this file with the appropriate contents in your hack-2-shell repo on GitHub.