9

In this situation:

              v HEAD
     o--o--o--x--o--o (foo)
    /
o--o--o--o--o--o--o (master)

If I want to move one step backward I type:

$ git checkout HEAD~1
           v HEAD   
     o--o--o--x--o--o (foo)
    /
o--o--o--o--o--o--o (master)

If I want to move a step forward I came with this ridiculous command:

$ git log --pretty=oneline --all | \
       grep -B1 `git rev-parse HEAD` | \
       head -n1 | egrep -o '[a-f0-9]{20,}' | xargs git checkout

                 v HEAD
     o--o--o--x--o--o (foo)
    /                 
o--o--o--o--o--o--o (master)

Can I do better such as git checkout HEAD+1?

My current implementation in my ~/.gitconfig is:

[alias]
    # Move forward/Backward
    fw = "!git log --pretty=oneline --all | grep -B1 `git rev-parse HEAD` | head -n1 | egrep -o '[a-f0-9]{20,}' | xargs git checkout"
    bw = "!git checkout HEAD~1"
nowox
  • 25,978
  • 39
  • 143
  • 293
  • possible duplicate of [Referencing the child of a commit in Git](http://stackoverflow.com/questions/1761825/referencing-the-child-of-a-commit-in-git) – Joe Jun 25 '15 at 12:53

1 Answers1

14

Git commits have pointers back to their parents, which is why you can go backwards. They do not have pointers to their children, so you cannot "go forwards," as you are trying to do. The best you can do is look at the git log and determine the hash of the commit you want to checkout.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • Whoooh! This explain many things. Also this is totally absurd because `git bisect` is able to move forward and backward in a branch... – nowox Jun 25 '15 at 12:40
  • 2
    I'm pretty sure that `git bisect` doesn't actually move forward, it just moves backward from HEAD by a lesser amount, if that makes sense. – David Deutsch Jun 25 '15 at 12:43