Github won't forget deleted items

天大地大妈咪最大 提交于 2021-02-19 07:33:05

问题


I am having a very simple problem with git that I just can't figure out. I originally tried to add a folder containing some scripts using:

git init
git add folder
git commit
git push origin master

However I didn't realize I left something in that folder which is way too large to push to github, so of course it failed.

So I deleted that item out of the folder and tried again. But to my amazement, I got the SAME error, with git thinking I still had that large item. So I went in, and the item was somehow back in the directory!!

I have re-tried this over and over, trying to remove, reset, everything I can think of. No matter what I do, git somehow adds this large item back into the folder, and tries to push it along with everything else.

So what am I doing wrong? How on earth do I get git to forget about this large item?!


回答1:


You've said everything in veyr general words, so I'll be mostly guessing.

First, if you realized the problem right after adding&committing the large file in a folder, and if you did not make any more commits then git commit --amend is your friend. git rm that file from that folder, commit that deletion with --amend option and the commit-with-large-thing-in-a-folder will get corrected/rewritten and the large file will be "permanently" removed from that commit. The bad news is that --amend works only on the very latest commit. You cannot --amend any older commit that the most-recent:

git add folder
git commit            ## oops!!
git rm folder\hugefile.zip
git commit --amend    ## uff

Otherwise, if you made some commits after that bad one, review those commits. If all of them have low or no value or are easily redoable, then the best way would be to git reset to trash/undo them all, and then --amending the commit with big file as above, and then redoing the extra commits.

git add folder
git commit -m AAA            ## oops, but I didn't notice yet
git add foo\bar
git commit -m BBB
git rm baz\boom
git commit -m CCC
# uh-oh, I just noticed the big file..
git log       # find the commit hash of 'AAA'
git reset --hard  THATCOMMITHASH
git rm folder\hugefile.zip
git commit --amend          # uff.. but BBB and CCC evaporated!
git add foo\bar
git commit -m BBB           # redo BBB
git rm baz\boom
git commit -m CCC           # redo CCC

(if you're smart, you'd write down the commit hashes of BBB and CCC from the log too, and instead of redoing them manually, you could also git cherrypick them even after reset, but I won't delve into that now)

Of course, that's infeasible for large files. And of course doing git reset --hard will trash all your uncomitted local changes, too, if you had any.

If neither of those above are OK for you, then, well, you will need to try harder. For example, if you have local changes you dont want to lose, commit or stash them first, then do the first or second way as above. If you have many commits and can't or don't want to redo them manually, you can play with rebase.

Rebase 'edits' (rewrites) the history of commits and has the power of:

  • selecting or ignoring commits
  • changing the order of commits
  • squashing/merging commits into one, similar to --amend

So, to use rebase:

  • commit all local changes, DONT remove the huge file yet
  • remove the huge file, commit that as separate commit
  • run git rebase -i HASH_OF_ONE_COMMIT_BEFORE_HUGE_FILE_ADDED, this will run "interactive rebase" and should open a text editor with "rebase plan"
  • in the rebase plan, find the commit that adds the file (should be first on the list), find the commit that removes the file (should be last on the list)
  • in the rebase plan, cut&paste the pick line of the commit-that-deletes-the-file and move it right after the commit that added the file
  • change the mode (pick) of that commit into fixup
  • save the plan, let rebase continue

This will effectively "move in time" the file-deleting commit to be just-after the addition and then will merge addition and deletion into one commit (with message from addition). Effectively, it'd be just as if you remembered to --amend that back then. However, in fact this will rewrite history (btw. amend also does that), so if you have ever somehow managed to push any of the original-not-rewritten commits then other people may be angry at you after doing so. But that shouldn't be the case since you were unable to push since committing the large file, right?




回答2:


As you added the large file to the index you will need to use the git rm command to remove the file so that git no longer tracks the file. See git help rm for a full explanation.




回答3:


1) git log --oneline --decorate

The first line should have HEAD (this is your last commit) Some lines bellow, there should be origin/master, origin/HEAD (this is where your remote is, since your push didn't get through)

Since your commit is too big, you don't want to revert it but reset it (you don't want anymmore of it in the history), so that its content will never get pushed.

You'll be able to reset it because it's not pushed yet.

2) In the log, find the commit just before the one you made adding your big file and copy the hash (the 7 characters, say : e565r44).

3) Reset to this commit : git reset --hard e565r44 (use your hash)

4) Check your history : git log --oneline --decorate (and your working directory).

Then you should be able to push.

Warning: this will remove any commits between the HEAD and e565r44, but as I understand, you only did one (this can also be reverted if you mess it up, don't worry).



来源:https://stackoverflow.com/questions/31732550/github-wont-forget-deleted-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!