To just move HEAD (as asked - this doesn't update the index or working tree), use:
git reset --soft $(git child)
You'll need to use the configuration listed below.
Explanation
Based on @Michael's answer, I hacked up the child
alias in my .gitconfig
.
It works as expected in the default case, and is also versatile.
# Get the child commit of the current commit.
# Use $1 instead of 'HEAD' if given. Use $2 instead of curent branch if given.
child = "!bash -c 'git log --format=%H --reverse --ancestry-path ${1:-HEAD}..${2:\"$(git rev-parse --abbrev-ref HEAD)\"} | head -1' -"
It defaults to giving the child of HEAD (unless another commit-ish argument is given) by following the ancestry one step toward the tip of the current branch (unless another commit-ish is given as second argument).
Use %h
instead of %H
if you want the short hash form.
With a detached head, there is no branch, but getting the first child can still be achieved with this alias:
# For the current (or specified) commit-ish, get the all children, print the first child
children = "!bash -c 'c=${1:-HEAD}; set -- $(git rev-list --all --not \"$c\"^@ --children | grep $(git rev-parse \"$c\") ); shift; echo $1' -"
Change the $1
to $*
to print all the children