简体   繁体   中英

Accidentally committed a large file to local git. Now I have to remove it to push to remote

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM