How to switch to specific Git commit without losing all the commits made after it?
I want that local files will be changed, but commits' database will remain intact, only the current position pointer is set to currently selected commit.
I want to change files' state to specific commit, run project and, when finished, restore files back to last commit.
How to do this without zipping the whole project's folder?
First, use
git log
to see the log, pick the commit you want, note down the sha1 hash that is used to identify the commit. Next, rungit checkout hash
. After you are done,git checkout original_branch
. This has the advantage of not moving the HEAD, it simply switches the working copy to a specific commit.If you are at a certain branch
mybranch
, just go ahead andgit checkout commit_hash
. Then you can return to your branch bygit checkout mybranch
. I had the same game bisecting a bug today :) Also, you should know about git bisect.In addition to the other answers here showing you how to
git checkout <hash-that-you-want>
it's worth knowing that you can then switch back to where you were usinggit checkout @{-1}
. This is often more convenient thangit checkout original-branch
.As you might expect,
git checkout @{-2}
will take you back to the branch you were at twogit checkout
s ago, and similarly for other numbers. If you can remember where you were for bigger numbers then you should get some kind of medal for that.