如何撤消git rm

时间:2015-11-29 13:09:37

标签: 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)

然后我做了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

然后(我忽略了我添加了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

然后我意识到我已经添加并提交了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

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

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

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

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

我现在该怎么办?

3 个答案:

答案 0 :(得分:8)

如果它只是在最后一次提交中, 然后执行git rm huge_file.log然后git commit --amend

Git允许您为提交 ted的最后一个错误进行修改。字面上。

答案 1 :(得分:3)

你可以做到

$ git reset --soft HEAD~[n]

其中[n]是您希望重置的过去提交次数。

例如,撤消上次提交

$ git reset --soft HEAD~1
$ git status

然后像往常一样继续提交文件。

答案 2 :(得分:1)

如果您尚未提交。您可以使用@types/react-router

相关问题