Skip to content

Instantly share code, notes, and snippets.

@raypereda
Last active March 8, 2022 18:50
Show Gist options
  • Save raypereda/5138604 to your computer and use it in GitHub Desktop.
Save raypereda/5138604 to your computer and use it in GitHub Desktop.
complex git commands for managing branches
# To delete all branches on origin that have already been merged into master:
git fetch origin && git remote show origin | grep tracked | grep -v master | awk '{print $1}' | xargs -I '{}' bash -c 'if [ ! "$(git cherry origin/master origin/{})" ]; then git push origin :{}; fi'
# To show the authors of unmerged branches:
git fetch origin; git branch -r | grep origin | xargs -I {} bash -c 'if [[ ! `git cherry HEAD {}` ]]; then echo "{} -- $(git show --format=%an {} | head -n 1)"; fi'
# If you think you lost a commit somehow and it's not stashed and not in a branch,
# it might be located within a dangling commit.
# Use the following one-liner to dump all of your dangling commits to a file:
$ git fsck | awk '{print $3}' | xargs -I '{}' git show {} >> blob_contents
# This generates the file 'blob_contents' which contains all git dangling objects from your repository.
# You can now grep or search with a text editor for the changes that you lost.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment