Skip to content

Instantly share code, notes, and snippets.

@TSMMark
Last active February 20, 2023 05:41
Show Gist options
  • Star 55 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save TSMMark/99d7153c97b0c0762bd6105f0e39f8e2 to your computer and use it in GitHub Desktop.
Save TSMMark/99d7153c97b0c0762bd6105f0e39f8e2 to your computer and use it in GitHub Desktop.
Delete local branches that don't exist on remote/origin !USE AT YOUR OWN RISK!
git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print $1 }' | xargs -r -n 1 git branch -D
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
# See: http://stackoverflow.com/a/28020809/2696867
mark$ git fetch --all -p; git branch -vv | grep ": gone]" | awk '{ print $1 }' | xargs -r -n 1 git branch -D
Fetching origin
From github.com:tsmmark/vydia
x [deleted] (none) -> origin/UI/description-errors
x [deleted] (none) -> origin/UI/even-more-style-tweaks
x [deleted] (none) -> origin/UI/make-merge-tags-look-more-disabled
x [deleted] (none) -> origin/UI/more-style-tweaks
x [deleted] (none) -> origin/UI/move-connect-socials
x [deleted] (none) -> origin/UI/remove-redundant-social-input-fields
x [deleted] (none) -> origin/UI/unconnected-vevo-disabled-title-text
* [new branch] ui/description-errors -> origin/ui/description-errors
* [new branch] ui/even-more-style-tweaks -> origin/ui/even-more-style-tweaks
* [new branch] ui/make-merge-tags-look-more-disabled -> origin/ui/make-merge-tags-look-more-disabled
* [new branch] ui/more-style-tweaks -> origin/ui/more-style-tweaks
* [new branch] ui/move-connect-socials -> origin/ui/move-connect-socials
* [new branch] ui/remove-redundant-social-input-fields -> origin/ui/remove-redundant-social-input-fields
* [new branch] ui/unconnected-vevo-disabled-title-text -> origin/ui/unconnected-vevo-disabled-title-text
Fetching staging
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for ec2-54-213-156-142.us-west-2.compute.amazonaws.com has changed,
and the key for the corresponding IP address 54.213.156.142
is unchanged. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
Offending key for IP in /Users/mark/.ssh/known_hosts:16
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
REDACTED
Please contact your system administrator.
Add correct host key in /Users/mark/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/mark/.ssh/known_hosts:6
RSA host key for ec2-54-213-156-142.us-west-2.compute.amazonaws.com has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
error: Could not fetch staging
Fetching production
fatal: 'src/vydia.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
error: Could not fetch production
Deleted branch UI/445-email-on-guest-upload-complete (was d617b3a).
Deleted branch UI/confirmation-required-indicator-cc-form (was f32a36a).
Deleted branch UI/user-detail-page (was b96da79).
Deleted branch bug-more-fixes-for-advanced-rights (was 773a1a3).
Deleted branch bug/a2v/artist-name-overflow (was 0fed2b1).
Deleted branch bug/billing-date-in-past (was 321175e).
Deleted branch bug/fix-maintenance-mode (was b831d88).
Deleted branch bug/fix-renewal-presets-always-reverting-to-default (was ee2614d).
Deleted branch change-media-artist-bug (was 75da3f9).
Deleted branch feature/introduce-resque-scheduler-web (was 9e350cf).
Deleted branch feature/move_release_message_to_another_release (was cf6c739).
Deleted branch feature/youtube-import-routes (was 1fcbc56).
Deleted branch fork/redirect-after-payment-choose-service (was 24afb4f).
Deleted branch initial-notifications (was 8f05b96).
Deleted branch internal/add-backtrace-to-payment-revert-report (was f83e92d).
Deleted branch internal/random-failure-in-discounts-model-tests (was 5c9cc34).
Deleted branch internal/test-some-deployments (was 2d56df6).
Deleted branch poltergeist-integration-testing (was 04e02c1).
Deleted branch pr/842 (was ef37f1e).
Deleted branch spike/publisher-files-presenter (was 26cbe42).
Deleted branch staging (was ed83c49).
@virtualprodigy
Copy link

solid!

@sebas-alem-f
Copy link

Useful! Thanks!

@Warafux
Copy link

Warafux commented Feb 18, 2021

👌 Thanks!

@hexpunk
Copy link

hexpunk commented Jun 1, 2021

Thank you! I use this all the time.

One small suggestion, though. If there are no matching branches to delete, git branch -D still runs and prints the message fatal: branch name required.

I'd add -r to the xargs invocation, which is short for --no-run-if-empty. That way it doesn't try to run git branch -D if there are no branches to delete.

@TSMMark
Copy link
Author

TSMMark commented Jun 1, 2021

Thanks @jayandcatchfire! I updated the gist but didn't test running it. Does it look right to you?

@hexpunk
Copy link

hexpunk commented Jun 1, 2021

Yes! Works for me. Now it just says Fetching origin and drops back to the prompt without any errors. Thanks!

@dep
Copy link

dep commented Jan 12, 2022

xargs: illegal option -- r
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]
             [-L number] [-n number [-x]] [-P maxprocs] [-s size]
             [utility [argument ...]]

@TSMMark
Copy link
Author

TSMMark commented Jan 13, 2022

@dep maybe on osx should be -I instead https://unix.stackexchange.com/a/285170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment