撤消 git rm -r --cached

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25311743/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:18:23  来源:igfitidea点击:

Undo git rm -r --cached

git

提问by Ned

My .gitignorefile wasn't working, so I followed this question.

我的.gitignore文件不起作用,所以我跟着这个问题

And now I have bunch of files that I need to commit, in order to proceed. I made a mistake and committed those, because I couldn't find a way to get rid of those.

现在我有一堆文件需要提交,以便继续。我犯了错误并犯了这些错误,因为我找不到摆脱它们的方法。

Is there any way that I can revert all these actions? I can't do resetcause I now I get error: error: The following untracked working tree files would be overwritten by checkout.

有什么办法可以恢复所有这些操作吗?我不能做,reset因为我现在收到错误: error: The following untracked working tree files would be overwritten by checkout

Stupid stupid mistake, and I can find solution for this.

愚蠢的愚蠢错误,我可以找到解决方案。

回答by u1860929

If you've run only git rm -r --cached, try doing a git reset HEAD .from within your repo root.

如果您只运行了git rm -r --cached,请尝试git reset HEAD .从您的存储库根目录中执行 a 。

If you did a git commit -m "msg"after doing a git rm -r --cached, i.e., you committed the changes, then do git reset HEAD~1to undo your last commit.

如果你做的git commit -m "msg"做了之后git rm -r --cached,也就是说,你所犯下的变化,然后做git reset HEAD~1撤消上次提交

回答by big kev

Git works based on file caching so if you removed everything from the cache you can just reverse the whole process by executing .This will add back the files that were being tracked and tell which ones have been modified since the last commit .

Git 基于文件缓存工作,因此如果您从缓存中删除所有内容,您可以通过执行来反转整个过程。这将添加回正在跟踪的文件并告诉自上次提交以来哪些文件已被修改。

> git add . 

回答by Hesham Yassin

Running git checkout HEAD path/to/filewill not work if the file is removed from the cache.

git checkout HEAD path/to/file如果文件从缓存中删除,运行将不起作用。

What worked for me is to move to a new branch by running the command:

对我有用的是通过运行以下命令移动到新分支:

git checkout -b newBranch

回答by Juancho Ramone

If you want to revert it for only a particular file you can do git restore --staged <file>

如果您只想为特定文件恢复它,您可以这样做 git restore --staged <file>