Skip to content
Shukaro edited this page Sep 4, 2015 · 4 revisions

Git's branching and merging are by far its greatest features, so here's a quick run-down of how to use them.

All git repos have a master branch, and generally speaking master should be maintained in proper buildable and working order by a smart use of branches to develop features and large fixes. When the branch has served its purpose and its changes have been merged into master, it can be safely deleted.

When should I create a new branch?
Generally speaking, a branch should be created and developed on whenever you are adding a new (major) feature to master or troubleshooting a (major) bug. When you want to begin work on one of these things, you should perform a git fetch in order to bring your local repo up to speed, then git checkout master if you're not on it already, then finally git branch myDescriptiveBranchName.

After you have created a branch you should also perform a git push origin myDescriptiveBranchName in order to create it on the remote repo as well. At this point, all commits working on that feature or bugfix should be performed on that branch ONLY.

When should I merge my branch back into master?
A development branch should be merged back into master if and only if either its feature is in a functional state or the bug that it addresses is fixed. The simplest way to merge a branch back into master is with the following commands (see Git for important warnings):

  • git checkout master
  • git pull origin master
  • git merge myDescriptiveBranchName
  • git push origin master

If the branch has no further use you should delete it using git branch -d myDescriptiveBranchName and git push origin master in order to prevent cluttering up the repo with dead branches.

If you have previously merged a branch, and now want to do more development on it, then you should run git fetch, git checkout myDescriptiveBranchName, and git rebase origin/master in order to bring it up to speed with the current state of master.