如何撤消git rm

soc*_*qwe 2 git

我不小心添加并将一个(巨大的)日志文件提交到我的本地git存储库,然后执行了git rm(没有--cached).所以历史看起来像这样:

> 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)
Run Code Online (Sandbox Code Playgroud)

然后我做了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
Run Code Online (Sandbox Code Playgroud)

然后(我忽略了我已经添加huge_file.log)我做了一个git commit:

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

然后我意识到我已经添加并提交了 huge_file.log.所以我认为我可以通过以下方式撤消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
Run Code Online (Sandbox Code Playgroud)

所以在这一点上,我意识到我所做的混乱:我无法提交并推动掌握这一变化,因为这意味着推送huge_file.log然后删除huge_file.log.不幸的是,我无法推送,因为我们的文件限制为每个文件100 MB,huge_file.log超过1 GB.

那我现在该怎么办?我不能简单地将HEAD重置为先前的提交,因为我做了很多更改,Foo.java因为我不想撤消.

有没有办法改变以前的提交以huge_file.log某种方式删除.

请注意,这Foo.java只是代表在StackOverflow上更好的可读性.我已经触及了10个文件并修改了每个文件中的许多行代码,我不想丢失这些更改.

我现在应该怎么做?

PSk*_*cik 8

如果它只是在最后一次提交中,那么请git rm huge_file.log继续git commit --amend.

Git允许你为你提交的最后一个错误修改 s .从字面上看.


小智 5

如果你还没有承诺。您可以使用git restore --source=HEAD --staged --worktree <file>

  • 在运行此命令时,我收到此错误“git:‘restore’不是 git 命令。” (2认同)