Pangamma

Git command to clean up local branches that no longer exist on remote. PRUNE

Jun 17th, 2021 (edited)
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.73 KB | None | 0 0
  1. ## Delete all local (and already merged/deleted) branches not on remote.
  2. git fetch -p && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
  3.  
  4. # Prune any branches that no longer exist on remote from our "remotes" list.
  5. git fetch -p
  6.  
  7. # Show sha1 and commit subject line for each head, along with relationship to upstream branch (if any).
  8. # Print the name of the upstream branch, as well.
  9. git branch -vv
  10.  
  11. # Filter and only show branches that have been removed on the remote.
  12. grep ': gone]'
  13.  
  14. # Print first column only.
  15. awk '{print $1}'
  16.  
  17. # build and execute command lines from standard input
  18. xargs
  19.  
  20. # Delete local branch that has same name as one of the branches we isolated in previous steps.
  21. git branch -D
  22.  
Add Comment
Please, Sign In to add comment