Creating and Deleting Branches using Git

January 10, 2022
Cover image for Creating and Deleting Branches using Git

Creating and deleting branches using Git are a crucial part of working with version control software. They allow you to build on previous work and helps teams collaborate by being able to work on new features or bug fixes simultaneously. Developers can simply create new branches off of the main branch and work on their changes. When they are done working on their changes, a pull request is created to merge their changes on the main branch.

How to Create New Branches using Git

Creating branches lets you work on new features of an application without directly modifying the main branch. This allows developers to work on different features of an application at the same time.

To create a new branch, run the commands:

git branch [NEW_BRANCH_NAME]
git checkout [NEW_BRANCH_NAME]

The first command creates a new branch with the name you specified. The second command switches the current branch you are on to the one you just created.

There is a shortcut to handle the above steps all in one command. We could instead execute:

git checkout -b [NEW_BRANCH_NAME]

Example: Creating and Switching to a Branch

git branch test
git checkout test
OR
git checkout -b test

How to Delete a Local Branch using Git

There may be cases where we may want to checkout a branch and see what changes are being made. If we don't need the branch anymore, we may decide to delete it locally. This can be done with the following command:

git branch --delete [LOCAL_BRANCH_NAME]

As a shorthand, we can use the -d flag:

git branch -d [LOCAL_BRANCH_NAME]

If we made some changes to the branch but want to delete it without merging those changes, run:

git branch --delete --force [LOCAL_BRANCH_NAME]

A shorter version of this is to use the -D flag:

git branch -D [LOCAL_BRANCH_NAME]

Example: Switching to and Deleting a Local Branch

Let's say there's a branch called 'test' and we want to see what changes were made:

git checkout test

After looking at the branch, we decide we don't need it anymore locally. So we want to delete it:

git branch --delete test
OR
git branch -d test

If we made changes to the 'test' branch and we decide we don't need them, we can delete the branch with:

git branch --delete --force test
OR
git branch -D test

How to Delete a Remote Branch using Git

If we pushed a branch to some code hosting platform, such as GitHub or GitLab, we may decide to delete those branches there. The command to do so is:

git push [REMOTE_NAME] --delete [REMOTE_BRANCH_NAME]

Alternatively, you could write:

git push [REMOTE_NAME] :[REMOTE_BRANCH_NAME]

Example: Deleting a Remote Branch

This deletes the 'test' branch assuming it was pushed to the remote repository on the 'origin' remote:

git push origin --delete test
OR
git push origin :test

Conclusion

We learned the commands for creating and deleting branches using Git. It's especially useful to know these commands when working in a team as there may be many branches being worked on at the same time.