56

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

If so how would I specify multiple files?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
John Smith
  • 591
  • 1
  • 4
  • 4

6 Answers6

106

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
sjagr
  • 15,983
  • 5
  • 40
  • 67
8

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

Jason Mahony
  • 81
  • 1
  • 3
4

Possible option could be:

git status --porcelain | cut -c4- | xargs git checkout
sshepel
  • 880
  • 1
  • 9
  • 16
  • How do I filter this command to ignore certain extension files ? – Vicky Dev Jun 17 '20 at 13:18
  • You can try to add one more pipe statement with list of files for exclude (-f key for grep to exclude list of files) or provide specific file name directly. Single file: git status --porcelain | grep -vF sing-file-name-to-exclude.extention | cut -c4- | xargs git checkout. Or multiple files: git status --porcelain | grep -vFf list-of-excludes.txt |cut -c4- | xargs git checkout – sshepel Jun 21 '20 at 20:58
3

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.

Jonathan Cardoz
  • 874
  • 9
  • 12
0

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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

git checkout ./*
Peter Arboleda
  • 463
  • 3
  • 12