Skip to content
dasarpmar edited this page Nov 26, 2014 · 1 revision

Branches is a really neat way to organize your workflow while working on git. We shall again do this by example.

Our contributor has a fork of this repository, and a local copy on his computer. Now suppose he wishes to work on adding the proof of the CoolTheorem. He knows that this is going to take a while to finish and he is expecting upstream to change in the process. The best thing to do at this point is work on a separate branch. This way, his workflow would be uninterrupted by all changes upstream etc.

$ git branch CoolTheorem
$ git checkout CoolTheorem

The first command creates a branch called CoolTheorem, and the second command moves you to that branch. You could combine both the commands into one by git checkout -b CoolTheorem.

Now the contributor can go ahead and keep making his changes, and committing them to the branch CoolTheorem. Now suppose he is tired of working on CoolTheorem but instead wants to work on something else, he can create yet another branch and start working on that! If you want to go back to the master branch and make some change, you can do

$ git checkout master

Now all new changes would be committed to the master branch. Note that this does not change your CoolTheorem branch in any way. You can always go back to it, and it would be just as you had left it. You are free to use as many branches as you want, and in fact branching is an excellent way to keep track of a certain activity. Normally, I keep every major section to a separate branch.

Whenever you are finished with your changes on CoolTheorem, you can now choose to add all those changes to the master branch. The way to do this is using git merge. Suppose you want to merge the CoolTheorem branch with the master branch, you first go to the master branch

$ git checkout master

and then merge the desired branch

$ git merge CoolTheorem

If git is able to merge without any conflicts, it would do so automatically. If there are conflicts, it would ask you to address those conflicts and then commit again. That's it!

Branching and merging helps you compartmentalize your workflow.

Clone this wiki locally