Git: How to Delete and Rename Branches

Updated: January 27, 2024 By: Guest Contributor Post a comment

Overview

Working with branches is a core part of any developer’s workflow when using Git. Branches offer a way to segregate your work from the main code base, often referred to as the ‘master’ or ‘main’ branch. In your day-to-day development activities, you may find yourself needing to rename or even delete branches. This tutorial will walk you through the processes of deleting and renaming branches in Git, with practical examples and the outcomes you can expect. We will progress from basic to advanced examples, helping you to manage your Git branches with confidence.

Optional: What are Git Branches?

Before we dive into deleting and renaming branches, it’s important to understand what branches are and why they are essential. In Git, branches are effectively pointers to a specific commit in your repository’s history. When you create a new branch, Git creates a pointer to the commit you’re currently on, allowing you to diverge from the original code base without affecting it.

Deleting a Branch

Deleting branches that you no longer need helps to keep the repository clean and manageable. Here’s how you can delete branches in Git:

Deleting a Local Branch

git branch -d <branch-name>

This command deletes the local branch named <branch-name>. The -d flag stands for –delete, which is a safe operation that only deletes the branch if it has been fully merged in its upstream branch or in HEAD.

Output:
Deleted branch <branch-name> (was <commit-hash>).

Force-Deleting a Local Branch

git branch -D <branch-name>

If you want to forcefully delete a local branch, regardless of its merge status, use the -D flag (capital D), which stands for –delete –force:

Output:
Deleted branch <branch-name> (was <commit-hash>).

Deleting a Remote Branch

git push <remote> --delete <branch-name>

To delete a remote branch, you would use the –delete flag with the git push command. Replace <remote> with the name of your remote repository (typically ‘origin’):

Output:
To <repository-url>
 - [deleted]         <branch-name>

Renaming a Branch

If you have named a branch incorrectly or need to change it for any reason, you can easily rename it using the following commands:

Renaming the Current Branch

git branch -m <new-name>

To rename the branch you’re currently on, use git branch with the -m flag:

Output:

Renaming a Different Branch

git branch -m <old-name> <new-name>

To rename a different branch, you need to specify both the current name and the new name:

Output:

Renaming a Remote Branch

Rename a remote branch by renaming the local reference, pushing that new branch to the repository, and then deleting the old branch:

git branch -m <old-name> <new-name>
git push <remote> :<old-name> <new-name>
git push <remote> -u <new-name>

This will effectively rename the branch on the remote repository. Ensure to communicate this change with your team as others may be using the old branch name.

Advanced Branch Management

As we delve deeper into branch management, here are some more advanced examples and concepts.

Changing the Default Branch Name

You might want to change the default branch name from ‘master’ to ‘main’ or any other nomenclature preferred:

git branch -m master main
git push -u origin main

Pruning Remote References

Over time, your local Git repository might have references to remote branches that are deleted. To clean up these references, use:

git remote prune <remote>

Batch Deleting Branches

For deleting multiple branches at once, you could use:

git branch | grep -E '<pattern>' | xargs git branch -D

This would delete all branches that match the <pattern> provided.

Conclusion

Branch management is a vital skill for any Git user. Whether you’re cleaning up old branches or restructuring branch naming, understanding how to delete and rename branches effectively is crucial. Always proceed with caution, double-check branch statuses before deletion, and ensure teamwork and communication when making changes to remote branches.