Referencing the child of a commit in Git

后端 未结 10 1119
失恋的感觉
失恋的感觉 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:01

    The above method using git rev-list --all considers all available commits, which can be a lot and is often not necessary. If the interesting child commits are reachable from some branch, the number of commits that a script interested in child commits needs to process can be reduced:

    branches=$(git branch --contains $commit| grep -v '[*] ('| sed -e 's+^..++')
    

    will determine the set of branches that $commit is an ancestor of. With a modern git, at least version 2.21+, this should do the same without the need for sed (untested):

    branches=$(git branch --format='%(refname:short)' --contains $commit| grep -v '[*] (')
    

    Using this set, git rev-list --parents ^$commit $branches should yield exactly the set of all parent-child relationships between $commit and all branch heads that it is an ancestor of.

提交回复
热议问题