Skip to content

Instantly share code, notes, and snippets.

@dennislo
Created January 12, 2026 16:51
Show Gist options
  • Select an option

  • Save dennislo/3e830135e9920b5e737bc0b1791f829d to your computer and use it in GitHub Desktop.

Select an option

Save dennislo/3e830135e9920b5e737bc0b1791f829d to your computer and use it in GitHub Desktop.
Useful GIT commands

Useful git commands

Bulk delete branches

List all branches sorted by commit date

git branch -r --sort=-committerdate

Write listed branches sorted by commit date to a file

git branch -r --sort=-committerdate > branches.txt


Issue

The number of merged, non-removed branches in our repository grew significantly. The time to cleanup has come. How to make it quickly?

Doing a hundred “click-delete-and-confirm” manual sequences in Web UI is a bad idea, let’s find a better one.

Solution

Notice: pay attention to the name of your remote and change it if needed.

  1. Fetch all changes from the repository, cleaning up stale remote references at the same time.

git fetch -p

  1. Checkout the branch you consider “if it’s merged to this branch, it can be removed”. For us, it was develop. Update your local repository with most recent changes.
git checkout develop
git merge --ff-only origin/develop
  1. Create a list of branches to be removed, filtering out all release branches, master and develop. Dump it to a file.
git branch -r --merged \
  | grep -v '\*\|master\|release\|develop' \
  | sed 's/origin\///' > merged_branches.txt
  1. Check the candidates in merged_branches.txt if they’re all safe to be removed.

  2. Remove ‘em!

cat merged_branches.txt | xargs -n 1 git push --delete origin

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