Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

git - How to remove a too large file in a commit when my branch is ahead of master by 5 commits

I've been stuck all day on this issue, looking for an answer here :( ...

Context

I'm working alone on a project and I used github until now to save my work other than on my computer. Unfortunately, I added a very large file to the local repository : 300mb (which exceed Github's limit).

What I did

I will try to make an history of what I made :

  1. I (dumbly) added everything to the index :

    git add *
    
  2. I committed changes :

    git commit -m "Blablabla"
    
  3. I tried to push to origin master

    git push origin master 
    

    It took a while, so I just CTRL+C, and repeated step 2 and 3 four times, until I realised that a file was too large to be pushed to github.

  4. I made the terrible mistake to delete my large file (I don't remember if I did a git rm or a simple rm)

  5. I followed the instructions on (https://help.github.com/articles/remove-sensitive-data)

  6. When I try to git filter branch, I get the following error : "Cannot rewrite branches: You have unstaged changes."

Thanks in advance !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A simple solution I used:

  1. Do git reset HEAD^ for as many commits you want to undo, it will keep your changes and your actual state of your files, just flushing the commits of them.

  2. Once the commits are undone, you can then think about how to re-commit your files in a better way, e.g.: removing/ignoring the huge files and then adding what you want and then committing again. Or use Git LFS to track those huge files.


Edit: this answer is also acceptable if for instance your commits needed authentication (e.g.: username and email) and that you need to add the proper credentials after having commited. You can undo things the same way.

Question: would someone have a way to just cherrypick the commit that is bad and change it directly? I'm asking especially in the case of someone who would just need to re-authenthify his commits like in here, but in a case where the files needs not to be changed. Only commits to authentify.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...