Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: checkout files from another branch into current branch (don't switch HEAD to the other branch)

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?

like image 234
Kache Avatar asked Mar 20 '13 23:03

Kache


People also ask

Does git checkout change branches?

The git checkout command is a useful and multi-purpose command. You can use it to create new branches, checkout a branch, checkout specific commits, and more.

Does git checkout branch from current branch?

The "checkout" command can switch the currently active branch - but it can also be used to restore files. The most common use case for "checkout" is when you want to switch to a different branch, making it the new HEAD branch.


1 Answers

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.

like image 188
Kache Avatar answered Oct 03 '22 07:10

Kache