Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo git rm

Tags:

git

I have accidentally added and committed a (huge) log file into my local git repository and then did a git rm (without --cached). So the history looks like this:

> git status

modified:  Foo.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    huge_file.log

nothing added to commit but untracked files present (use "git add" to track)

Then I did git add .:

> git add .
> git status

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    modified:  Foo.java
    new file:   huge_file.log

Then (I have overlooked that I've added huge_file.log) I did a git commit:

> git commit -am "Foo commit"
[master 7582793] Foo commit
 1 file changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 huge_file.log

Then I realized that I have added and committed huge_file.log. So I thought I could undo that by doing git rm huge_file.log:

> git rm huge_file.log
> git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    huge_file.log

So at this point, I realized the mess I made: I can't commit and push to master this changes because that would mean pushing huge_file.log and then deleting huge_file.log. Unfortunately, I can't push since we have a file limit of 100 MB per file and huge_file.log is more than 1 GB.

So what can I do now? I can't simply reset HEAD to a previous commit since I have made a lot of changes in Foo.java that I don't want to undo.

Is there a way to change the previous commit to remove huge_file.log somehow.

Please note that Foo.java is just representative for better readability here on StackOverflow. I have touched about 10 files and modified many lines of code in each file, and I don't want to lose this changes.

What should I do now?

like image 994
sockeqwe Avatar asked Dec 19 '22 21:12

sockeqwe


1 Answers

If it's just in the last commit, then do git rm huge_file.log and then git commit --amend.

Git allows you to make amends for the last error you've committed. Literally.

like image 184
PSkocik Avatar answered Dec 31 '22 11:12

PSkocik