1

I am trying to rebuild my git workflow by using restore instead of the old checkout. I knew that restore can undo my previous work (on my branch). But what if I want to checkout a file from another branch? The checkout version is quite simple,

git checkout main -- my-file

Can I do the same thing with restore command?

My further question is: should I totally move from checkout to switch and restore since they already split some features apart, like:

git checkout -b new-branch   ===>   git switch -c new-branch
git checkout -- my-file      ===>   git restore -- my-file

I mean if I can also checkout a file from another branch using restore, there's almost no need of checkout in simple daily use.

Feel free to redirect this question to other similar one if any.

shen
  • 933
  • 10
  • 19

1 Answers1

2

As far as I know, the intention is to replace git checkout with git restore and git switch as appropriate.

Where git checkout takes two positional arguments to specify the branch and a file within that branch, git restore uses the --source argument to specify the "tree" (could be a branch, tag, commit, etc) from which to take the file given as a positional argument.

# git checkout dev my-file
git restore --source dev my-file
LeGEC
  • 46,477
  • 5
  • 57
  • 104
chepner
  • 497,756
  • 71
  • 530
  • 681
  • It works, thx. So do you recommend to use `restore` and `switch` to replace the old `checkout` and `reset`, I mean TOTALLY? – shen Jan 30 '22 at 21:41
  • 1
    `reset` is still used to change what a branch head refers to, independent of what `checkout`, `restore`, and `switch` do. I *try* to use `restore` and `switch` in place of `checkout`, and for my needs, they've been sufficient. I would stick with `checkout` in a script until `restore`/`switch` are no longer documented as experimental, but for command-line use, you can probably use them exclusively. – chepner Jan 30 '22 at 21:44
  • Thanks @chepner , it's quite helpful! – shen Jan 30 '22 at 21:48
  • Note that Junio Hamano, at least, still seems rather resistant to, um, "encouraging" use of separate restore and switch. Or that's my reading of messages on the Git mailing list. :-) I'm pretty firmly in the switch-to-switch camp myself, but obviously opinions vary. – torek Jan 30 '22 at 23:59
  • @torek I would be interested in a reference (link) to illustrate this resistance. And I am curious to see if https://public-inbox.org/git/pull.1095.git.1639117329.gitgitgadget@gmail.com/T/#u will go through (new option to the old checkout command) – VonC Jan 31 '22 at 07:43
  • @VonC: I'm afraid that's going to be very hard for me to find. It's a *feeling*, not an outright statement. – torek Jan 31 '22 at 07:57
  • @torek OK. That is reassuring. I will keep promoting switch/restore over the obsolete and confusing checkout command. – VonC Jan 31 '22 at 07:59