1

I accidentally committed a large file to my local git. The file is too big to be added to the remote repository which makes it impossible to push my code at the moment. The problem is, that I didn't notice how big the file was getting for quite a while so I happily added some commits on top of it. Now, 4-5 commits later I need to get rid of this file in the previous commits so that I can push my code to the remote repository.

Naturally, I am very scared to delete half my code :-D

Is there any risk free way to achieve this?

Xen_mar
  • 8,330
  • 11
  • 51
  • 74
  • 1
    Does _that_ commit only contain _that_ big file? – Enlico Feb 22 '20 at 19:57
  • 3
    Does this answer your question? [How to remove/delete a large file from commit history in Git repository?](https://stackoverflow.com/questions/2100907/how-to-remove-delete-a-large-file-from-commit-history-in-git-repository) – Calum Halpin Feb 22 '20 at 19:59
  • No, the entire project. – Xen_mar Feb 22 '20 at 19:59
  • @Xen_mar, I think you have misunderstood the question. – Enlico Feb 22 '20 at 20:00
  • Ok, so that particular commit in which I added the large file? No, I usually only push code every 2-3 days. So there were around 20-30 files changed in that one commit. – Xen_mar Feb 22 '20 at 20:05

3 Answers3

3

What I would do is

git rebase -i HEAD~N // where N is the number of commit you have since including
// the one where the big file added
// then deleting the first "pick" and write an "e" to edit and saving it
// then deleting the huge file
git add -A
git commit -amend // save and exit
git rebase --continue

Also if you fear to screw things up what I would do is (not necessary best practice) creating a save branch first by git checkout -b saving so if you would screw up the thing badly you could restore it from this one.

Eraklon
  • 4,206
  • 2
  • 13
  • 29
1

If you committed only that file, as in

# nothing is staged
git add bigfile
git commit -m 'big file committed'
# other work and commits

I think you can simply

git rebase -i HEAD~x

where x is the number of commits from that commit to the last one, then change pick to drop for the incriminated commit (which should be the topmost in the list), save and exit.

If that commit changed other files, you can change pick to edit, and git will allow you to change the commit. You will then be able to stage/unstage files appropriately before committing.

Enlico
  • 23,259
  • 6
  • 48
  • 102
0

What I can suggest you to do is to cancel your last accidentally commit by this

git reset HEAD~1

And you can commit without your large file