321

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?

Paul
  • 25,812
  • 38
  • 124
  • 247
  • Related: [Revert to a previous Git commit](http://stackoverflow.com/q/4114095/456814). –  Jul 22 '14 at 03:36

3 Answers3

438

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.

user1134918
  • 129
  • 1
  • 8
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93
75

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.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • 4
    I think you mean `git checkout `. `git checkout HEAD` is effectively a NOOP – Abe Voelker Apr 19 '12 at 14:46
  • 4
    `git reset --hard ` changes the HEAD of the current branch, while with `git checkout ` 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. – jofel Apr 19 '12 at 16:15
  • @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 ? – nutty about natty Apr 01 '13 at 09:22
  • @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. – echristopherson Jul 04 '13 at 21:25
31

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 checkouts 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.

Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • 8
    Note that `git checkout -` is a shorthand alias for `git checkout @{-1}` – N7L Mar 24 '20 at 12:44
  • @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. – Benjohn Mar 25 '20 at 14:57
  • 1
    No 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 like `cd -` in bash. – N7L Mar 26 '20 at 15:09