Lets Learn About Git Branching!
So, what is branching anyways? Branching is the way we split history while using git, as well as most other RCS’s.
It can be visualized as something like the image to the left.
Now, why should you use branching? Branching developed as a solution to developers disrupting one another during mid-development. Imagine you are working on a project with other developers, you may want to use branches to develop without fear that the other developers code will break yours before it is finished.
Lets say you finish your feature. Now what? You have to get your code mixed in with what the other developers have done! To do that git provides a few different commands. The two we will take a look at are merge and rebase.
How do you do it? You can create a new branch with git branch <branch> . Alternately you can do it with git checkout -b <branch> if you want to checkout the branch after its creation.
Merging
Merging is the first command you can use to recombine your histories.
Say we are on the master branch and we want to merge in the changes on the apple branch. We could accomplish that like so git merge apple .
Usually merging is like the image to the right; a new commit with two parents, the commit where the head branch, master, was, and the commit where the branch being merged in, apple, is. It is important to note that the head branch moves up to the merged commit.
Get Charles Bayley’s stories in your inbox
Join Medium for free to get updates from this writer.
There is another kind of merge called a fast-forward merge. The difference is the merge could be completed without a two parent commit, so the head branch simply moves to the same commit as the merged in branch.
Rebase
Rebase is another way of combining branches.
If we are on the branch apple, and want to place our changes onto master we could do it with git rebase master .
That command will creates copies of a branch apple’s commits all the way back to where it diverged and then places those commits on top of the branch master. The branch apple will be moved to the copied commits as well.
Comparison
You may be asking, which one should I use? Good, I’m here to answer!
Merge is good when you want to preserve the history exactly as it was created. This is a reasonably position if you don’t mind the extra complexity that comes with it. The extra complexity is usually mild if you are working on a smaller team. This is my default way of combining histories since I like to preserve the history.
Rebase can be good if you want to simplify the history of a branch. You achieve this simpler history by sort of just pretending the branching never happened. This become much more useful if you are in larger teams and the complexity of a correct history becomes to hard to follow, therefore defeating one of the main points of using a RCS in the first point!
I’ve supplied a few more images to demonstrate my point about the complexity/truth trade-off below.
Conclusion
I hope you you learned something here!
The tool used to create the images is http://git-school.github.io/visualizing-git/ .




