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
.
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