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

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

是否可以对多个文件使用 git 检出以放弃更改?

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

运行多次命令

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

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

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

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

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

我在我用户的 git 回购目录中 运行 不小心修改了目录中的所有文件。

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

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

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

使用以下命令

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

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

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

可能的选项可以是:

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

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

在 Git 2.25(2020 年第一季度)中,更多命令学习了“--pathspec-from-file”命令行选项,which I previously mentioned for git commit

这意味着您可以将 old confusing git checkout command, or the new command (Git 2.23+) git restore 文件列表 (到 checkout/restore)一起使用,而不是在不同的系统上多次重新启动相同的命令文件。

参见 commit a9aecc7, commit cfd9376, commit 8ea1189, commit 6fdc9ad, commit 1d022bb, commit bebb5d6, commit 21bb308 (03 Dec 2019) by Alexandr Miloslavskiy (SyntevoAlex)
(由 Junio C Hamano -- gitster -- in commit 135365d 合并,2019 年 12 月 25 日)

checkout, restore: support the --pathspec-from-file option

Signed-off-by: Alexandr Miloslavskiy

Decisions taken for simplicity:

  1. For now, --pathspec-from-file is 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 restore block was moved down to be able to test for pathspec.nr instead, because testing for argc is no longer correct.

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


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

参见 commit f94f7bd (30 Dec 2019) by Alexandr Miloslavskiy (SyntevoAlex)
(由 Junio C Hamano -- gitster -- in commit 96aef8f 合并,2020 年 1 月 30 日)

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

对于已经回答的问题,我会为当前路径做这样的事情:

git checkout ./*