Some commonly used Git commands related to branch operations.

Create a new branch (locally and remotely)

Create branch locally

We can use the following command to create a new branch

git branch <new_branch>

At this time, we are still in original branch. To switch to new branch, use the following command:

git checkout <new_branch>

The two steps can be combined with the following command:

git checkout -b <new_branch>

Create branch remotely

To create a remote branch, use the following command:

git push -u origin <local_branch>

The -u option sets a upstream branch of the same name for tracking, i.e., now our local branch corresponds to the remote branch of the same name.

Ref:

Create a git branch from a specific commit

Sometimes, we may find that we need to fix a flaw in a specific commit, but current work has not been finished. We may create a new branch from that commit, and after that, merge that branch to current branch.

This is how we do using git:

git branch fix_bug <commit_id>
git checkout fix_bug

<commit_id> is the hash of that commit as shown in the git log output.

The above command can also be simplified using -b for checkout:

git checkout -b fix_bug <commit_id>

After fixing the issue in branch fix_bug, we can then merge the change into the original branch, say, master branch:

git checkout master
git merge fix_bug

Ref:

Show branches

  • show all branches (local and remote): git branch -a
  • show all local branches: git branch -l
  • show all remote branches: git branch -r

Delete a branch

  • Delete local branch: git branch -d <branch_name>
  • Delete remote branch: git push -d <remote_name> <remote_branch>

Note that you can not delete a local branch when you are on it! You need first to checkout to another branch.

Ref:

Create a local branch from a remote branch?

I have a remote branch and I would like to create a local branch based on it. First, we need to fetch the remote changes:

git fetch

Then we can create a local branch from the remote branch:

git checkout -b local_branch_name remote_name/remote_branch

If you do not want to change the remote name, you can also also -t:

git checkout -t <name of remote>/<remote branch name>

It will create a local branch of the same name.

Ref:

Find remote tracking branch for local branch

If we want to find the remote local branch a local branch is tracking, we can use the following command:

git branch -vv

We can also use the git remote command to show remote tracking branch:

git remote show <remote name>

Ref: