Branches management in Git

Create a new branch

# Create a new branch named develop
$ git branch develop

You can also download a new branch from remote.

Switch to a branch

# Switch to develop branch
$ git checkout develop

-b option allows you to create a new branch and switch to it in one command:

# Create develop branch and switch to it.
$ git checkout -b develop

To checkout to a remote branch, run:

# Checkout to origin/master
$ git checkout origin/master

Note: After switched to a remote branch, you would be in a detached state which means the branch won’t move after you made a new commit here. The reason is that you can not modify a remote branch, it is read-only for you.

List branches

# List branches
$ git branch
* develop
  master

* indicates the branch that you are currently on.

To see the branches and their last commit, add -v option:

# List branches and their last commit message
$ git branch -v
* develop 625cdcb initial commit
  master  b1f092d feat: add settings

To see the branches that have been merged or unmerged into the current branch, use --merged option:

# List only the branches that have been merged to the current branch
$ git branch --merged.

The branches that have been merged can be deleted safely, since their commits have been contained in the current branch.

To see the branches that have not been merged to the current branch, use --no-merged option:

# List only the branches that have not been merged to the current branch
$ git branch --no-merged

Rename a branch

# Rename old-branch to new-branch
# -m or --move, rename a branch
$ git branch -m old-branch new-branch

Delete a branch

# Delete test branch
$ git branch -d test

# Force to delete a branch if it has not been merged yet.
$ git branch -D test

Note: you can not delete the branch you are currently on, first switch to another branch.

See more : how to delete a local and a remote branch.

Setting up a local branch to track a remote branch in Git

Set up upstream branch for a branch

To set up a local branch to track a remote branch, use :

# Suppose you are on local temp branch
# Set up current branch temp to track topic branch from origin.
$ git branch -u origin/test

Now origin/topic is the current branch’s upstream branch, you can use @{u} to reference it for shortcut instead of origin/topic.

# Merge from upstream branch, 
# here it is origin/topic if you are on local temp branch.
$ git merge @{u}

Check which remote branch a local branch is tracking

If you want to see which remote branch a local branch is tracking, run below command:

# List local branches and which remote branches they are tracking.
$ git branch -vv
develop 99914fb [origin/develop: ahead 2] fix: missing comma
master 04a4f6a [origin/master: ahead 1, behind 2] feat: add menu
* temp 94f4439 [origin/topic: ahead 2] feat: add custom post type
test ac5446a [origin/test] initial commit

This command also tells whether a local branch is ahead or behind its upstream branch.

Checking out a new remote branch in Git

If you have just one remote, the easiest way to check out a new branch from the remote is:

# Download a new remote branch named topic and switch to that branch
$ git checkout topic

What above command does includes :

  1. Fetch “topic” branch from remote

  2. Set up its upstream branch :

    Here the branch “topic” is tracking /topic. Thus when you execute git pull, Git will know where to get update.

  3. Switch to “topic” “branch

Note: $ git checkout --track origin/topic does the same thing with $ git checkout topic if you just have one remote like it is named origin.

--track sets up which remote branch the new branch is tracking.

Here -b is ignored, meaning the new branch is derived from the remote branch.

git chekcout a shortcut. If you have multiple remotes or you want to give a different branch name with the remote one, use the full format which specifies the remote:

# Create a new branch from a remote branch and switch to it.
$ git checkout -b <branch> <remote>/<branch>;

# Examples:
# Create a new branch setupfix from origin/issue13 and switch to setupfix.
$ git checkout -b setupfix origin/issue13

Checking local branch is ahead or behind remote branch

To see a local branch is ahead or behind its upstream branch, use:

# List all branches with more information:
# id, message and relationship with their upstream branches
$ git branch -vv
develop 99914fb [origin/develop: ahead 2] fix: missing comma
* master 04a4f6a [origin/master: ahead 1, behind 2] feat: add menu
temp 94f4439 feat: add custom post type
test ac5446a [origin/test] initial commit

Above information illustrates the following relationship with remote ones:

  • develop branch is ahead origin/develop by 2 commits
  • master branch is ahead by 1 commit, and behind by 2 commit
  • temp branch is not tracking any remote branch
  • test branch stays up to date

Note that the remote information may not be up to date, it just present situations the last time you communicated with remote server.

To see up to date information, you can first update changes from remote :

# Fetch update from all remote tracking branches. Note it does not
# merge them to the local branches.
$ git fetch --all
$ git branch -vv

Deleting a local or remote branch in Git

Use git branch -d to delete a local branch. Use git push --delete to delete a branch on the remote repository.

Delete a local branch

Make sure you are not on the branch to be deleted :

# Delete local branch 'test'

# First switch to another branch if you are on 'test' branch
$ git checkout master

# Do delete action
$ git branch -d test

If your branch has not been merged, use -D option to force delete it :

$ git branch -d test
error: The branch 'testing' is not fully merged.
If you are sure you want to delete it, run 'git branch -D test'.

$ git branch -D test
Deleted branch test (was 4f10b4a).

Delete a remote branch

Use --delete option to push command :

# Delete a remote branch 'test' on Git version above v1.7.0
$ git push origin --delete test

The command above is available after Git v1.7.0. If you are using an earlier version :

# Push empty source to remote branch of 'test', meaning deleting it
$ git push origin :test