git 如何撤消git rm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33983138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to undo git rm
提问by sockeqwe
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 存储库中,然后做了一个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)
Then I did git add .
:
然后我做了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
:
然后(我忽略了我添加的内容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
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
:
然后我意识到我已经添加并提交了 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
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.
所以在这一点上,我意识到我造成的混乱:我无法提交和推送来掌握这些更改,因为这意味着推送huge_file.log
然后删除huge_file.log
. 不幸的是,我无法推送,因为每个文件的文件限制为 100 MB,并且huge_file.log
超过 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.
那我现在能做什么?我不能简单地将 HEAD 重置为之前的提交,因为我做了很多Foo.java
不想撤消的更改。
Is there a way to change the previous commit to remove huge_file.log
somehow.
有没有办法改变以前的提交以huge_file.log
某种方式删除。
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.
请注意,这Foo.java
仅代表 StackOverflow 上更好的可读性。我接触了大约 10 个文件并修改了每个文件中的多行代码,我不想丢失这些更改。
What should I do now?
我现在该怎么办?
回答by PSkocik
If it's just in the last commit,
then do git rm huge_file.log
and then git commit --amend
.
如果它只是在最后一次提交中,那么执行git rm huge_file.log
然后git commit --amend
.
Git allows you to make amends for the last error you've committed. Literally.
Git 允许您对提交的最后一个错误进行修改。字面上地。
回答by Achrome
You can just do
你可以做
$ git reset --soft HEAD~[n]
where [n]
is the number of commits in the past you wish to reset.
[n]
您希望重置的过去提交次数在哪里。
For example, to undo your last commit
例如,撤销上次提交
$ git reset --soft HEAD~1
$ git status
And then proceed to commit files as you would normally do.
然后像往常一样继续提交文件。
回答by Kirubel Tadesse
If you have not committed yet. You can use git restore <file>
如果你还没有承诺。您可以使用git restore <file>