Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout HEAD+1 and HEAD-1

Tags:

git

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"
like image 646
nowox Avatar asked Jun 25 '15 12:06

nowox


1 Answers

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.

like image 94
David Deutsch Avatar answered Sep 22 '22 18:09

David Deutsch