git branch [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
git branch --unset-upstream [<branchname>]
git branch (-m | -M) [<oldbranch>] <newbranch>
git branch (-d | -D) [-r] <branchname>…
git branch --edit-description [<branchname>]
git branch [--color[=<when>] | --no-color] [-r | -a] [--list] [-v [--abbrev=<length> | --no-abbrev]] [--column[=<options>] | --no-column] [(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>] [--points-at <object>] [<pattern>…]
|-d, --delete||Delete a branch. The branch must be fully merged in its upstream branch, or in |
|-D||Shortcut for |
|-m, --move||Move/rename a branch and the corresponding reflog|
|-M||Shortcut for |
|-r, --remotes||List or delete (if used with -d) the remote-tracking branches|
|-a, --all||List both remote-tracking branches and local branches|
|--list||Activate the list mode. |
|--set-upstream||If specified branch does not exist yet or if |
Every git repository has one or more branches. A branch is a named reference to the
HEAD of a sequence of commits.
A git repo has a current branch (indicated by a
* in the list of branch names printed by the
git branch command), Whenever you create a new commit with the
git commit command, your new commit becomes the
HEAD of the current branch, and the previous HEAD becomes the parent of the new commit.
A new branch will have the same
HEAD as the branch from which it was created until something is committed to the new branch.
Git provides multiple commands for listing branches. All commands use the function of
git branch, which will provide a list of a certain branches, depending on which options are put on the command line. Git will if possible, indicate the currently selected branch with a star next to it.
|List local branches|
|List local branches verbose|
|List remote and local branches|
|List remote and local branches (verbose)|
|List remote branches|
|List remote branches with latest commit|
|List merged branches|
|List unmerged branches|
|List branches containing commit|
$ git branch -avvor
$ git branch -vvwill print the name of the upstream branch as well.
To create a new branch, while staying on the current branch, use:
git branch <name>
Generally, the branch name must not contain spaces and is subject to other specifications listed here. To switch to an existing branch :
git checkout <name>
To create a new branch and switch to it:
git checkout -b <name>
To create a branch at a point other than the last commit of the current branch (also known as HEAD), use either of these commands:
git branch <name> [<start-point>] git checkout -b <name> [<start-point>]
<start-point> can be any revision known to git (e.g. another branch name, commit SHA, or a symbolic reference such as HEAD or a tag name):
git checkout -b <name> some_other_branch git checkout -b <name> af295 git checkout -b <name> HEAD~5 git checkout -b <name> v1.0.5
To create a branch from a remote branch (the default
<remote_name> is origin):
git branch <name> <remote_name>/<branch_name> git checkout -b <name> <remote_name>/<branch_name>
If a given branch name is only found on one remote, you can simply use
git checkout -b <branch_name>
which is equivalent to
git checkout -b <branch_name> <remote_name>/<branch_name>
Sometimes you may need to move several of your recent commits to a new branch. This can be achieved by branching and "rolling back", like so:
git branch <new_name> git reset --hard HEAD~2 # Go back 2 commits, you will lose uncommitted work. git checkout <new_name>
Here is an illustrative explanation of this technique:
Initial state After git branch <new_name> After git reset --hard HEAD~2 newBranch newBranch ↓ ↓ A-B-C-D-E (HEAD) A-B-C-D-E (HEAD) A-B-C-D-E (HEAD) ↑ ↑ ↑ master master master
$ git branch -d dev
Deletes the branch named
dev if its changes are merged with another branch and will not be lost. If the
dev branch does contain changes that have not yet been merged that would be lost,
git branch -d will fail:
$ git branch -d dev error: The branch 'dev' is not fully merged. If you are sure you want to delete it, run 'git branch -D dev'.
Per the warning message, you can force delete the branch (and lose any unmerged changes in that branch) by using the
$ git branch -D dev
There are three ways of creating a new branch
feature which tracks the remote branch
git checkout --track -b feature origin/feature,
git checkout -t origin/feature,
git checkout feature- assuming that there is no local
featurebranch and there is only one remote with the
To set upstream to track the remote branch - type:
git branch --set-upstream-to=<remote>/<branch> <branch>
git branch -u <remote>/<branch> <branch>
developor the one created by user,
<branch>is user's branch to track on remote.
To verify which remote branches your local branches are tracking:
git branch -vv
Rename the branch you have checked out:
git branch -m new_branch_name
Rename another branch:
git branch -m branch_you_want_to_rename new_branch_name
The checked out file will overwrite not yet commited changes you did in this file.
This command will check out the file
file.example (which is located in the directory
path/to/) and overwrite any changes you might have made to this file.
git checkout some-branch path/to/file
You have to add
-- before the path if your file could be mistaken for a file (optional otherwise). No more options can be supplied after the
git checkout some-branch -- some-file
some-file is a file in this example.
To delete a branch on the
origin remote repository, you can use for Git version 1.5.0 and newer
git push origin :<branchName>
and as of Git version 1.7.0, you can delete a remote branch using
git push origin --delete <branchName>
To delete a local remote-tracking branch:
git branch --delete --remotes <remote>/<branch> git branch -dr <remote>/<branch> # Shorter git fetch <remote> --prune # Delete multiple obsolete tracking branches git fetch <remote> -p # Shorter
To delete a branch locally. Note that this will not delete the branch if it has any unmerged changes:
git branch -d <branchName>
To delete a branch, even if it has unmerged changes:
git branch -D <branchName>
git checkout --orphan new-orphan-branch
The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.
Use to push commits made on your local branch to a remote repository.
git push command takes two arguments:
git push <REMOTENAME> <BRANCHNAME>
As an example, you usually run
git push origin master to push your local changes to your online repository.
-u (short for
--set-upstream) will set up the tracking information during the push.
git push -u <REMOTENAME> <BRANCHNAME>
git pushes the local branch to a remote branch with the same name. For example, if you have a local called
new-feature, if you push the local branch it will create a remote branch
new-feature as well. If you want to use a different name for the remote branch, append the remote name after the local branch name, separated by
git push <REMOTENAME> <LOCALBRANCHNAME>:<REMOTEBRANCHNAME>
A branch is just a pointer to a commit, so you can freely move it around. To make it so that the branch is referring to the commit
aabbcc, issue the command
git reset --hard aabbcc
Please note that this will overwrite your branch's current commit, and as so, its entire history. You might loose some work by issuing this command. If that's the case, you can use the reflog to recover the lost commits. It can be advised to perform this command on a new branch instead of your current one.
However, this command can be particularly useful when rebasing or doing such other large history modifications.
You can quickly switch to the previous branch using
git checkout -
To list local branches that contain a specific commit or tag
git branch --contains <commit>
To list local and remote branches that contain a specific commit or tag
git branch -a --contains <commit>