Created
February 11, 2019 04:52
-
-
Save trungungtoan-agilityio/f864a639df2a73d2881b2a8a3a00028a to your computer and use it in GitHub Desktop.
Deleting Old Local Branches
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | xargs git branch -d |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
git remote prune origin
Read more about git remote prune here
Deletes all stale remote-tracking branches under . These stale branches have already been removed from the remote repository referenced by , but are still locally available in "remotes/".
git branch -vv will list all local branches along with some additional information such as their related upstream/remote branch and latest commit message
Next, we pipe the output from git branch -vv into grep 'origin/.: gone]'. This filters our list down to only lines that match the regex origin/.: gone] leaving us with
Random, useful grep tip : Invert Match
I just came across grep's -v (short for --invert-match) grep -v 'some pattern' that filters lines down to those not matching any of the specified patterns.
Piping that into awk '{print $1}' cleans up our output so we end up with a branch name per line.
$1: feature/some-old-feature
$2: cba2191
$3: [origin/feature/some-old-feature:
$4: gone]
$5: First
$6: words
$7: in_commit_message...
...
4a) Delete the branches!
Next, we pipe our filtered down, cleaned up git branches list into git branch -d (short for --delete) and say our final goodbyes.
git branch -d branch01 branch02 branch03 # ...
4b) Review/edit list before deleting
If you want to review and/or edit the list of branches before deleting them. One way to accomplish this is to pipe the branch names into your clipboard via pbcopy, paste them into your favorite editor, and then pipe them into git branch -d yourself. Example:
Pipe list into your clipboard via git branch -vv | grep 'origin/.*: gone]' | awk '{print $1}' | pbcopy
Paste into editor. Review. Edit as needed.
Copy the edited list back into your clipboard.
Pipe clipboard into commmand via: pbpaste | xargs git branch -d