Referencing the child of a commit in Git

后端 未结 10 1148
失恋的感觉
失恋的感觉 2020-11-28 08:37

If you want to move the HEAD to the parent of the current HEAD, that\'s easy:

git reset --hard HEAD^

But is there

10条回答
  •  余生分开走
    2020-11-28 09:24

    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

提交回复
热议问题