Wednesday, November 11, 2020

git - make the history never exist

I committed something to my git repo that I didn't mean to. Always, I mean always create a .gitignore first and add everything that should be excluded. Modifying the history is super scary and dare I say, dangerous. I did a quick google search and ran across this which seems to have the best steps for doing the desired surgery.

First run this to check the size:

git count-objects -vH

Just in case that link stops working I've pasted there relevant bits here:

# Make a fresh clone of YOUR_REPO
git clone YOUR_REPO

# Create tracking branches of all branches
for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done

# Remove DIRECTORY_NAME from all commits, then remove the refs to the old commits
# (repeat these two commands for as many directories that you want to remove)
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch DIRECTORY_NAME/' --prune-empty --tag-name-filter cat -- --all
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

# Ensure all old refs are fully removed
rm -Rf .git/logs .git/refs/original

# Perform a garbage collection to remove commits with no refs
git gc --prune=all --aggressive

# Force push all branches to overwrite their history
# (use with caution!)
git push origin --all --force
git push origin --tags --force

Lastly run this command to check the size again:

git count-objects -vH

It appears to have worked. I did have to reclone my repository which seemed a little odd but it worked.

No comments:

Post a Comment