如何让 GIT 忽略我的更改

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

How to make GIT ignore my changes

git

提问by rahman

Although the title seems similar to previous questions, I could not find the solution to to my Simple case:

虽然标题看起来与之前的问题相似,但我找不到我的简单案例的解决方案:

  • I committed to my branch 'ABCD' : git commit -a .....
  • Then decided to visit a previous commit so : git checkout 2387687326487
  • Here I made some changes which was for testing purpose only, and I don't want to keep them
  • I am done, I want to get back to my latest commit(again:ignoring changes whcih i made to the old commit) : the command git checkout 'ABCD'gives error:
  • 我致力于我的分支 'ABCD' : git commit -a .....
  • 然后决定访问以前的提交,因此: git checkout 2387687326487
  • 在这里我做了一些仅用于测试目的的更改,我不想保留它们
  • 我完成了,我想回到我最近的提交(再次:忽略我对旧提交所做的更改):命令git checkout 'ABCD'给出错误:

Your local changes to the following files will be overwritten by checkout....please commit or stash...

您对以下文件的本地更改将被结帐覆盖....请提交或隐藏...

while I don't want to commit or stash or whatever. I just want to go back home :) what should I do please?

虽然我不想提交或藏匿或其他什么。我只想回家 :) 我该怎么办?

采纳答案by Nicolas Barbey

If you are sure that you don't want to keep your changes, the following line will do :

如果您确定不想保留更改,以下行将执行:

git checkout -f 'ABCD'

Option '-f' stands for force.

选项“-f”代表力。

回答by Carlos Campderrós

Using just

仅使用

git checkout .

will discard any uncommitted changes in the current directory (the dot means current directory).

将丢弃当前目录中任何未提交的更改(点表示当前目录)。

EDITin response to @GokulNK:

编辑回应@GokulNK:

If you want to keep those changes for future use, you have 2 options:

如果您想保留这些更改以备将来使用,您有两个选择:

  • git stash: this will save those changes in a stack. You can apply the changes to your working copy later using git stash pop
  • git diff > changes.patch: This will save those changes to the file changes.patch. If you want to apply them to your working copy you can do so: git apply changes.patch.
  • git stash:这会将这些更改保存在堆栈中。您可以稍后使用将更改应用于您的工作副本git stash pop
  • git diff > changes.patch:这会将这些更改保存到文件中changes.patch。如果你想将它们应用到你的工作拷贝,你可以这样做:git apply changes.patch

回答by Marcin Koziński

To undo local changes and go back to the state from the commit you can:

要撤消本地更改并从提交返回状态,您可以:

git reset --hard

回答by steffen

Apart from the mentioned solutions you can stash the changes

除了提到的解决方案,您还可以隐藏更改

git stash

Checkout your branch

结帐你的分行

git checkout 'ABCD'

And eventually, when you are sure that you don't need anything from the stashed changes, and have checked twice with gitkor gitg, throw away the stash:

最终,当您确定不需要任何隐藏的更改时,并使用gitk或进行了两次检查gitg,扔掉隐藏的内容:

git stash drop

That is my preferred way, as it is safer.

这是我的首选方式,因为它更安全。