Skip to content

Instantly share code, notes, and snippets.

@debakarr
Created April 21, 2023 04:44
Show Gist options
  • Save debakarr/ebce4bf1a6abda8cacb5bdd350fa27be to your computer and use it in GitHub Desktop.
Save debakarr/ebce4bf1a6abda8cacb5bdd350fa27be to your computer and use it in GitHub Desktop.
Delete branch which are missing upstream

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.

  1. First, fetch the latest information from the remote repository:

git fetch --all --prune

  1. List all branches that are missing upstream:

git branch -vv | grep ': gone]' | awk '{print $1}'

  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 the git branch -d command, which deletes them. The -r option tells xargs to not run the command if there are no input items.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment