Skip to content

Instantly share code, notes, and snippets.

@cranberyxl
Last active April 28, 2021 13:27
Show Gist options
  • Save cranberyxl/010752de01609e9b76c45f72d9c7024a to your computer and use it in GitHub Desktop.
Save cranberyxl/010752de01609e9b76c45f72d9c7024a to your computer and use it in GitHub Desktop.
Useful git bash scripts

List remote branches already merged to main

git fetch origin > /dev/null; git checkout origin/main; for branch in $(git branch -a --merged | grep remotes/origin | grep -v main | grep -v 'no branch'| sed -e 's/\s*remotes\///'); do echo "${branch:7}"; done; git checkout main

Delete remote branches already merged to main

git fetch origin > /dev/null; git checkout origin/main; for branch in $(git branch -a --merged | grep remotes/origin | grep -v main | grep -v 'no branch'| sed -e 's/\s*remotes\///'); do git push origin :${branch:7}"; done; git checkout main

List local branches already merged to main

git fetch origin > /dev/null; git checkout origin/main; for branch in $(git branch --merged | grep -v main | grep -v 'no branch'); do echo "$branch"; done; git checkout main

Delete local branches already merged to main

git fetch origin > /dev/null; git checkout origin/main; for branch in $(git branch --merged | grep -v main | grep -v 'no branch'); do git branch -D $branch; done; git checkout main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment