To delete all branches that are missing upstream, you can use the following Git commands. This will first list all branches that are missing upstream, and then delete them one by one.
- First, fetch the latest information from the remote repository:
git fetch --all --prune
- List all branches that are missing upstream:
git branch -vv | grep ': gone]' | awk '{print $1}'
- Delete the branches that are missing upstream:
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs -r git branch -D
Here's a breakdown of the command:
git branch -vv
: List all branches with detailed information including the tracking branch.grep ': gone]'
: Filter the output to show only branches with the "gone" status, indicating they are missing upstream.awk '{print $1}'
: Extract the branch names from the output.xargs -r git branch -D
: Pass the branch names to thegit branch -d
command, which deletes them. The-r
option tellsxargs
to not run the command if there are no input items.