Chapter 5. Work With Branches

Create Branch and Check Branch Status – Git Branch

git branch is a multi-use command. We'll explain the three major use cases on this page.

The git branch command use cases

  1. Create a new branch
  2. Check branch status
  3. Delete an unused existing branch

1. Create a new branch

Creating a new branch is the first action in the branch operation. When running git branch [new branch name], a new branch is created from the original branch where you are located.

However, you are still on the original branch until you switch the current branch to the newly created branch by running the git checkout or git switch commands, which will be explained on the next pages.

Also, one important note here is that a new branch diverges from the latest commit (HEAD) of the parent branch. Unless you rebase it (to be explained later), the diverged point remains the same throughout the entire life cycle of the branch.

Example

If you want to create a new branch named Branch_A, run the command below.

git branch Branch_A

The image below illustrates an example of how Branch_A is created from the master branch.

Create-Branch-and-Check-Branch-Status--Git-Branch

2. Check branch status

When you simply run the git branch command without any additions, the command line returns the branch status. The branch status includes a list of branches and the information about which branch you are located in.

Example

In the following example, there are two branches: the master branch and Branch_A. The branch with " * " is the current branch, which is the master branch in this case.

Command Line - INPUT
git branch
Command Line - RESPONSE
  Branch_A
* master

The git branch command with the -a option returns the list of branches including branch information in the Remote Repository recorded in the Local Repository if the Local Repository is already linked with the Remote Repository.

Example

Command Line - INPUT
git branch -a
Command Line - RESPONSE
  Branch_A
* master
  remotes/origin/master

How to manage branches in the Remote Repository will be explained in the next chapter.

3. Delete unused branch

When you don't need a branch after merging it with another branch, you can delete the branch by running the git branch -d [existing branch name] command.

Example

Command Line - INPUT
git branch -d Branch_A
Command Line - RESPONSE
Deleted branch Branch_A (was 86c37e9).

If the branch you are trying to delete has not been merged with any other branches, the command line gives you an error.

Command Line - RESPONSE
error: The branch 'Branch_A' is not fully merged.
If you are sure you want to delete it, run 'git branch -D Branch_A'.

If you really want to delete the branch (for example, when you mistakenly made a new branch), you can use the -D option. git branch -D [existing branch name] allows you to delete a branch regardless of its merge history.

Example

Command Line - INPUT
git branch -D Branch_A
Command Line - RESPONSE
Deleted branch Branch_A (was b43edc9).

4. Rename branch

There are other usages of the git branch command. For example, if you want to change a branch name, you can use the -m option. Switch to the branch you want to rename. Then, run the git branch -m [new branch name] command.

Example

To change the Branch_A's branch name to Branch_B, run the command below on Branch_A.

Command Line - INPUT
git branch -m Branch_B

Unless there is an error, the command line won't give you a response. To check if the branch name is successfully changed, run the git branch command.

Example

Command Line - INPUT
git branch
Command Line - RESPONSE
* Branch_B
  master

git branch is a multi-use command. We'll explain the three major use cases on this page.

The git branch command use cases

  1. Create a new branch
  2. Check branch status
  3. Delete an unused existing branch

1. Create a new branch

Creating a new branch is the first action in the branch operation. When running git branch [new branch name], a new branch is created from the original branch where you are located.

However, you are still on the original branch until you switch the current branch to the newly created branch by running the git checkout or git switch commands, which will be explained on the next pages.

Also, one important note here is that a new branch diverges from the latest commit (HEAD) of the parent branch. Unless you rebase it (to be explained later), the diverged point remains the same throughout the entire life cycle of the branch.

Example

If you want to create a new branch named Branch_A, run the command below.

git branch Branch_A

The image below illustrates an example of how Branch_A is created from the master branch.

Create-Branch-and-Check-Branch-Status--Git-Branch

2. Check branch status

When you simply run the git branch command without any additions, the command line returns the branch status. The branch status includes a list of branches and the information about which branch you are located in.

Example

In the following example, there are two branches: the master branch and Branch_A. The branch with " * " is the current branch, which is the master branch in this case.

Command Line - INPUT
git branch
Command Line - RESPONSE
  Branch_A
* master

The git branch command with the -a option returns the list of branches including branch information in the Remote Repository recorded in the Local Repository if the Local Repository is already linked with the Remote Repository.

Example

Command Line - INPUT
git branch -a
Command Line - RESPONSE
  Branch_A
* master
  remotes/origin/master

How to manage branches in the Remote Repository will be explained in the next chapter.

3. Delete unused branch

When you don't need a branch after merging it with another branch, you can delete the branch by running the git branch -d [existing branch name] command.

Example

Command Line - INPUT
git branch -d Branch_A
Command Line - RESPONSE
Deleted branch Branch_A (was 86c37e9).

If the branch you are trying to delete has not been merged with any other branches, the command line gives you an error.

Command Line - RESPONSE
error: The branch 'Branch_A' is not fully merged.
If you are sure you want to delete it, run 'git branch -D Branch_A'.

If you really want to delete the branch (for example, when you mistakenly made a new branch), you can use the -D option. git branch -D [existing branch name] allows you to delete a branch regardless of its merge history.

Example

Command Line - INPUT
git branch -D Branch_A
Command Line - RESPONSE
Deleted branch Branch_A (was b43edc9).

4. Rename branch

There are other usages of the git branch command. For example, if you want to change a branch name, you can use the -m option. Switch to the branch you want to rename. Then, run the git branch -m [new branch name] command.

Example

To change the Branch_A's branch name to Branch_B, run the command below on Branch_A.

Command Line - INPUT
git branch -m Branch_B

Unless there is an error, the command line won't give you a response. To check if the branch name is successfully changed, run the git branch command.

Example

Command Line - INPUT
git branch
Command Line - RESPONSE
* Branch_B
  master