Investigate affected branches and tags
Find the first commit to the branch you want to nuke, perhaps using git log --all --decorate --graph --oneline
or using git reflog
. Let's say that first commit is 666bad
.
Use the tips mentioned by PyRulez to help find any tags or branches that may be contaminated with this branch.
git branch -a --contains 666bad
git tag --contains 666bad
If any tags are associated with the branch, delete them locally and remotely.
git tag -d <tag name 1> <tag name 2> ... # remove tags locally
git push --delete origin <tag name 1> <tag name 2> ... # remove tags remotely
Clean up your local and remote branches
Checkout the branch you want to delete and use git filter-branch
to reset all commits on the branch to the parent of the branch's first commit:
git checkout evilbranch
git filter-branch --force --index-filter `git reset --hard 666bad^' \
--prune-empty 666bad..HEAD
This will delete the commits as it goes, because they are empty.
Force-push the results to your origin
git push origin --force
Clean up your reflog
and prune dangling commits
git reflog expire --expire=now --all
git gc --prune=now
Clean up team member branches
Make sure anyone else that has checked out the branch delete the tags and the branch on their machine, and then that they expire their reflog and garbage collect.
git tag -d <tag name 1> <tag name 2> ...
git branch -D evilbranch
git checkout master
git reflog expire --expire=now
git gc --prune=now
A final note
If your origin is GitHub and your branch has any Pull Requests on it, you must contact GitHub support and request that they prune your Pull Requests. You should also contact them to remove an cached views.