Skip to content

Instantly share code, notes, and snippets.

@hutusi
Last active January 1, 2024 15:56
Show Gist options
  • Star 68 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save hutusi/e4f32e2bcd8d53ec86de8254ab0d5127 to your computer and use it in GitHub Desktop.
Save hutusi/e4f32e2bcd8d53ec86de8254ab0d5127 to your computer and use it in GitHub Desktop.
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.

Please check hutusi/git-paging for updates.

#!/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
Copy link
Author

hutusi 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

@cleverlzc
Copy link

nice

@Sanaki
Copy link

Sanaki commented Jul 5, 2020

This is lovely, though I'd prefer if the four scripts came as 744/755 rather than 644. Saves a step when updating that way.

@hutusi
Copy link
Author

hutusi commented Sep 25, 2020

This is lovely, though I'd prefer if the four scripts came as 744/755 rather than 644. Saves a step when updating that way.

Thanks @Sanaki , I have fixed that.

@hutusi
Copy link
Author

hutusi commented Oct 16, 2020

Please check https://github.com/hutusi/git-paging for updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment