Git is a powerful and widely-used version control system that allows developers to track changes in their codebase and collaborate on projects with ease. One of the key features of Git is its ability to create and manage branches, which allows developers to work on different versions of the codebase simultaneously. In this article, we will discuss how to delete branch in Git like a pro using CLI, and other best practices for branch management.
What are branches in Git?
A Git branch is a separate line of development within a repository. It allows multiple developers to work on different versions of the codebase simultaneously without affecting the main branch. When a branch is no longer needed, it can be deleted to keep the repository clean and organized. Git branches are implemented in a lightweight manner, unlike other version control systems (VCS). Instead of copying files between directories, a branch in Git is simply a reference to a commit. This means that a branch serves as a pointer to the latest commit in a series of commits.
The git branch command is a powerful tool that enables developers to manage branches in a Git repository. It allows developers to create new branches, list existing branches, rename existing branches, and delete branches that are no longer needed. While git branch is a useful command, it is not capable of switching between branches or merging a forked history. To perform these tasks, git checkout and git merge commands can be used.
$ git branch <branch-name>
The above command in CLI creates a new branch called ＜branch-name＞. Remember that the new branch is not checked out with this command.
$ git branch
This lists all the branches. Adding -a to this will list all remote branches.
When to Delete Your Git Branches?
There are several cases when one needs to delete branches in Git:
- The branch is no longer needed: This could be because the feature or bug fix that the branch was created for has been completed and merged into the main branch.
- The branch is outdated: If the branch was created a long time ago and is no longer relevant to the current development, it may be best to delete it to avoid confusion.
- The branch is causing conflicts: If a branch is causing conflicts with other branches or the main branch, it may be best to delete it to resolve the conflicts.
How to Delete Branches in Git?
Now that we understand how branches in Git works, let's see how to delete local git branches:
1. Deleting Git Branches Locally
Deleting a branch in Git does not erase the commits themselves. In fact, the commits still exist and can potentially be recovered. The outcome of deleting a branch is contingent on whether the branch was previously merged or not. When a branch has been fully merged, it is considered "safe" to delete. This means that all of the commits on the branch have been incorporated into the main branch and are no longer needed.
In this case, you can use the command to delete the branch:
$ git branch -d branch-name
On the other hand, if a branch has not been fully merged, you should be cautious when deleting it. This is because the commits on the branch may contain changes that have not been backed up and can be permanently lost if the branch is deleted. In this case, you can use the command:
$ git branch -D branch_name
to forcefully delete the branch, but be aware that this action can permanently delete commits and changes that have not been backed up. Still, if you act quickly you can recover them. That will be discussed later. It is important to note that you can use:
$ git branch -vv
to see the branches that are merged or not and you can use $ git log to see all the commits made in the branch before deletion.
2. Deleting All Local Branches
You can use the git command along with grep to delete multiple branches that match a certain pattern. This can be useful when you need to delete a group of branches that share a similar name or follow a certain naming convention. For instance, the following command uses grep and xargs to delete all branches that have been merged except the current branch:
$ git branch -D $(git branch --merged | grep -v \* | xargs)
3. Deleting Git Branches Remotely
The branch delete commands mentioned yet are only for the local git repository. Now let’s see how to delete git branches remotely. To delete a remote branch in Git, you can use the command:
git push origin --delete branch_name
This command will delete the specified branch from the remote repository, but it will not delete the local copy of the branch.
$ git push <remote-name> --delete <branch-name>