Before we go into how to delete a local and remote Git branch, first let's understand - what is Git?
Git is a popular open-source distributed version control system that can handle a wide range of projects, from small to large, quickly and efficiently. It stands out since it is user-friendly and simple to understand.
In this tutorial, you will delete a local and remote Git branch. We will also address a few FAQs on how to delete a local and remote Git branch.
Delete a Local Git Branch
You can list, create, rename, and delete branches with the git branch command.
git branch command with the
-d (--delete) option followed by the branch name to delete a local Git branch:
git branch -d branch_name
Deleted branch branch_name (was 11f4gt1).
You'll see the following error notice if you try to delete a branch with unmerged changes:
error: The branch 'branch_name' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch_name'.
As you can see from the statement above, the
-D option, which is a shortcut for
--delete --force is used to force the deletion of a branch.
git branch -D branch_name
Please keep in mind that deleting an unmerged branch will result in the loss of all changes made to that branch.
git branch --no-merged command to see all the branches with unmerged changes.
You'll see the following notice if you try to remove the current branch:
error: Cannot delete branch 'branch_name' checked out at '/path/to/repository'
You won't be able to delete the branch you're on right now. Switch to a different branch first, then delete the
git checkout master
git branch -d branch_name
Delete a Remote Git Branch
Local and remote branches are distinct objects in Git. The remote branch is not deleted when a local branch is deleted.
git push command with the
(--delete) option to delete a remote branch:
git push remote_name --delete branch_name
In most cases,
remote_name is origin:
git push origin --delete branch_name
- [deleted] branch_name
There is another command to delete a remote branch that is more difficult to remember:
git push origin remote_name :branch_name
You will obtain the following error message if you are working on a project with a group of people and try to delete a remote branch that has already been removed by someone else:
error: unable to push to unqualified destination: branch_name 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 '[email protected]:/my_repo'
You'll need to synchronize your branch list with:
git fetch -p
Before fetching, the
-p option instructs Git to erase any remote-tracking references that no longer exist on the remote repository.
FAQs to Delete a Local and Remote Git Branch
What if I want to force delete a local branch?
If you want to force delete a local branch that hasn't been merged, you can use the command
git branch -D <branch_name>. This will delete the branch forcibly.
What happens if I delete a branch that hasn't been merged?
If you delete an unmerged branch, all commits and changes unique to that branch will be lost permanently. Make sure you don't have any important work on the branch before deletion.
What's the difference between deleting a remote branch and deleting a local branch?
Deleting a remote branch removes the branch from the remote repository, while deleting a local branch removes it from your local repository only. However, both actions will make the branch's commits and changes inaccessible if they haven't been merged.
How can I see a list of all local branches before deleting?
You can use the command
git branch to see a list of all local branches in your repository. This will help you identify the branch you want to delete.
Is it possible to delete a remote Git branch?
Yes, you can delete a remote Git branch using the command
git push origin --delete <branch_name>. This will remove the specified branch from the remote repository.
Can I delete multiple local branches at once?
Yes, you can delete multiple local branches together using the command
git branch -d <branch1> <branch2> .... Just list the branch names separated by spaces.
Is there any way to delete multiple remote branches simultaneously?
Yes, you can delete multiple remote branches at once using the command
git push origin --delete <branch1> <branch2> .... This will delete all specified branches from the remote repository.
We've already shown you how to delete Git branches on the local and remote levels. Branches are essentially a snapshot of your changes that have a short life span. The branch is no longer needed and should be removed after it has been merged into the master (or another major branch).
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.