Git/Branching & merging
Branching is supported in most VCSs. Branching can be an expensive and time consuming operation like in centralized systems that require making a copy of the whole source tree. Branching can also be given a high priority like DVCSs do to make it faster. In git, creating branches is easy, and very fast.
View your branches
git branch with nothing else to see what branches your repository has:
$ git branch * master
The branch called "master" is the default main line of development. You can rename it if you want, but it is customary to use the default. When you commit some changes, those changes are added to the branch you have checked out - in this case, master.
Create new branches
Let's create a new branch we can use for development - call it "dev":
$ git branch dev $ git branch dev * master
This only creates the new branch, it leaves your current HEAD where you remain. You can see from the * that the master branch is still what you have checked out. You can now use
git checkout dev to switch to the new branch.
Alternatively, you can create a new branch and check it out all at once with
$ git checkout -b newbranch
Delete a branch
To delete the current branch, again use git-branch, but this time send the
$ git branch -d <name>
If the branch hasn't been merged into master, then this will fail:
$ git branch -d foo error: The branch 'foo' is not a strict subset of your current HEAD. If you are sure you want to delete it, run 'git branch -D foo'.
Git's complaint saves you from possibly losing your work in the branch. If you are still sure you want to delete the branch, use
git branch -D <name> instead.
Pushing a branch to a remote repository
When you create a local branch, it won't automatically be kept in sync with the server. Unlike branches obtained by pulling from the server, simply calling git push isn't enough to get your branch pushed to the server. Instead, you have to explicitly tell git to push the branch, and which server to push it to:
$ git push origin <branch_name>
Deleting a branch from the remote repository
To delete a branch that has been pushed to a remote server, use the following command:
$ git push origin :<branch_name>
This syntax isn't intuitive, but what's going on here is you're issuing a command of the form:
$ git push origin <local_branch>:<remote_branch>
and giving an empty branch in the <local_branch> position, meaning to overwrite the branch with nothing.
Branching is the central concept of DVCS, but without good merging support, branches would be of little use.
git merge myBranch
This command merges the given branch into the current branch. If the current branch is a direct ancestor of the given branch, a fast-forward merge occurs, and the current branch head is redirected to point at the new branch. In other cases, a merge commit is recorded that has both the previous commit and the given branch tip as parents. If there are any conflicts during the merge, it will be necessary to resolve them by hand before the merge commit is recorded.