How to delete local and remote Git branches
How to delete local and remote Git branches
A branch is a part of the repository where we write new features, fix bugs, etc. For example, if three developers are working on a project, they can create their own branches and work on those branches because the branches are isolated, so everyone can work on their independent branches.
Branches can be:
- Local – only on your local computer.
- Remote – it is located remotely, such as in a GitHub repository
In fact, there is a third type of branch, a reference to a remote branch. When cleaning up, these branches should also be cleaned up. Today’s article will discuss various scenarios related to branch deletion. This article will show you how to delete local branches and remote branches on GitHub as well as some common mistakes when deleting branches.
Why you need to delete a branch
To make sure your Git repository isn't a bunch of old branches that are outdated and no longer used. You should regularly clean up branches, delete old branches or merge them into the main branch, so that the code repository will be tidy.
Delete Git local branch
Note that deleting the local branch does not delete the remote branch. Here is the command to delete a branch locally:
git branch -d "branch name"
The following command will also perform the same function, just with slightly different syntax.
git branch --delete <branch>
Note that -d
option is --delete
short for , which will only delete a branch if it is fully merged into its parent branch. If you have unmerged changes then it won't delete the branch and you will get an error. If you want to delete a branch, regardless of the merge status, you need to force delete the branch. You can force delete a local branch using the following command:
git branch -D <branchName>
Another point to remember is rebase/merge. If your branch is in the process of rebase/merge, you will see the error "Rebase/Merge in progress" and you will not be able to delete your branch. You can force the removal if you wish, or you can resolve the rebase/merge issues before trying again.
Delete Git remote branch
To delete a branch from the remote repository, use the following command:
git push origin -d "branch name"
In the above example, the remote branch dev-testing
was deleted. Both of the following commands will delete the remote branch:
git push <remote_name> --delete <branch_name>
If you are using a Git version earlier than 1.7.0, the following commands apply:
git push <remote_name> :<branch_name>
Note that executing git push origin –delete
will only delete your remote branch. The branch name is not mentioned at the end of the command, however, if you put the branch name at the end, it will delete it and push it to the remote at the same time.
What are tracking branches and how to delete them
When we branch from a remote check out
to a local branch, it automatically creates what is called a tracking branch. These are local branches that have a direct relationship to the remote branch. This means it exists in our local machine cache, but not in the remote repository.
If you git push origin :<branchname>
delete a remote branch using the command , references to it still exist in team members' local repositories. Now, you also need to delete the local reference. git remote prune origin
Remove references to branches that don't exist on the remote.
Another version of the same command is: git fetch <remote> --prune
This will delete all stale remote tracking branches. The abbreviation for this command is:git fetch <remote> -p
To delete a specific local remote tracking branch, you can use the following command: The git branch --delete --remotes <remote>/<branch>
abbreviation of this command is:gitbranch -dr <remote>/<branch>
Note that if you delete a remote branch from git push
the command line usingX
origin/X
git fetch -prune
git fetch –p
To confirm whether the remote tracking branch was deleted, you can run the following command:
git branch –remotes
It can be abbreviated as:
git branch –r
How to delete a branch on Github using the web console
- Navigate to the repository's home page.
- Above the file list, click Branch.
- Navigate to the branch you want to delete and click the delete icon
common problem
Unable to delete branch
Solution: You cannot delete a branch you are already on. You have to switch to another branch first and then delete the desired branch. See the example below:
In the above example, we switched to another dev-arsam
branch named and then we were able to delete test
the branch successfully.
I accidentally deleted a branch, can I restore it?
Solution: Yes, you can use git reflog
the command and find the commit at the top of the deleted branch SHA1
and just do that git checkout [sha]
. Once you've made that commit, you can simply git branch branchname<SHA>
recreate the branch from there.
git reflog
Command is used to record recent updates made to a branch. It allows return commits. After the history is rewritten, the reference log contains the history of previous branch commits and can be returned to a specific state if needed.
An example is provided below where dev-arsam
a branch named will be reverted.
How to automatically delete branches when merging back to master
You can set up the configuration so that your branch is master
automatically deleted as soon as it is merged into its parent branch e.g. branch:
- On GitHub, go to the repository's home page.
- Under your repository name, click Settings.
- Under "Pull Requests," select or deselect
Automatically delete head branches
.
I get an error when I delete a branch with the same name as a tag
If you try to delete tag
a branch with the same name as , you may get an error message. You will see branch-or-tag-name matches more than one
an error similar to .
If you want to specify the branch to be deleted instead tag
, try the following:
git push origin :refs/heads/branch-name
Likewise, if you want to specify a delete tag
instead of a branch, use the following command:
git push origin :refs/tags/tag-name
summary
In this article, we learned about the different methods of deleting branches in Git, and also answered common questions related to deleting branches in Git. I believe you can use it in your daily work and development.