Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

If so how would I specify multiple files?

like image 889
John Smith Avatar asked Feb 17 '15 05:02

John Smith


People also ask

Does git checkout change files?

The checkout is not problematic and Git carries it out without batting an eyelid: the contents of that particular revision get copied to your working tree (and overwrite stuff already present there, if needed).

Can we checkout specific folder in git?

To checkout everything from your HEAD (not index) to a specific out directory: git --work-tree=/path/to/outputdir checkout HEAD -- .

What does git checkout -- file do?

Git Checkout File Checking out a file is similar to using git reset with a file path, except it updates the working directory instead of the stage. Unlike the commit-level version of this command, this does not move the HEAD reference, which means that you won't switch branches.

What are two uses for git checkout?

It can be used to create branches, switch branches, and checkout remote branches. The git checkout command is an essential tool for standard Git operation.


3 Answers

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
like image 119
sjagr Avatar answered Oct 22 '22 17:10

sjagr


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

like image 8
Jason Mahony Avatar answered Oct 22 '22 19:10

Jason Mahony


Possible option could be:

git status --porcelain | cut -c4- | xargs git checkout
like image 4
sshepel Avatar answered Oct 22 '22 19:10

sshepel