如何 git reset --hard 所有但 4 个文件

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

How to git reset --hard everything but 4 files

git

提问by Ron Garrity

Assuming nothing is staged, what's the recommended way to reset all the work you've done, except for files:

假设没有上演任何内容,除了文件之外,重置您所做的所有工作的推荐方法是什么:

app/models/a.rb
app/views/a/index.html.rb
config/foo.rb
config/bar.rb

Other dirty files exist in app/models/ and app/views/ that I don't want to keep.

其他脏文件存在于 app/models/ 和 app/views/ 中,我不想保留。

回答by kan

As you described the problem, it doesn't look like you want do reset. The simplest thing for this is:

正如您描述的问题,看起来不像您想要的那样reset。最简单的事情是:

 git add app/models/a.rb app/views/a/index.html.rb config/foo.rb config/bar.rb
 git checkout .

so, it adds your 4 files into the index, and checks out clean versions (i.e. discards changes) for other files.

因此,它将您的 4 个文件添加到索引中,并检查其他文件的干净版本(即丢弃更改)。

I assume you have not staged (git add) modified files before (you should then unstage it using reset).

我假设您之前没有暂存 ( git add) 修改过的文件(然后您应该使用重置取消暂存)。

回答by siride

One way is to git commit those four files, then reset hard, and then undo the commit:

一种方法是 git commit 这四个文件,然后硬重置,然后撤消提交:

git add <files to keep>
git commit -m "temp"
git reset --hard
git reset HEAD~

回答by Tobias Kienzler

If those four files are in the index while the dirty files aren't, a simple git-cleanshould do the trick. If unsure, use the --dry-runswitch first, and notice the --excludeswitch.

如果这四个文件在索引中而脏文件不在索引中,那么简单的方法git-clean就可以解决问题。如果不确定,--dry-run请先使用开关,并注意--exclude开关。

Otherwise, stick with siride's answer

否则,坚持siride的回答