Removing a Big File from Git
If you accidentally commit a big file:
git reset HEAD~1And that’s it!
What if it wasn’t the last commit
Run rebase -i --root. This will open a file.
Find the bad commit and replace the word pick with the word edit (or just the letter e).
Save and quit the file.
Fix your mistake (e.g. compress the file).
Run git add <the file>.
Run git rebase --continue.
What if you want to save space locally
If you want to remove the object from your local .git to save space:
git reflog expire --expire=now --all
git gc --prune=nowWhat if you want to save space locally but like your reflog
If you don’t want to expire your entire reflog, edit these two files:
.git/logs/refs/HEAD
.git/logs/refs/heads/mainIn each of them, delete 2 lines:
- the line containing the bad commit and
- the line containing the reset (called
reset: moving to HEAD~1) The should be the last two lines in the file. And then:
git gc --prune=nowIf you’re not working on main, use the appropriate reflog instead.
(You can also just grep from the hash in .git/logs.)
If you re-based, delete all the children of the bad commit.
What if you’re a hoarder like me
Note that there may be other unreachable commits. If you care about this, you should list them before you edit the reflog:
git fsck --unreachableThen, assign temporary refs to those commits and check to make sure the output is clean. After you remove the bad commit, you can also remove the temporary refs.