Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkout a commit without updating HEAD

Tags:

git

I need to update working directory and index to the state of some commit. When I run git checkout Git updates HEAD, index and a working directory. I know I can checkout a single file from a commit without updating HEAD by simply specifying a path to the file. But what is the best way to do that for the entire working directory?

At the moment I'm simply doing:

ID=$(git rev-parse HEAD)
git reset --hard COMMIT_ID
git reset --soft $ID
like image 775
Max Koretskyi Avatar asked Jun 16 '26 08:06

Max Koretskyi


1 Answers

Use git checkout with a dot as a path

git checkout COMMIT_ID .

From the man page of git checkout:

git checkout [<tree-ish>] [--] <pathspec>…​

Overwrite paths in the working tree by replacing with the contents in the index or in the <tree-ish> (most often a commit). When a <tree-ish> is given, the paths that match the are updated both in the index and in the working tree.

The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out. Using -f will ignore these unmerged entries. The contents from a specific side of the merge can be checked out of the index by using --ours or --theirs. With -m, changes made to the working tree file can be discarded to re-create the original conflicted merge result.

like image 72
Ferrybig Avatar answered Jun 19 '26 06:06

Ferrybig