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

GitGit Checkout

Git Problem Overview


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

If so how would I specify multiple files?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

Possible option could be:

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

Solution 4 - Git

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.

Solution 5 - Git

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 <[email protected]> Signed-off-by: Alexandr Miloslavskiy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>

Solution 6 - Git

Alternatively to what already has been answered I would do something like this for the current path:

git checkout ./*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohn SmithView Question on Stackoverflow
Solution 1 - GitsjagrView Answer on Stackoverflow
Solution 2 - GitJason MahonyView Answer on Stackoverflow
Solution 3 - GitsshepelView Answer on Stackoverflow
Solution 4 - GitJonathan CardozView Answer on Stackoverflow
Solution 5 - GitVonCView Answer on Stackoverflow
Solution 6 - GitPeter ArboledaView Answer on Stackoverflow