Git Delete a Branch

Learn via video courses
Topics Covered

Git, a fundamental version control system for web developers, leverages branches to manage code evolution. While strategies like GitFlow and GitHub Flow enhance development, they can clutter local repositories with unnecessary branches. Therefore, understanding how to efficiently remove both local and remote branches in Git is crucial for maintaining a streamlined development environment.

When to Delete Branches?

To understand when we can perform the git delete branch, we should first learn the need for branching and then we can understand when the branch needs to be deleted.

As we know that a new branch is a copy of the master branch which is created for bug fixes and for the addition of new features. After the bug is fixed or new features are added, we can merge the branch to the master branch. The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

Command for Creating a New Branch

Command for Listing all the Branches

or

Now that the bug fixing branch or the feature branch is merged into the master branch, we can easily perform the git delete branch operation to delete the unwanted (no longer needed) branches.

Branching provided better management of the project development so we must delete the branches after their need is satisfied so that we don't have a lot of any longer needed branches which makes the overall structure a complete mess. The git delete branch operation is considered a very good repository hygiene practice. When there is less number of branches then the performance of the repository is also increased.

How to Delete Local Git Branches?

Before deleting any branch we should make sure that the changes are pushed to the main or master branch and it is safe to be deleted. Now, to delete a branch locally, we can use the Git bash application or a Command Window in the root of the Git repository. First, we need to switch to another branch if we are in the branch that has to be deleted. For switching to the other branch, we can use the git checkout command and then issue the following command:

We can also use the -d flag for deleting a branch. The overall command for the same is:

Both the above commands will work the same. It is just the difference of command (syntax).

Refer to the image provided below for more clarity.

How to delete local Git branches1

Note: We can use the git branch command to list down all the branches of the current git repository.

If we try to delete our current branch then Git will show the following error.

How to delete local Git branches2

We can even check whether the branch is deleted or not using the following command:

The active branches are shown in green color while the deleted branches are shown in red color. For more clarity, please refer to the demo provided below. How to delete local Git branches3

There may be situations in which we have created a branch and made some changes in the branch but we are not satisfied with the changes and we want to delete the branch. Now if we try to delete a branch having non-merged changes then Git will not allow us to do so and raise an error. If we are completely sure that we do not wish to merge the changes and delete the branch then we can use the D flag which will forcefully delete the branch. The overall command for the same is:

Remove vs Local Git Branch Deletes

As we know that GitHub is a place or server where we host our repository remotely. So whenever we delete a branch locally then we need to delete the branch remotely as well. The branches on the remote server are not deleted when we delete the local branch because the changes have to push and pulled from the remote branch to the local branch.

We can verify this by logging into our GitHub account and clicking on the toggle button in the branches tab in our GitHub repository.

Deleting a Branch Remotely

Let us now learn how we can delete a remote branch. For deletion of the remote branch, we can push the changes of the git delete branch operation to the remote server using the following command:

If the name of our remote server is not origin then we can place the name of our remote server in the place of origin in the above command.

We also have a smaller command for deleting the remote branch. The command is:

Now, if we are getting any error in the above two commands then there may be a possibility that the branch has already been deleted by someone.

The error can be:

So, we must sync and synchronize the remote repository with our local repository. For syncing, we can use the git fetch -p command.

There is also a button present in GitHub to delete the branch so if we do not want to write the commands, we can use the button for the same.

Deleting a Branch on GitHub

So far we have learned how to delete a local branch and a remote branch. Now, we can also delete a branch using our central server i.e. GitHub. If we go to any repository, we can see a branch section on the left side of the screen.

How to delete local Git branches4

So, we can toggle between the branches using GitHub and we can also delete the branches. To delete a particular branch, open the branches menu and scroll to the branch that we want to delete, then click on the EMPTY symbol. If we try to delete a branch that is associated with at least one open pull request, we must confirm our intent to close the pull request(s).

If we delete a head branch after its pull request has been merged, GitHub checks for any open pull requests in the same repository that specify the deleted branch as their base branch. GitHub automatically updates any such pull requests, changing their base branch to the merged pull request's base branch.

We must remember that if the branch we want to delete is associated with an open pull request, we must merge or close the pull request before deleting the branch.

Conclusion

  • Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base, etc.
  • Branching provided better management of the project development so we must delete the branches after their need is satisfied.
  • To delete a branch locally, we can use the Git bash application or a Command Window in the root of the Git repository. We can use the git branch --delete "branch-name" command to delete the branch.
  • The git delete branch operation is considered a very good repository hygiene practice. So, we must delete the unused branches.
  • But before deleting any branch we should make sure that the changes are pushed to the main or master branch and it is safe to be deleted.
  • We can use the git branch command to list down all the branches of the current git repository. We can use the git branch -a command to check whether the branch is deleted or not.
  • If we do not wish to merge the changes of a branch and delete the branch then we can use the D flag which will forcefully delete the branch.
  • Whenever we delete a branch locally then we need to delete the branch remotely as well. The branches on the remote server are not deleted automatically.
  • For deletion of the remote branch, we can push the changes of the git delete branch operation to the remote server.
  • There is also a button present in GitHub to delete the branch so if we do not want to write the commands, we can use the button for the same.