How to Delete a Git Branch Both Locally and Remotely
In most cases, it is simple to delete a Git branch.
1 | // delete branch locally |
Delete a Git Branch Locally
error: Cannot delete branch ‘xxxxx’ checked out at ‘’
Git won’t allow you to delete a Git branch you are currently working on. So you must make sure to checkout to a branch that you are NOT deleting. For this use the command:
1 | git checkout <branch-name> |
Here we will check out our main branch from my test branch.
Now in order to delete the test branch locally, we use the command :
Syntax
1 | git branch -d <branch-name> |
We will delete my test branch as an example.
Note: The -d option will delete the branch only if it has already been pushed and merged with the remote branch. If you want to forcefully delete a branch you will have to use the -D option instead. The -D flag is synonymous with –delete –force. This will forcefully delete the branch even if it hasn’t been pushed or merged with the remote. the full command is:
Syntax
1 | git branch -D <branch-name> |
With this, we can successfully delete a local branch.
Delete a Git Branch Remotely
You can’t use the git branch command to delete a remote branch. Instead, you have to use the git push command with the –delete flag, followed by the name of the branch that you want to delete. You also need to specify the remote name (origin in this case) after “git push”. The command is as follows:
Syntax
1 | git push <remote-name> --delete <branch-name> |
Here I will delete my test branch in my remote repository as shown below.
This command will delete the branch remotely. You can also use the shorthand:
Syntax
1 | git push <remote-name> :<branch-name> |
As you can see my remote branch is no more in my GitHub repo:
With this, we have successfully deleted our remote branch. A common error faced by many in this step is:
error: unable to push to unqualified destination: remoteBranchName The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to ‘git@repository_name’
This means that someone has already deleted the branch that you want to delete. If this happens you can use the following command to synchronize your branch list in the local environment:
Syntax
1 | git fetch -p |
The -p flag here means “prune”. After fetching the branches which no longer exist remotely will be deleted in your local working environment.
Local Deletion vs Remote Deletion
Action | Local Deletion | Remote Deletion |
---|---|---|
Command | git branch -d branch-name |
git push origin --delete branch-name |
Force Deletion | git branch -D branch-name |
git push origin :branch-name |
Purpose | Removes branch from local repository | Removes branch from remote repository |
Safety | -d flag is safer, prevents unintentional deletion |
Must ensure the branch is not needed anymore |
Conclusion
Removing the branches which are not important keeps the repository organized, improves collaboration, and makes sure a better development workflow. However, it’s important to exercise caution when deleting branches, especially remote branches, as it may impact ongoing work by other team members. Therefore, always communicate with your team and verify that the branch is no longer required before proceeding with deletion.
FAQs on Delete a Git Branch Locally and Remotely
How do I delete a Git branch locally?
To delete a Git branch locally, you can use the following command:
git branch -d <branch_name>
Replace ‘<branch_name>’ with the name of the branch you want to delete. This command will remove the branch from your local repository.
How do I delete a remote Git branch?
To delete a remote Git branch, use the following command:
git push <remote_name> –delete <branch_name>
Replace ‘<remote_name >’ with the name of the remote repository, and ‘<branch_name>’ with the name of the branch you want to delete.
Can I recover a deleted branch?
Yes, if you accidentally delete a branch, you can recover it if you act quickly. Locally deleted branches may still be recoverable using
**git reflog**
or**git fsck**
, but it depends on how long ago they were deleted. For remotely deleted branches, you may be able to restore them using version control hosting platforms (GitHub).
Wrapping Up
Bear in mind that to completely remove a Git branch from your project, you need to use the git push origin command.
That’s because you’ve pushed the branch already. So, running the git branch -d command would only remove the branch locally.
And if you have issues working with Git, I suggest you switch your terminal to Git bash. That’s because it has syntax highlighting for everything – making it easier to work with Git.