How to Delete Branch in Git: Streamlining Git Repository

Git branches allow you to work on different versions of a project simultaneously, but over time you can accumulate branches that are outdated, merged, or no longer needed. Deleting old branches helps simplify your repository by removing clutter. Here are some best practices for identifying and removing branches in Git.

When to Delete Git Branches

There are a few key times when you’ll want to delete branches:

  • After Merging a Branch: Once you’ve merged a feature branch into main, you likely don’t need to keep the branch around. You can safely delete it.
  • Abandoned Branches: If you’ve started working on a feature but decide to shelve it, delete the branch rather than leaving old unfinished work in your repository.
  • Before Releases: Do some cleanup of merged branches before major releases to keep your repository history clean for the release.
  • Temporary Branches: Some branches are only intended to be temporary, like experiment branches. Delete these once you’re done with them.

Cleaning outdated branches keeps your list of branches focused on active work.

How to Delete Local Branches in Git

To delete a local branch, use the git branch -d <branch> command:

git branch -d my-old-branch

This will delete the branch my-old-branch from your local repository if it has been successfully merged into the upstream branch.

To force delete a branch, even if not merged, use -D instead of -d:

git branch -D my-branch

Some things to note about deleting local branches:

  • Check Branch Status First: Before deleting, double check that the branch has been merged or is safe to remove.
  • Use Caution with Unmerged Branches: Don’t force delete unmerged branches unless you’re sure – you could lose work.
  • Delete Related Remote Branch: When you delete a local branch, also prune the remote branch if you pushed it.

Pruning Remote Branches in Git

If you’ve pushed branches to a remote repository, you’ll also need to prune remote branches after deleting the local copies. This removes stale branches from the remote.

To prune remote branches that don’t have a local counterpart:

git fetch -p

Or to prune a specific remote branch:

git push origin --delete my-old-branch

Some tips for managing remote branches:

  • Communicate Before Deleting Remote Branches: Warn collaborators before pruning shared remote branches.
  • Review Remotes First: Check all remote repositories before pruning to avoid surprises.
  • Prune After Merging: Wait until a feature branch is merged and deleted locally before pruning the remote copy.

How to Delete Git Branches Using Git GUI Tools

If using the command line isn’t your preference, most Git GUI clients also provide an interface to delete branches:

GitHub Desktop

To delete a branch in GitHub Desktop:

  1. Click the Current Branch dropdown.
  2. Right click the branch you want to delete.
  3. Choose Delete…

This will delete both the local and remote branch.

GitKraken

In GitKraken, delete branches by:

  1. Right click the branch name in the left panel.
  2. Choose Delete Branch from the context menu.

The branch will be removed from the local and remote repositories.

Other Git GUIs

Look under the Branch menu for options to delete or prune branches within your Git desktop app. The process is generally straightforward.

Tips for Streamlining Your Git Repository

Here are some additional tips for simplifying your Git repository:

  • Enforce Branch Cleanup: Add branch cleanup tasks to your Git workflow policy so it happens consistently.
  • Review Existing Branches: Periodically browse all branches to identify old branches to remove.
  • Delete Merged Pull Requests: Enable options in GitHub or your code host to auto-delete branches after merged PRs.
  • Use Git Stash: Stash unfinished work instead of making branches you won’t come back to.

Conclusion

Removing outdated branches is an important part of Git repository maintenance. Keeping your branch list clean helps the team stay focused on active work and gives a clearer view of current progress.

The git branch and git push commands make it straightforward to delete local and remote branches once you’re ready. Many Git GUI tools also provide user-friendly interfaces to prune branches.