Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git push after removing large file

Tags:

git

I accidentally commited log/test.log but have never pushed it. I have since done a git rm to get rid of it. But when I try to push, I'm still getting a huge amount of data attempting to be transferred. Shouldn't the git rm fix that problem. If not, how could I fix it?

like image 341
Daniel Avatar asked Feb 24 '10 15:02

Daniel


People also ask

How do I remove a large file from a git push?

If the large file was added in the most recent commit, you can just run: git rm --cached <filename> to remove the large file, then. git commit --amend -C HEAD to edit the commit.

Can't push to GitHub because of large file?

Locally delete large files. Commit the local deletes. Soft reset back X number of commits (for me it was 3): git reset --soft HEAD~3 . Then recommit all the changes together (AKA squash) git commit -m "New message for the combined commit"

How do I upload files bigger than 100mb to GitHub?

Github does support uploading and managing large files at once. Check out Git LFS. Using this you can manage/upload & control your project/repo having sizes larger than 100mb, noting that tha maximum repository size is less than equal to 10GB for free tier.


1 Answers

Don't revert the commit and then push because the huge file will still be carried around in the history.

Given that you haven't yet pushed it and that the commit you want to redo is the most recent, remove that commit from your history:

$ git reset HEAD^

This will return your index to the state it was in on the parent commit (HEAD^). Now you have a mulligan: add and commit the way you meant to the first time around.

If you've made other subsequent commits, you'll need to git rebase -i <commit> where <commit> is the SHA-1 of the bad commit's parent.

For example (and note the SHA-1 will be different in your repo)

$ git rebase -i 57d0b28

will drop you in an editor that resembles

pick 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar

# Rebase 57d0b28..121802a onto 57d0b28
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#

Replace pick with edit on the line with the heavy commit

edit 366eca1 This has a huge file
pick d975b30 delete foo
pick 121802a delete bar

Save and quit your editor to return to your shell, where you'll see a message of the form

Stopped at 366eca1... This has a huge file
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

From there, delete the offending file (--cached removes the file from the index only)

$ git rm --cached big-nasty-file
rm 'big-nasty-file'

amend the commit

$ git commit --amend

and finish the rebase

$ git rebase --continue
like image 193
Greg Bacon Avatar answered Sep 28 '22 03:09

Greg Bacon