我可以在两个文件上使用“git checkout --”吗?

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

Can I use "git checkout --" on two files?

gitgit-checkout

提问by John Smith

Is it possible to use git checkout -- on multiple files in order to discard the changes?

是否可以在多个文件上使用 git checkout -- 以放弃更改?

If so how would I specify multiple files?

如果是这样,我将如何指定多个文件?

回答by sjagr

Run the command multiple times

多次运行命令

git checkout -- path/to/file/one
git checkout -- path/to/file/two

Or specify the multiple files in the same line:

或者在同一行中指定多个文件:

git checkout -- path/to/file/one path/to/file/two

You can also specify entire folders which will recurse to all files below them.

您还可以指定整个文件夹,这些文件夹将递归到它们下面的所有文件。

git checkout -- path/to/folder
git checkout -- . # for the current path

回答by Jason Mahony

I accidentally modified all files in a directory by running find in my user's git repo directory.

我通过在用户的 git repo 目录中运行 find 不小心修改了目录中的所有文件。

me@server:/home/me/gitrepo# git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .bashrc
    modified:   dir/ec2-user.pem
    modified:   dir/readme.txt
    modified:   dir/root.pem
    modified:   dir/ec2-user.pem
    modified:   dir/readme.txt
    modified:   dir/root.pem
    modified:   dir/ec2-user.pem
    modified:   dir/ec2-user.pem.pub
    modified:   dir/readme.txt
    modified:   dir/root.pem

To correct my mistake I ran something like this command to find all modified files and checkout the files from master.

为了纠正我的错误,我运行了类似这样的命令来查找所有修改过的文件并从 master 中检出文件。

git status | grep modified | sed 's/^.*modified: //' | xargs git checkout

git status | grep modified | sed 's/^.*modified: //' | xargs git checkout

回答by sshepel

Possible option could be:

可能的选择可能是:

git status --porcelain | cut -c4- | xargs git checkout

回答by Jonathan Cardoz

Use the following command

使用以下命令

   git checkout path/to/fileName path/to/fileName2 path/to/fileName3

This will allow you to revert or discard your changes to the files fileName, fileName2 and fileName3

这将允许您恢复或放弃对文件 fileName、fileName2 和 fileName3 的更改

Note: that spaces in the path name will cause an issue with git. Use single quotes '' to encase the path name in such cases.

注意:路径名中的空格会导致 git 出现问题。在这种情况下,请使用单引号 '' 将路径名括起来。

回答by VonC

List the two (or more) files in a file.

列出文件中的两个(或更多)文件。

With Git 2.25 (Q1 2020), a few more commands learned the "--pathspec-from-file" command line option, which I previously mentioned for git commit.

在 Git 2.25(2020 年第一季度)中,更多的命令学习了“ --pathspec-from-file”命令行选项,我之前在git commit.

That means you can use the old confusing git checkoutcommand, or the new command(Git 2.23+) git restorewith a list of files(to checkout/restore), instead of reaptinng multiple time the same command on different files.

这意味着您可以使用旧的令人困惑的git checkout命令新命令(Git 2.23+)git restore文件列表(用于检出/恢复),而不是在不同的文件上多次重复执行相同的命令。

See commit a9aecc7, commit cfd9376, commit 8ea1189, commit 6fdc9ad, commit 1d022bb, commit bebb5d6, commit 21bb308(03 Dec 2019) by Alexandr Miloslavskiy (SyntevoAlex).
(Merged by Junio C Hamano -- gitster--in commit 135365d, 25 Dec 2019)

参见Alexandr Miloslavskiy ( ) 的commit a9aecc7commit cfd9376commit 8ea1189commit 6fdc9adcommit 1d022bbcommit bebb5d6commit 21bb308(2019 年 12 月 3 日(由Junio C Hamano合并-- --commit 135365d,2019 年 12 月 25 日)SyntevoAlex
gitster

checkout, restore: support the --pathspec-from-fileoption

Signed-off-by: Alexandr Miloslavskiy

Decisions taken for simplicity:

  1. For now, --pathspec-from-fileis declared incompatible with --patch, even when <file>is not stdin. Such use case it not really expected.
  2. It is not allowed to pass pathspec in both args and file.

you must specify path(s) to restoreblock was moved down to be able to test for pathspec.nrinstead, because testing for argcis no longer correct.

git switchdoes not support the new options because it doesn't expect <pathspec>arguments.

checkout, restore: 支持--pathspec-from-file选项

签字人:亚历山大·米洛斯拉夫斯基

为简单起见而作出的决定:

  1. 目前,--pathspec-from-file被声明为与 不兼容--patch,即使<file>不是stdin。这样的用例并不是真正预期的。
  2. 不允许在 args 和 file 中传递 pathspec。

you must specify path(s) to restore块被向下移动以能够进行测试pathspec.nr,因为测试argc不再正确。

git switch不支持新选项,因为它不需要<pathspec>参数。



With Git 2.26 (Q1 2020) adds more tests.

Git 2.26(2020 年第一季度)添加了更多测试。

See commit f94f7bd(30 Dec 2019) by Alexandr Miloslavskiy (SyntevoAlex).
(Merged by Junio C Hamano -- gitster--in commit 96aef8f, 30 Jan 2020)

请参阅Alexandr Miloslavskiy ( )提交的 f94f7bd(2019 年 12 月 30 日)(由Junio C Hamano合并-- --提交 96aef8f 中,2020 年 1 月 30 日)SyntevoAlex
gitster

t: add tests for error conditions with --pathspec-from-file

Suggested-By: Phillip Wood Signed-off-by: Alexandr Miloslavskiy Signed-off-by: Junio C Hamano

t: 使用 --pathspec-from-file 添加错误条件测试

推荐人:Phillip Wood 签字人:Alexandr Miloslavskiy 签字人:Junio C Hamano