How to Delete a Git Branch – Locally and Remotely?

How to Delete a Git Branch - Locally and Remotely?

Hi guys, if you are searching for the best tutorial guide on git delete branches then you are in right place. In this tutorial, I will show you how you can delete a locally or remotely git branch. For both scenarios locally or remotely, you have to run the different commands in your terminal, I’ll explain you in detail in this guide.

As you know Git is a very powerful software for tracking the changes in files. It is usually used when you are working in a team and have a big project then you need Git software to collaborate with other programmers in your team.

Git is a software to tracks the changes in the files and makes a separate version of that commit for all changes files. So in case of something happen wrong then you can restore the correct version of the file.

GitHub is the online hosting web interface software, where you can host your files. It keeps your original files and changes in the file store in a different version if you have committed to it.

Github gives you access to create a new repository and create branches. A repository can have many branches and you can create as many as you want like development, production, staging, etc.

After creating the repository and branch, you can start working on your project. So as working on a big project, you may need to create many branches for each task.

What is Git Branch?

Branching is an important feature of version control systems such as Git. It allows you to make a new version of an existing project in Git. You can make modifications to the new branch without impacting the project’s original version.

Let’s take an example, suppose you are working in a team on a project like “Online Insurance Application“. One day you may find bugs on the application and you have to fix them. Or you need to add an extra feature in the application. So here you need to create a separate branch to fix the bugs.

So you have to clone the branch and name it as “bug-fix“. Then you fixed all bugs and merged both branches. Now you have all updated files in your main branch and don’t want to keep the “bug-fix” branch. So you can delete it using the git command.

The above case could be locally or remotely but both have different commands to delete the git branch. So, the commands we will see in this tutorial.

Delete a Git Branch Locally and Remotely

So when you finished working on the branch and merged with the main branch then you no longer need that branch. So you have to delete that branch from locally and remotely using the git command.

I mean to say locally is that where you are working on projects such as localhost and remotely is that where you hosted your project files such as GitHub.

So let’s see how we can delete a locally and remotely git branch.

Git Delete Local Branch

To delete a local branch from your local machine, you have to use the git command that is git branch -d. The -d flag tells Git is that you want to delete that branch.

NOTE: Before deleting the git branch make sure you are in that branch which you are not deleting. Because git will not allow you to delete currently working on a branch.

So I would recommend you to switch to the main branch before deleting any other branches. For example, git checkout main

Syntax:

  • -d : It will delete the branch if the branch has already merged into main branch or pushed on remote.
  • -D : It will force delete the branch, even if you hasn’t merged with main branch or pushed on remote.

Like the above example, we have created a branch called “bug-fix” to fix the bugs on the project. Then we merged into the main branch. So now you want to delete the “bug-fix” branch. So use the following command as a reference to delete the branch from the local system.

Normally delete git branch:

Forcefully delete git branch:

Git Delete Remote Branch

To delete a remote branch from the server where you hosted your project files, you have to use the git command like git push origin –delete branch. This command basically tells Git to push your local changes into remote and then delete the branch that you have specified.

Syntax:

Let’s take an example as we did above. Suppose we have a branch “remote-bug-fix” on the remote server and you already merged into the main branch. Now you want to delete this branch then use the following command as a reference.

The above command will delete the remote branch from your GitHub versioning system where you hosted your files.

Shorter Command to Delete a Remote Git Branch

There is a shorter command that you can use to delete the branch from git. In this command you don’t need to pass the --delete flag and we will use : instead.

So see the following shorter command to delete the remote git branch.

Syntax:

And if we want to delete the above same branch from remote then we will use the shorter command like the following.

After deleting the remote branch, you should run a fetch command to sync our local files with the remote files. The fetch command will retrieve the updated files and changes into your local repository.

So use the following to fetch the remote files into the local repository and keep the local repository up-to-date.

The -p flag means, delete any local branches that no longer exist on the remote repository. For more detail, you can read the git fetch command tutorial.

Conclusion

So in this tutorial, we learned how to delete a local git branch and remote git branch. And learned about forcefully deleting the local branch, even if it has not merged with the main branch. You also see the shorter command to delete the remote branch in git.

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published.