How to Delete Both Local and Remote Branches in Git
- How to Delete a Local Branch in Git
- How to Delete a Remote Branch in Git
- How to Delete Remote-Tracking Branch
- The git checkout and git branch Commands
- The git fetch Command Working with Remote Branches
After finishing the work on a branch and merging it into the main branch, it is recommended to delete it. Local and remote branches are entirely separate objects in Git. Deleting one would not remove the other, even if there is a tracking connection between them. So, if you need to delete both local and remote branches, delete them separately. Let’s have a look at how to do it!
How to Delete a Local Branch in Git¶
Git does not allow deleting a branch you are currently on, that's why you should, first of all, check out to another branch.
Checkout from the current branch¶
For checking out from the current branch, run the git checkout command:
git checkout <branch-name>
Delete a branch¶
If your current branch is merged with the branch which you want to delete, then -d option will delete it, otherwise, you need to use -D option to force delete the branch.
git branch -d <branch-name>
If the branch has a reflog, it will be deleted, as well.
The -f or --force flag in combination with -d (or --delete), allows deleting the branch containing unmerged changes.
How to Delete a Remote Branch in Git¶
Here are the steps to take for deleting a remote branch.
Deleting remote branch¶
To delete a remote branch use the git push command with the --delete flag (suppose, the name of remote is origin, which is by default):
git push origin --delete <branch-name>
If you get an error, probably, it means someone else has already deleted the branch:
error: unable to delete <branch-name>: remote ref does not exist error: failed to push some refs to 'git@repository_name'
Synchronizing branch list¶
Synchronize your branch list with git fetch:
git fetch -p
The -p option deletes remote-tracking branches that no longer exist on the remote branch.
How to Delete Remote-Tracking Branch¶
If you work with remote-tracking branches, then to find and delete them, you must run the git branch command with the --remote or -r attributes.
git branch --delete --remotes <remote>/<branch>
git branch -d -r <remote>/<remote-branch-name>
To delete multiple obsolete tracking branches run the git fetch command with the --prune option:
git fetch <remote> --prune
git fetch <remote> -p
The git checkout and git branch Commands¶
The git checkout command is used for switching branches or restoring working tree files. It operates on files, commits, and branches. The git checkout command allows switching between multiple features in just a single repository. It operates with the git branch command. It updates the files in the working directory to match the version stored in that branch instructing Git to record all the commits.
The git fetch Command Working with Remote Branches¶
Git stores the local and remote commits and separates through the use of branch references. So as to see the list of the local branch references, you should run the git branch command. To see the remote branches, you should use the -r flag with git branch. You can examine the remote branches with the git checkout and git log commands and after approving the changes of the remote branch, run git merge to merge it into the local branch.