Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git checkout next / prev commit

Treat git log as a book, exec git next or git prev to checkout the next or the previous commit.

#!/bin/sh
first() {
branch=refs/heads/master
git log --reverse --pretty=%H $branch | head -1 | xargs git checkout
}
first "$@"
#!/bin/sh
last() {
branch=refs/heads/master
git log --pretty=%H $branch | head -1 | xargs git checkout
}
last "$@"
#!/bin/sh
next() {
branch=refs/heads/master
if [ -z "$1" ]; then
n=1
else
n=$1
fi
git log --reverse --pretty=%H $branch | grep -A $n $(git rev-parse HEAD) | tail -1 | xargs git checkout
}
next "$@"
#!/bin/sh
prev() {
branch=refs/heads/master
if [ -z "$1" ]; then
n=1
else
n=$1
fi
git checkout HEAD~$n
}
prev "$@"
@hutusi

This comment has been minimized.

Copy link
Owner Author

commented Aug 9, 2019

alias git diff to make a quick diff between current commit with the previous commit:

git config --global alias.df 'diff HEAD^ HEAD'
@geekrainy

This comment has been minimized.

Copy link

commented Aug 26, 2019

#!/bin/sh

to

#!/usr/bin/env bash
@hutusi

This comment has been minimized.

Copy link
Owner Author

commented Aug 27, 2019

#!/bin/sh

to

#!/usr/bin/env bash

Thank you. But I think #!/bin/sh is more compatible. Please check : https://superuser.com/questions/1133187/when-must-i-use-bin-bash-and-when-bin-sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.