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.