Git is one of the most popular version control systems that helps manage projects and collaborations among developers. While working with Git, branches are instrumental in making modifications, trying out new features, and maintaining the integrity of the main codebase. But when a branch serves no further purpose, it’s a good habit to delete it so your repository does not get cluttered.
So in this guide, we are going to address the question: How do you delete a git branch both locally and remotely? We will explain it stepwise so even people without prior knowledge can understand it.
Understanding Git Branches
Before getting into the deletion of branches, we should take a moment to understand the meaning of a Git branch.
Simply put, a Git branch is a reference to a particular commit in the history of a project. The default branch on most repositories is assigned as main or sometimes master. With a branch, you can work on changes without affecting the main branch.
For instance, when working on a new feature, you might create a branch named feature-login. When the needed work is completed and merged into the main, there’s no reason to keep the feature-login branch, and it is better deleted to avoid clutter.
How to Delete a Git Branch Locally
When a branch is merged and it is not needed anymore, you can delete it from your local machine with the steps below.
Step 1: View All Branches
To view all branches that are available on your local system, use this command:
git branch
This will list all branches, and the branch that is currently active will have an asterisk mark next to it (*).
Step 2: Move to Another Branch
You are not able to delete the branch you are on. Therefore, if you are on the branch that you want to delete, make sure to switch to another branch first:
git checkout main # Or ‘git switch main’
You can switch out the main with the branch that you want to switch to.
Step 3: Remove the Local Branch
Now, to remove the branch, you have to use the command below:
git branch -d branch-name
With branch-name being the name of the branch you want to delete.
In the case where the branch has unmerged changes, the deletion will not be permitted by Git. Instead, you can force delete it with.
git branch -D branch-name
Doing this will permanently delete the branch from your local repository.
How to Delete a Git Branch Remotely
Removing a branch in your local machine will not delete it from the remote repository, such as GitHub, GitLab, or Bitbucket. Follow the steps below to delete a remote branch.
Step 1: Checking Remote Branches
If you want to check which branch exists in the remote repository, use the command below:
git branch -r
This command will list all the remote branches.
Step 2: Delete Remote Branch
To delete a branch in a remote repository, enter the command below:
git push origin –delete branch-name
branch-name refers to the name of the branch that is going to be deleted.
Alternatively, you can use
git push origin: branch-name
Both commands will delete the chosen branch in the remote repository.
Step 3: Check If Deletion Was Successful
To check if the branch was deleted remotely, execute the command:
git branch -r
If the command no longer lists a particular branch, the deletion was successful.
Cleaning Up Stale Remote Branches
After deleting a remote branch, it might still show up in your local repository after running the command git branch -r. To clean up your local references, run the following command:
git fetch –prune
This command effectively cleans up obsolete remote-tracking branches that no longer exist on the remote server.
When Should You Delete a Git Branch?
Deleting branches will keep your repository clear and efficient. These are some of the scenarios when you should consider deleting a branch:
- You want to declutter your repository and keep it organized.
- The feature or bug fix is complete, and the branch is no longer needed.
- The branch has been merged into the main or developed successfully.
- You no longer need the branch because it was just for experimentation.
Best Practices for Managing Git Branches
Follow these best practices to maintain smooth collaboration and effective version control:
- Use meaningful branch names: Names like ‘feature-login’ or ‘bugfix-header’ assist in identifying the purpose of the branch.
- Regularly clean up old branches: Do not allow old branches to accumulate in your repository.
- Merge before deleting: Make sure anything that needs to be merged is done so before deleting a branch.
- Communicate with your team: In case you are part of a team, inform everyone before deleting branches that are shared.
Conclusion
Proper management of Git branches plays an important role in keeping your repository organized and performant. In this guide, we have addressed the issue, How do you delete a Git branch in local and remote settings? You can remove branches that are no longer needed from a Git repository without any difficulties by following well-organized steps.
For more of these tech guides and tutorials, make sure to check out Techaibro, your go-to tech guide online!