Gangmax Blog

Git Branch Operations

From here.

Create a new branch

1
2
3
4
5
6
7
8
9
10
# 1. Create a new local branch based on a specific remote branch and use
# it as the current branch.
git checkout -b bugfix-v1.0-jira-0001-max origin/release-1.0-bugfix-branch
# 2. Create a new local branch based on a specific remote branch without
# change the current branch.
git branch bugfix-v1.0-jira-0002-max origin/release-1.0-bugfix-branch
# 3. Create a new local branch, and push it to remote server as a new branch.
# "-u" equals to "--set-upstream".
git checkout -b feature-v1.1-jara-0002-max
git push -u origin feature-v1.1-jara-0002-max

Note that, when you create a new local branch, if you did not track it with a specific remote branch like “1” or “2” command, and when you push the branch to remove server, you did not set the “-u|–set-upstream” option like “3” command either, the push command “git push origin abc” will succeed, assume the branch name is “abc”. But the pull command “git pull” will fail with the error:

1
2
3
4
5
6
7
8
9
10
11
> git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> abc

List branches

1
2
3
# 1. List "All" branch with "Verbose" information. You can also use "-l"
# (local) or "-r"(remote) arguments.
git branch -av

Comments