Is there a simple way to delete all tracking branches whose remote equivalent no longer exists?
Example:
Branches (local and remote)
Locally, I only have a master branch. Now I need to work on bug-fix-a, so I check it out, work on it, and push changes to the remote. Next I do the same with bug-fix-b.
Branches (local and remote)
Now I have local branches master, bug-fix-a, bug-fix-b. The Master branch maintainer will merge my changes into master and delete all branches he has already merged.
So the current state is now:
Branches (local and remote)
Now I would like to call some command to delete branches (in this case bug-fix-a, bug-fix-b), which are no longer represented in the remote repository.
It would be something like the existing command git remote prune origin
, but more like git local prune origin
.
Remove All Local Branches not on Remote First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.
Steps to delete remote Git branchesIssue the git push origin –delete branch-name command, or use the vendor's online UI to perform a branch deletion. After the remote branch is deleted, then delete the remote tracking branch with the git fetch origin –prune command.
It could be either your local branch or a remote branch available on your Git server repository. Visual Studio 2017 provides you easy access to both. To delete a local branch, right click on it and select Delete from the context menu.
Deleting remote branches To delete a remote branch, you can't use the git branch command. Instead, use the git push command with --delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name ( origin in this case) after git push .
git remote prune origin
prunes tracking branches not on the remote.
git branch --merged
lists branches that have been merged into the current branch.
xargs git branch -d
deletes branches listed on standard input.
Be careful deleting branches listed by git branch --merged
. The list could include master
or other branches you'd prefer not to delete.
To give yourself the opportunity to edit the list before deleting branches, you could do the following in one line:
git branch --merged >/tmp/merged-branches && \ vi /tmp/merged-branches && xargs git branch -d </tmp/merged-branches
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With