Can I use “git checkout --” on two files?

2020-02-10 10:47发布

问题:

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

If so how would I specify multiple files?

回答1:

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


回答2:

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

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.

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



回答3:

Possible option could be:

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


回答4:

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

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



回答5:

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.

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

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)

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.


With Git 2.26 (Q1 2020) adds more tests.

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

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