4

The new command, git restore <file> does not seem to have much documentation yet. Does it do exactly the same thing as git checkout -- <file>?

I do not understand the explanation of the new commands in the release notes.

In Git 2.23 Release Notes:

  • Two new commands "git switch" and "git restore" are introduced to split "checking out a branch to work on advancing its history" and "checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single "git checkout" command.

https://github.com/git/git/blob/master/Documentation/RelNotes/2.23.0.txt

Edit1

https://git-scm.com/docs/git-restore/2.23.0

Mike Thielvoldt
  • 181
  • 1
  • 11
  • 1
    Maybe this answer to [Find and restore a deleted file in a Git repository](https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository/57809290#57809290) will help. And maybe it won't. – Jonathan Leffler Sep 09 '19 at 23:18
  • 1
    @JonathanLeffler The Q is about command [`git restore`](https://git-scm.com/docs/git-restore). Please remove your comment. – phd Sep 09 '19 at 23:39
  • @phd: No! Why should I? The answer I point at is explicitly discussing `git restore`, even if the question as a whole is not (mainly because `git restore` wasn't around when the question was asked). – Jonathan Leffler Sep 09 '19 at 23:41

1 Answers1

4

With no additional options, yes:

git restore <filename>

and:

git checkout -- <filename>

are synonymous.

If you add options to the git restore, then no, they're no longer synonymous. You can add options to the git checkout which also break this particular pairing. There are some additional modes in which git restore and git checkout are synonymous, but there are a few git restore operations that have no corresponding git checkout command. For instance:

git restore --source HEAD --staged --worktree <filename>

means the same thing as:

git checkout HEAD -- <filename>

but:

git restore --source HEAD^ <filename>

(which extracts the HEAD^ variant of the given file to the work-tree without touching the index) cannot be reproduced through git checkout.

torek
  • 448,244
  • 59
  • 642
  • 775