This is something I've had to do quite a few times, but it wasn't until I worked on a bunch of Github projects at HBO that I really started to get understanding branching in git. In my own Github projects I would often work right out of the master branch. This is fine for developers who are just getting started with git, but when working on large, enterprise projects you really want to be smart about using branches. This post will help you understand making git branches off of existing branches.
A Google drawing I made that visualizes branching off of another branch.
Let's begin with this repository here, Park-Boys-Bootcamp:
Of course, the first thing to do is to clone the repository:
Then you can checkout an existing branch with the checkout command:
If the branch already exists then you can checkout that branch directly right when you run the clone command with the --branch or shorthand -b flag:
Note that you can't create a new branch right from the clone command. If you try to do this it will complain, "fatal: Remote branch ____ not found in upstream origin":
If you want to create a new branch, you can first run git clone without the branch flag and then run the checkout command. With the checkout command we can add the --branch or shorthand -b flag to create a new branch:
The first argument here is the name of the new branch. The second argument is the name of the branch that defines the files and folders in the new branch.
This above command works, but there are some subtle less-than-optimal characteristics about checking out a branch this way. When you do git push or git pull it doesn't know exactly which branch is the "upstream branch" (ie. the one that you are pulling from / pushing to).
Also, when we print the branches with the branch command and the "double verbose flag" ( -vv ) it gives us a less than optimal description. Take a look at the screenshot below. The branch my-new-leaf-branch was checked out without tracking, and so it does not show a tracking branch in blue. However, consider the branch flipflop. We can see clearly (from the corresponding blue text) that flipflop is tracking boobledoop and thus when we are on the flipflop branch and simply type git pull or git push then git will automatically know to try to do the operation with the boodledoop branch. Also, just having the connections between branches listed out like this makes adding the tracking well worth it.
I use the -vv command often if I want to see what's tracking what.
Here's a nice article / tutorial on tracking branches. Basically, tracking tells git that this branch is linked to the branch that it's branching off of. But what do I mean by linked? Well, When you run push and pull in the new branch, it will by default try to push to and pull from the branch that you are tracking. This makes everything a lot nicer. You can checkout with tracking by adding the flag --track or it's shorthand form -t.
So, after all the background knowledge of my why is how it is, here's how I recommend creating a new branch called super-cool-new-branch off of the branch named base-branch:
But make sure you do "-tb" and not "-bt" since the second way will give an error:
The posts on this site are written and maintained by Jim Lynch. About Jim...