Temporarily switch working copy to a specific Git commit
Solution 1
If you are at a certain branch mybranch
, just go ahead and git checkout commit_hash
. Then you can return to your branch by git checkout mybranch
. I had the same game bisecting a bug today :) Also, you should know about git bisect.
Solution 2
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, run git 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.
Solution 3
In addition to the other answers here showing you how to git checkout <the-hash-you-want>
it's worth knowing you can switch back to where you were using:
git checkout @{-1}
This is often more convenient than:
git checkout what-was-that-original-branch-called-again-question-mark
As you might anticipate, git checkout @{-2}
will take you back to the branch you were at two git checkout
s ago, and similarly for other numbers. If you can remember where you were for bigger numbers, you should get some kind of medal for that.
Sadly for productivity, git checkout @{1}
does not take you to the branch you will be on in future, which is a shame.
Solution 4
I once implemented git checkout @{10} and was working fine, then suddenly my jumbo cup of caramel coffee fell on the PC and....
Related videos on Youtube
![Paul](https://i.stack.imgur.com/ZCbkS.jpg?s=256&g=1)
Paul
It is just a website. Life is somewhere else. Answers to downvoted question won't be accepted. Period. My wishlist The right syntax of with in Delphi: with r := obj.MyRecord do begin r.Field1 := 1; r.Field2 := '2'; // ... end; Favorites https://stackoverflow.blog/2019/10/17/imho-the-mythical-fullstack-engineer/ http://www.catb.org/esr/faqs/smart-questions.html Stackoverflow How to reduce image size on Stack Overflow Unicode Is there a list of characters that look similar to English letters? Windows How to programmatically get DLL dependencies Device misdetected as serial mouse Catch MSVCR120 missing error message in Delphi Get members of COM object via Delphi Olevariant type Controlling the master speaker volume in Windows 7 Filename timestamp in Windows CMD batch script getting truncated https://www.joachim-bauch.de/tutorials/loading-a-dll-from-memory Linux How to control backlight by terminal command MS Visual Studio How to change "Visual Studio 2017" folder location? Delphi Convert a null-delimited PWideChar to list of strings Out-of-the-box flat borderless button TFDConnection.OnRecover is never fired when PostgreSQL stops TScrollBox with customized flat border color and width? How to redirect mouse events to another control? Creating object instance based on unconstrained generic type how to create a TCustomControl that behaves like Tpanel? https://stackoverflow.com/a/43990453 - How to hack into a read-only direct getter property With DDevExtensions you can disable storing Explicit... properties in the dfm https://github.com/RomanYankovsky/DelphiAST C Arrow operator (->) usage in C The Definitive C++ Book Guide and List GDI+ How to rotate Text in GDI+? GDI+ performance tricks MSXML schema validation with msxml in delphi How can I get English error messages when loading XML using MSXML Inno Setup: Save XML document with indentation PostgreSQL Checking for existence of index in PostgreSQL Web Will the IE9 WebBrowser Control Support all of IE9's features, including SVG? http://howtocenterincss.com MVC for Web, MVP for Winforms and MVVM for WPF. Just an observation: Most of the claims such as "I can't understand what you wrote", "Please provide more details", etc. are made by people with a relatively low SO score. People with the highest score on SO understand everything. Template: This is an abandoned question. Author has no longer anything to do with the topic and can neither approve nor decline your answer.
Updated on July 08, 2022Comments
-
Paul almost 2 years
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?
-
Admin almost 10 yearsRelated: Revert to a previous Git commit.
-
-
Abe Voelker about 12 yearsI think you mean
git checkout <original_branch>
.git checkout HEAD
is effectively a NOOP -
jofel about 12 years
git reset --hard <hash>
changes the HEAD of the current branch, while withgit checkout <hash>
you get a detached checkout which does not change any branch, and you can easily return without knowing the original hash ID of your branch as shown in this answer. -
enderland over 11 yearsNote you can just do
git checkout commit_hash
if you are on a clean repository and not need to do branching. Might be easier for some use cases (like mine). -
nutty about natty about 11 years@Femaref Beginner's question: given the context of this question (switch to an earlier commit temporarily), why would it be an advantage or disadvantage to move or not move the HEAD ?
-
echristopherson almost 11 years@nuttyaboutnatty Assuming my edit is approved, it should answer your question. HEAD actually gets moved in any event; but in a checkout the branch reference HEAD points to is not itself moved.
-
Alexander Pavlov about 10 years@enderland: your HEAD always points at some branch, normally :)
-
mightyiam almost 10 yearsI've had an issue where I had to use the whole commit hash because a partial one wasn't accepted.
-
Niek over 7 yearsUpvote for the
git bisect
reference; what an extremely useful tool! -
Nathanael over 4 yearsNote that
git checkout -
is a shorthand alias forgit checkout @{-1}
-
Benjohn over 4 years@Nathanael OMGOD, no way … this changes everything! Nice, thank you! … I was going to incorporate this in the answer, but I think it's also useful to know about the general
@{n}
syntax, as it works with lots of git commands. I found it hard to add your shorthand without making the answer rather confusing. Instead I've voted up your comment – I hope people will see it. Thanks again. -
Nathanael over 4 yearsNo problem. This discussion is tangential to the actual question anyway. More of a bonus! I often use the same syntax for merging features into a release. e.g.
git merge -
to merge the branch you had last checked out into the currently checked out branch. It's likecd -
in bash. -
Andrew Halil over 2 yearsThis does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
-
avalanche1 over 2 yearsahaha hillarious!