In today’s tutorial, we’re going to learn how to Delete Git Branch. Just like you can Create Git Branches, the technology also provides a way to remove them. This process is simple and only takes a few seconds.
When to Delete Git Branch?
Having different branches in your Git repo is a great way to work on new features and fixes while isolating the code from the main codebase. This allows you to keep your development process organized and streamlined, making it easier for you to track your progress and merge your changes into the main codebase when they’re ready.
If you’re working on a branch and finish your work, it’s recommended that you delete the Git branch. This keeps the main codebase clean and organized.
|Post Type:||GIT Tutorial|
|Applicable For:||Freshers & Experience|
|Get Updates:||Join Our Telegram Group|
Why Delete Git Branch?
Have you ever thought about why or when you should delete a branch in Git? It’s quite natural to get these questions in mind. Additionally, it would be helpful to know the logic behind deleting branches before actually proceeding with the process.
Let us take a scenario, If you’re working on a project and have an idea for a new feature, it’s straightforward to create a new branch, implement your idea, test it out, and merge it with the master. After that’s done, you can go ahead and delete the newly created feature branch from the repository. Keeping unused branches around doesn’t serve any purpose and can actually make things more complicated as your project progresses.
If you’re worried about keeping track of the history and being able to go back to features developed long ago, don’t worry. You can use “tags” in Git which will allow you to label a commit and easily access it later on if you need to. We have a whole chapter dedicated to tags that goes into more detail.
What Happens If I Delete Git Branch?
When we are deleting a branch in GIt, you don’t delete the commits themselves. which means the commits are still there and if we want to recover then we will be able to recover them.
When we are deleting a branch, it depends on whether the branch is merged or not. There are two possible scenarios, which is:
- Delete Git Branch with Merged Changes
- Delete Git Branch With Unmerged Changes
Delete Git Branch with Merged Changes
Suppose think about one scenario where the changes are merged to the main branch and at this point, if we are deleting a branch then nothing will change. Because the changes are already merged with the main branch.
Delete Git Branch With Unmerged Changes
Now we are going to explore another scenario where a branch has some changes and that is not merged with the main branch. If you look at the diagram you have added a few more commits but you have not merged the changes into the main branch.
If we delete the last branch, then our repo will look something like the below:
If you notice the above diagram, the commits are still there but you can not reach them without the hash of the last commit.
The commit has a reference to its parent. However, it does not have a reference to any children. That’s why you can only move backward from the commit that main points to. Consequently, you cannot reach the commits that exp formerly pointed to.
What happens if you delete a branch with unmerged changes by mistake? Are those lost forever? Not yet! There’s a way to get them back if you act fast. I’ll tell you more about that later.
Delete Git Branch
This section will help you understand how to delete branches from your local system and push them to the remote repository. We will also highlight what happens when you delete a branch from the remote repository.
How To Delete A Local Branch?
To delete a branch you have to follow the below steps:
- List down all the branches of a repository by running the below command: git branch -a
- If the branch has already been merged into your current branch, then you can delete it with git branch -d [branch_name].
Note: If you want to delete your local branch regardless of its merge status, you can use the -D flag. This is equivalent to the –delete –force command.
We’re currently on the “master” branch and trying to delete it through a command. Let’s see if it works by executing the command!
If you get an error when trying to delete a branch in Git, it’s probably because you’re currently working on that branch. To fix the issue, check out from the branch and then try deleting again. This should work smoothly. You can use this as a practice opportunity if you’d like.
We have deleted the branch on our local machine. But we can also try and delete a branch on the remote repository through our local machine.
How To Delete A Remote Branch?
We’re going to remove a remote branch, but first, let’s take a look at all the branches in the remote repository. Type and execute the following command:
git branch -a
From the above diagram, we can notice it is displaying all the remote branches. From those lists, we will try to delete those branches. In our example, we are going to delete the XXX branch. So to delete the remote branch execute the below command:
git push <remote_repo_name> –delete <branch_name>
How Can You Delete All Non-Merged Git Branches?
If you have multiple Git branches that have already been merged, you can delete them all at once instead of deleting them branch by branch.
Note: If you would like to merge changes from another branch, you can use the git merge command. This will take the changes from the other branch and integrate them into your current branch.
To get all the branches that are merged in the remote repository, use this command:
git branch –merged
To delete a local branch that has been merged into the master branch, use the following command:
git branch -d branch-name
Use the following command to delete it from the remote repository:
git push –delete origin branch-name
Remove All Local Branches, not on Remote
You can use the following bash command to remove all local branches, not on the remote repository.
git branch -r | egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) | xargs git branch -d
I thought most of our readers may not know what the meaning or action will be taken by the above command. So let us break the command into multiple sections for better understanding:
- git branch -r: Means first get all the remote branches.
- egrep -v -f /dev/fd/0 <(git branch -vv | grep origin): means get all the local branches that are not in the remote.
- xargs git branch -d: Finally we are deleting the branches using this command.
Delete All Your Local Git Branches Except Master
It’s always a good practice to remove your local git branches after you’re done with them, to free up space. You can simply run the following command to do so:
git branch | grep -v “master” | xargs git branch -D
To delete all non-master branches, we use the “grep -v master” command to search for them, then delete them using the “git branch -D” command.
You’ve learned how to delete a branch both locally and remotely through Git. In the next tutorial, we’ll talk about merging and creating pull requests. Keep practicing in the meantime so you’re ready for the next lesson.
Leave a Reply