-1

It easy task to go back in time, but I want to go to the future. I want to find when something happened. I can do

get checkout HEAD~1

until I see the change. But I want to search faster so I'm doing:

get checkout HEAD~10, until I see when the app is working.

Then I want to find the specific commit, so I want to go back (future), commit by commit commit, searching for something like:

git checkout HEAD~+5.

Is something like that is exists?

UPDATe: I see that git bisect can help me do it. I keep the question, for others who don't know that git bisect exists, and have the same issue. Thanks for the answers.

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
  • Possible duplicate of [How to move HEAD back to a previous location?](http://stackoverflow.com/questions/34519665/how-to-move-head-back-to-a-previous-location) – CodeWizard Apr 16 '16 at 20:18
  • Possible duplicate of [How to use git bisect?](http://stackoverflow.com/questions/4713088/how-to-use-git-bisect) – Andrew C Apr 17 '16 at 02:47
  • It is not duplicate, because who asked "How to use git bisect", he knows what it is. This question if for who do not know what is git bisect, and searching for steps for finding the first commit of a bug. – Aminadav Glickshtein Apr 17 '16 at 07:22

3 Answers3

6

What you can do is use a branch name, instead of HEAD:

git checkout master~5

This gives you a "fixed point of reference". So if you want to back to the future, use master~1, or etc.

HEAD is your current position. Because two branches can diverge, there can be multiple paths forward.

Also, the procedure you're describing sounds like a "bisect". Git will actually do this for you, and it's really nice. I highly recommend it.

  1. git bisect start will start the operation, and save your current position.
  2. Type git bisect bad assuming where you started is not working.
  3. Next, git checkout HEAD~10 (or somewhere) and test. git bisect bad if not working, git bisect good if it's working.
  4. Repeat step 3 until you find a good commit. After that, it will automatically go forward or backward in the most efficient way possible - just keep testing and saying "good" or bad.
  5. When the operation is complete, type git bisect reset to go back to where you started.

This will manage going forward and back for you.

Todd Christensen
  • 1,297
  • 8
  • 11
  • Awesome. What I have looked For! A question, about your steps. What happen between step 3, and 4. How it is automatically go forward and backward? It will move 10 each time? – Aminadav Glickshtein Apr 16 '16 at 20:20
  • It will automatically move by halves. So between 10 commits, it will first go 5, then 2, etc. This gives you the least possible number of trials. You can also use `git bisect start path/stuff/` if you know the problem must've been caused by changes in `path/stuff/` and want to ignore other unrelated commits. – Todd Christensen Apr 16 '16 at 20:22
  • I did `git bisect bad` in step `3`, but it is didn't move the HEAD to any where – Aminadav Glickshtein Apr 16 '16 at 20:23
  • Ah, sorry, you have to find a good commit before it can start moving by halves. You'll have to repeat step 3 until you find a good commit, I'll edit. – Todd Christensen Apr 16 '16 at 20:24
  • Ok. I did git bisect good, and see something like that: `:040000 040000 576583e5a493ac6d8d98153a2227cf7ac09544f8 76c7e6d815e26e3199f13d5ef91be57f69244430 M frontend :040000 040000 f13d9b1c4c8f199d227d15ea1fb57a905806a63e ac1d84f40f4b4303e4bdee868fb55bc26a096e33 M queries` What is it mean? – Aminadav Glickshtein Apr 16 '16 at 20:26
  • It's telling you where it checked out to for your test. It should also tell you how many trials are left. When you're done, it will tell you "x is the first bad commit." – Todd Christensen Apr 16 '16 at 20:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109345/discussion-between-aminadav-and-todd-christensen). – Aminadav Glickshtein Apr 16 '16 at 20:27
1

Corrected answer

Apparently you need git bisect to find some error in code.

Old

do flowing:

git reflog
# find commit you are looking for, copy hash value
git checkout -b newBranchName <hash you have found>
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • How is it helps me? I found that now it is working in specific commit. How do I go to the next commits in the branch, do see until when is it working? – Aminadav Glickshtein Apr 16 '16 at 20:17
  • sorry i read only a title (I can see that not only me), you need https://git-scm.com/docs/git-bisect – Marek R Apr 16 '16 at 20:30
0

You have all the different ways to do it in here:

How to move HEAD back to a previous location? (Detached head)

You can use:

  • git reflog
  • git revert
  • git reset
  • git checkout

and more.

All the details are in the attached link.

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167