

Understanding the Fundamentals of Branching in Git
ModelingGitposted by ODSC Community August 27, 2021 ODSC Community

Using branches is critical to working with git, not to mention to the success of your project. The idea is this: you have a ”main” branch that you’ll do your releases from. Each time code is added to the main branch it is checked and reviewed so that the main branch stays as clean as possible.
This article is an excerpt from the book, Git for Programmers by Jesse Liberty – A comprehensive guide that equips you with actionable insights on advanced Git concepts in an engaging and straightforward way.
When you want to work on a bug or a feature, you create a new branch (often called a feature branch). This creates a copy of the code that is currently in the main branch. You can work on your feature branch without affecting the main branch at all. Once you are done, and all is working, you can then ”merge” your feature branch into the main branch:
Figure 1: First feature branch
Notice that there is a pointer named Head. This points to whatever is in your work area. In this case, we’ve branched to Feature 1 and Head shows that the code for that feature branch is now in our work area.
That is a pretty good simplification of branching but there is a good deal more to it. First, let’s see how to do it. Until now, all your code has been on the Main branch – a bad practice. We should have created a feature branch before doing any coding. That said, we can do so now.
From the command line, you simply check out Main (putting whatever is at the tip of Main into your work area. The tip means the most recent commit). Once you are in Main you will pull from the remote repository to get the very latest version of Main. You are now ready to create your first branch. The sequence of commands looks like this:
Figure 2: Creating branch on the command line
Note that creating the branch Calculator did not check it out; you must do that as a separate step. However, if you use the -b flag, then you can create the branch and check it out at the same time:
git checkout -b Calculator
In either case, the new branch is in the work area. But what is in that branch? Because that branch was created from Main, and we’ve not changed anything yet, the new branch is identical to the Main branch. From here they will diverge. As you add code, it will be in the new branch (Calculator) but not in the main.
Before we look at that in-depth, let’s create branches for the VisualStudio user and for the GitHub Desktop user.
The easiest way to do this (and the least confusing) is to open Windows Explorer and navigate to the VisualStudio folder (in my case, GitHub | VisualStudio | ProGitForProgrammers | ProGitForProgrammers). In that folder is a .sln file, which I will double-click on, opening Visual Studio.
Note: Don’t be confused between the VisualStudio folder (which we are using to demonstrate Git in Visual Studio) and the program itself, which we use to modify the code in all three places (CommandLine, GitHubDesktop, and VisualStudio).
It might be less confusing if you think of these as three separate programmers, each one on its own computer (mimicked here by using separate directories). Each programmer has a main branch and each one is now branching off for their own work.
We want to put this on a branch as well, and to reduce confusion we’ll call this branch Book. To create the branch, click on the Git menu and select New Branch Give the new branch the name Book, and press Create:
Figure 3: Creating a new branch in Visual Studio
When you do, a window will open on the left that lists the branches for this repository and Book will be in bold indicating that it is the current branch:
Figure 4: Branches menu in Visual Studio
Two of the users have now branched off of Main. Let’s use GitHub Desktop to create a third branch. Open the program and click on the menu choice Repository. On that menu click on Show In Explorer and make sure you are in the path C:\GitHub\GitHubDesktop\ProGitForProgrammers.
It should indicate that you have one pull from the origin (the server) and have a button that says Pull Origin. Go ahead and click that button. That brings down the latest version of Main. You now should see that the button says Push Origin – that’s to push up to the server the two commits that are now sitting in this directory.
To create your new branch, click on the Branch menu choice and select New Branch. It will prompt you for the name of your new branch. Enter Movie and click Create. The interface now asks if you want to publish your branch. Publishing in GitHub Desktop simply means uploading it for the first time. Let’s hold off on that and first make some commits.
Programmer 1 (CommandLine) and Calculator
Open Visual Studio in the CommandLine directory path. In the Solution Explorer, you should see Program.cs, which has five WriteLine statements. Add a new class named Calculator and set it to public:
namespace ProGitForProgrammers { public class Calculator { } }
Normally we would not create a commit after so little work, but for this book, to make simple examples, we’ll be doing a great deal of committing. Return to the command line and get the status. It will tell you that you have one untracked file. Git has recognized that there is a file in the directory that it knows nothing about. Our next step is to add it to git:
git add .
By using the dot, the add command will add any modified or new files to the staging area. You can then commit the new file just by writing:
git commit -m "Add calculator class"
If you write git status now, git will tell you that you are on the branch Calculator, that you have nothing to commit, and that your working tree is clean.
We’re going to talk about the log command in detail in the book but for now, let’s just use it to see our commit and the message that went with it:
git log –oneline
This will display all of your commits, one per line:
❯ git log --oneline e5c4db9 (HEAD -> Calculator) Add calculator class b00ca09 (origin/main, origin/HEAD, main, featureOne) Demonstrating the staging area 4ac9d40 Add a line to program to indicate why it was added ef16f81 Add writeline indicating we are in command line d418600 Add informative WriteLine a3f085e First commit -- from command line a5798e1 Initial commit
The seven-digit hexadecimal identifier is the ”short SHA” and is enough to uniquely identify each commit. They are listed in newest to oldest order. Our most recent commit says:
e5c4db9 (HEAD -> Calculator) Add calculator class
This tells you that your Head pointer is pointing to your Calculator branch (that is, what is in your working area right now is the Calculator branch) and it displays the message we added for that commit. Graphically, it might look like this:
Figure 5: Head pointer
Notice that the arrow runs from Calculator to Main. Each commit points to its parent.
Pushing the New Branch
We can push this commit up to the server, but the server doesn’t know about this branch. When we enter git push, we get back this message:
❯ git push fatal: The current branch Calculator has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin Calculator
It is saying that it could not proceed (fatal) because the current branch (which is Calculator) does not correspond to a branch on the server. Wonderfully, however, it gives us the command line to use. Just copy the command and paste it at the prompt and hit Enter. Hey, presto! You’ve pushed your branch up to the server:
Figure 6: Pushing to the server
For now, you can ignore all the other messages; what you care about is the last two lines, indicating that you now have a branch, Calculator, on the server, and that on the server it is also called Calculator.
Note that from now on, when pushing commits on the Calculator branch, you won’t have to use that line; you’ll just be able to write git push.
Examining Origin
Let’s go to GitHub and examine our new branch. Sign in and select the ProGitForProgrammers repository:
Figure 7: Repository on server
So, where is our calculator folder? Notice the button in the upper left that says Main. Drop that down and choose Calculator – which will display the contents of the Calculator branch:
Figure 8: Calculator Branch on server
You can see that the Calculator branch does have the expected file.
Adding Commits to a Branch
Let’s add another commit to our branch. Return to Visual Studio and give our Calculator class an add method:
public int Add (int left, int right) { return left + right; }
Again, just to have lots of commits, let’s commit this. The easiest way is to combine the add and the commit and adding a message on a single line:
git commit -a -m "Add the add method"
To see that this was in fact committed, run the log command again:
Figure 9: Tracking HEAD
If you study this for a moment, not only will you see that our commit worked (it is the first one listed) but also that we have various pointers. On the first line, we see that HEAD is pointing to our Calculator branch. Good enough.
The second line indicates that the Calculator branch on origin (GitHub) is pointing to the previous commit. We have one commit to push.
The third line shows us that Main on origin, HEAD on origin, the Main branch, and the featureOne branch are all pointing to the third most recent commit. All this is fine. We expect Calculator to have diverged from Main, and we can push our commit if we like, or we can wait until we have a few of them.
Summary
In this article, we have learned about creating branches, committing code, and how it eventually helps to move commits from the work area to the repository and from the repository to the remote repository. Learn more from the book Git for Programmers.
About the Author
Jesse Liberty is a senior programmer specializing in Xamarin.Forms and related tools, including Git. He has been working with Git for fourteen years and is considered an expert by the developer community.
Liberty hosts the popular, Yet Another Podcast and he is the author of more than a dozen best-selling programming books. and one not yet published novel. Liberty is a Microsoft MVP and has worked as a Program Manager for Microsoft, Distinguished Software Engineer at AT&T; Software Architect for PBS and Vice President of Information Technology at Citibank, and he was on the teaching staff at Brandeis University.