简体   繁体   中英

Removing commited files from older commit that have been deleted locally

I've managed to commit some files incidentally in commit A001 . Those files are quite big (around 100MB each). After noticing my mistake, In next commit, I've deleted those files with rm command, and commited those changes to commit A002 . I've thought that this solved my problem and continued developing. After few commits, let's say commit A005 I've tried to push everything on remote (Large files have been deleted, but they're still commited in commit A001 ), push fails with following message:

remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
...
...

 ! [remote rejected] melodic-dev -> melodic-dev (pre-receive hook declined)

Which is okay, because large files still exist in commit A001 .

How can I do following:

  • Go back to commit A001 , unstage those large files and recreate commit A001 without those large files that have been removed, do same to commit A002
  • Recreate all following commits A003 - A005 as they were on tip of the recreated A001_edited-A002_edited commits without those large files
  • Push corrected A001-A005 commits to remote branch

I would need something like: Create new branch that has all commits NEW_BRANCH:A001-A005 . In that branch, I would like to remove some files from commit A001 , therefore i would have NEW_BRANCH: A001_edited commit, and I would like to merge OLD_BRANCH:A001-A002-A003-A004-A005 with NEW_BRANCH: A001_edited-A002_edited so that I have following: OLD_BRANCH: A001_edited-A002_edited-A003-A004-A005

A001_edited = A001 without commiting (revert git add Large files ) those large files A002_edited = A002 without removing (revert git rm Large files ) those large files

In two successive commits somewhere in history, I want to remove some staged/unstaged files and keep everything else the same.

I've tried some stuff, but without success. I hope I've clarified my problem enough, if not let me know:)

Thank you for your time and help.

This can be fixed with a powerful and scary approach called rebasing: :) Here's one way:

  1. Run git rebase -i --root . This will open an editor listing all commits all the way from A001 to your latest one.
  2. In that file, change the word "pick" in front of A001 to "edit". Save the file and close the editor.
  3. Git will rewind you to the moment after you made A001 (where you added the big files by mistake). Run git rm --cached bigfile1 bigfile2 to remove them from the index, then run git commit --amend to fix commit A001 to not contain them.
  4. Run git rebase --continue . This will reapply all commits after that on top of the commit you changed. A002 should now only contain the changes you made other than removing the files. No commit should contain the big files anymore.

Hope that works for you, let me know if you encounter any problems!

I've managed to use rebase as suggested by blinry but without git rm --cached bigfile1 bigfile2 .

I would use git rm --cached bigfile1 bigfile2 if I haven't already done so with A002 commit.

To clarify, I've added accidentaly some big files to A001 and removed them in commit A002 which disabled me from pushing on remote branch. I've simply used interactive rebasing and squashed commit A002 into A001 : as follows:

pick A005 
pick A004 
pick A003
squash A002
pick A001

Squash command "merges" choosen commit with previous one, in this case it "merged" removal of big files with addition of big files and some code changes, which resulted with wanted code changes and without big files, and clean git history ( Everything before A001 - A001_without_big_files - A003 - A004 - A005 )

Thank you for your help:)

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