How to Delete a Git Branch Remotely and Locally?

Photo of author

By admin

Git branches can be referred to as integral parts of software development and every developer will admit this fact. No matter whether you’re developing an app, or fixing bugs on an existing software platform, Git branches come in handy. You often need to collaborate with other developers and work as a team to develop a software application. In such a scenario, Git branches tend to be very essential to keep your line of work separate.

However, you may need to delete Git branches on and off due to different reasons. Aren’t you familiar with the process of deleting Git branches? Or are you unsure about why you need to do that? This article will help you find the right answers to all your questions regarding Git branch deletion.

What is a Git Branch?

You are bound to use software repositories while developing a software program or application. A Git branch is nothing but a separate version of the software repository you’re using. A Git branch makes your task shift from the main project to a separate pathway. That’s how you get the opportunity to differentiate your work from your team members’ works. In short, your Git branches showcase the moderations you have made to an ongoing software development project.

Whenever you make some changes to the existing code base to add some new features, you create a new Git branch so that your codes don’t mix up with the main code base. So, keeping a track of your work becomes way more effortless. Later, you can delete the Git branches you created by merging your changes with the main project.

Disadvantages of Git Branches that Lead to their Deletion

Every software developer is familiar with the efficiency and effectiveness of Git branches. If you’re just starting with software development, you might be curious about the disadvantages of Git branches. Despite being effective, Git branches come with some potential consequences too. Let’s have a quick look at them.

Merger Interruptions are Genuine Threats

How does it feel when you’re unable to merge a Git branch with the main code base after working hours on the codes of the branch? You may face something like this anytime without having any clue. Once you become the victim of a merger inconvenience, you need to spend prolonged hours fixing the merger issue. After putting effort into adding new features to an application, it becomes annoying to waste extra hours in merging your Git branches to the main project.

Displacement of Codes

When you’re working with your teammates remotely using Git branches, the chances of code misplacements become higher. As you’re creating a branch pathway to work on your codes, you are supposed to check the main project repeatedly to see if any of your codes were altered or not. That’s a common scenario when you plan to create a Git branch to work independently. As a developer, you surely know the pain of getting a code displaced from your developing project. Also, it’s next to impossible to check the changes of the main project every other minute. So, this is a major pitfall of Git branching.

Feedbacks can be the Point of Concern

Updating your team members and seniors about what you’re doing isn’t possible every time. Negative feedbacks and reworks are bound to come your way when you create a separate Git branch to work on a project. As you work using a specific Git branch, your team members and project supervisors aren’t aware of what you’re doing. Your work may not necessarily please them always. Therefore, you might have to work overtime to rework your project and make the desired changes. Ultimately, the entire project will get delayed and you have to put double effort too.

These are the consequences that you would face as you start using Git branches to complete your projects. Anyway, that doesn’t mean you shouldn’t use Git branches. If a long project is assigned to you, make sure you utilize Git branches on a short-term basis. Also, you can avoid the pitfalls by integrating your work with the main code base continuously. Divide your entire work into small parts and keep merging your work with the main code base as you complete every small part. That’s how you can reduce rework requirements.

General Reasons for Deleting Git Branches from Repository

Deleting Git branches from a repository is a common thing every developer performs regularly. But why is it important to delete Git branches and clear Git history regularly? The following points make it essential to delete the Git branches you don’t use anymore:

Once you create a Git branch and complete your project by proceeding with that branch, you need to raise a pull request. A pull request is made to merge a Git branch with the main project. Merging a Git branch is only possible when your pool request is accepted. Now, once the pull request of a particular Git branch gets accepted and the branch is merged with the main project, the branch is of no use. You can’t use a merged Git branch in another way. So, it’s a better idea to delete the Git branch from your repository.

When your repository gets overloaded with Git branches, it becomes hard for you to keep track of all of them. Also, you may face difficulty in finding out the necessary Git branch for a particular project in the crowded repository. Hence, deleting used and old Git branches from your repository is a mandate.

These are the main two reasons to delete the old Git branches from your repository and keep your repository clutter-free.

How Risky is it to Delete Git Branches from Repository?

Deleting Git branches from your repository is a harmless practice. Once you have successfully merged a Git branch with the main project, you’re free to delete the Git branch without thinking twice. Anyway, you must wait until the merger process is completed. By chance, if you delete a Git branch before the merger is completed, all the changes you made will get dismissed. As a result, all your efforts will get drained and you need to rework again. However, once the merger is completed, it’s completely safe to delete a Git branch without having affecting your work. So, never change anything in your repository before your project is complete.

Way to Delete Git Branches Remotely and Locally

Limiting the number of Git branches in your repository is a prior task for you to access the necessary Git branches whenever required. Now, the question is how do you do that? Go through the following sections to have a clear idea about the entire process.

Before moving to the main process, you are supposed to be familiar with your repository. You need to know the Git branches that are present in your repository currently. To check all the local branches available in your repository in a list form, use this command:

git branch

When you want to track all the remote branches, use this command:

git branch -r

This command helps you check all the commands present in your repository, including local branches and remote branches:

git branch -a

As mentioned above, you’re supposed to remove the merged branches that still exist in your repository. But the commands mentioned above won’t show the merged branches separately. If you want to detect the merged branches in your repository, you need to use separate commands.

To find out local merged branches, type in this command:

git branch –merged

For remote merged branches, use this command:

git branch -r –merged

The process to Delete Local Git Branches

When you’re about to delete a single local Git branch, this command will make your task way easier:

git branch -d <branchname>

The best thing about this command is that it won’t work when you try to use it for deleting an unmerged branch. As you input the command for deleting an unmerged branch, your system will prompt an error code.

Therefore, to delete an unmerged local Git branch, use this command:

git branch -D <branch>

You can also choose to delete multiple Git branches at once. It becomes possible with this command:

git branch –merged | egrep -v “(^\*|branch|branch)” | xargs git branch -d

With this portion, you can select all the Git branches that are already merged and are of no use:

git branch –merged

This portion of the command line excludes the branches you want to keep in your repository:

egrep -v “(^\*|branch|branch)”

This portion of the command line deletes the rest of the selected Git branches at once:

xargs git branch -d

Process of Deleting Remote Branches

How do you delete non-existent tracking branches?

Sometimes some local branches remain in your repository to track remote branches you have already deleted. Those branches need to get deleted from your repository too. You can use the following command to track the branches easily:

git remote prune <remote> –dry-run

To delete them, input this command:

git remote prune <remote>

Now, when you’re planning to delete a single remote branch, this command will get the job done for you:

git push <remote> –delete <branch>

This command works for all merged and unmerged Git branches that are present in your repository. Also, you have the option to delete all the remote branches of your repository at once. Let’s see how it is done.

This single command deletes all the merged remote branches within seconds:

git branch -r –merged | egrep -v “(^\*|branch|branch)” | sed ‘s/origin\///’ | xargs -n 1 git push origin –delete

After executing this command, you need to input the following command to complete the process:

xargs -n 1 git push origin –delete

That’s all you need to do to delete all the merged remote branches from your repository effortlessly.

How to Restore a Deleted Git Branch?

Often programmers delete Git branches accidentally and think of restoring the deleted branches. Unfortunately, there is no way to restore a Git branch if you have deleted that remotely. But a Git branch can be restored when you delete it locally. That’s why most developers and programmers prefer to delete Git branches locally so that they can recover them comfortably.

Also, make sure you attempt to restore a Git branch immediately after deleting. Otherwise, you may not be able to restore it conveniently. Now, what’s the process to restore a deleted Git branch?

When you try to restore a Git branch right after deleting it, you are supposed to find this command on the terminal:

Deleted branch <your-branch> (was <sha>)

Now, you can use this command to restore the deleted Git branch:

git checkout -b <branch> <sha>

You can use this command to find the ‘sha’:

git reflog

Follow the other steps mentioned above to complete the restoration.

As mentioned above, you can’t restore a Git branch when it is deleted remotely. So, be cautious while deleting old Git branches remotely from your repository. Make sure you exclude the necessary Git branches from the deletion list while deleting remote Git branches.


In this article, you have acquired sound knowledge about Git branches. Needless to specify, the proper utilization of Git branches makes your programming tasks way more effective. Though there are certain consequences of using Git branches, we have shared some tips to overrule the consequences in this article. We suggest you use Git branches to enhance the quality of your outputs to some extent. No need to refrain from creating and using Git branches due to the fear of merger issues.

Also, we have provided a detailed guideline on how you can delete old and unusable Git branches from your repository. Even if you’re a beginner, you can delete Git branches smartly by following the instructions given above. We have attempted to break down the process into simple steps and make the process as easy as possible. We have also mentioned the procedure to recover Git branches you’ve deleted accidentally. So, there’s no point in hesitating while using Git branches to complete your programming tasks. All you need to do is to adhere to the instructions. So, put your knowledge into work and make your software development project smarter and better.

Leave a Comment