Git Branching

By Xah Lee. Date: . Last updated: .

How to list branches?

git branch
Show all local branches.
git branch -r
Show all remote branches.
git branch -a
Show all local and remote branches.
git show-branch
Show branches and their commits.

How to find out which is the current branch?

Type git branch. The current branch is indicated by a asterisk *.

git branch 2016 12 16
creating git branch

How to create a branch?

git branch name master
Create a branch named name from “master”. The master can also be any {commit ID, branch name, tag name}.
Note: this does not switch you to the newly created branch.

How to switch to a branch?

Note: before you switch to a branch, best to commit your changes first.

git checkout branch_name
Update current dir's files to be the branch named branch_name's code.

How to rename branch?

git branch -m old_name new_name
Rename branch.
git branch -M old_name new_name
Rename branch, overwrite existing branch named new_name.

How to merge branch?

WARNING: you should commit before you merge, because, otherwise, when merge has conflicts it's hard to revert to your uncommited pre-merge state.

First, switch to the branch you wan to merge to. For example, git checkout branch_name.

git merge name
Merge a branch named name into the current branch.
Note: merge does not delete any branch.

How to delete a branch?

git branch -d branch_name
Delete the branch. (the branch must be merged first)
git branch -D branch_name
Force delete the branch.

2014-01-18 thanks to Nick Alcock for help.