![endif]-->
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 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.