71

I want to load a different version of the files that exist in another branch into my current branch.

git help checkout says:

DESCRIPTION
   Updates files in the working tree to match the version in the index or
   the specified tree. If no paths are given, git checkout will also
   update HEAD to set the specified branch as the current branch.

Is there a way to checkout all those files, but not update HEAD?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kache
  • 15,647
  • 12
  • 51
  • 79
  • @BobbyA I don't think so. 1: the question and answer are about something else (branch & file/folder name collisions). 2: this question is older. – Kache Jun 12 '18 at 18:02
  • I flagged the wrong question (multiple tabs), realized it immediately, and retracted the flag. I didn't realize there was a comment though. I got caught! Apologies :) Deleting that comment now since it's misleading – BobbyA Jun 12 '18 at 22:04

2 Answers2

99

checkout by providing the current path, .:

git checkout other-branch-name -- .

This operation is similar to switching HEAD to another branch without checking out files, but just from the "other direction".

As @김민준 mentions, this overwrites any uncommitted changes. Remember to either stash or commit them somewhere first if needed.

Kache
  • 15,647
  • 12
  • 51
  • 79
  • 6
    Just a heads up, this will discard all your uncommited work. Obvious in hindsight, but got bitten hard :'( – 김민준 May 16 '17 at 03:12
  • 1
    This is the easiest way to perform a merge from a feature branch from which you don't want to keep all the details. The `-- .` was a bit hard to find, thanks! – Laurent Caillette May 29 '18 at 06:56
  • 1
    can you explain what -- . is actually doing? "." is here and -- with no argument mean "all" ? – red888 Aug 27 '18 at 14:37
  • As far as I know, Git uses `--` as a separator between the commands to the left and the file globs to the right. – Kache Aug 27 '18 at 15:48
  • This didn't work exactly for me, using Windows (maybe other differences?) I had to use `git checkout -- C:\path\to\changes\*` – rythos42 Jan 08 '19 at 18:33
6

Similar to @Kache's answer, but using the newer git restore command (requires Git version 2.23 or above):

git restore --source=<other-branch/tag/commit> <pathspec>
# or
git restore -s <other-branch/tag/commit> <pathspec>

# example: to load all files from branch "other"
git restore -s other .

This new command was introduced to split "checking out a branch" and "checking out files" out of the single git checkout command. Read more: What is the git restore command.

zypA13510
  • 1,092
  • 1
  • 10
  • 28