I notice that one can specify ancestors of actual commit by using the ^ or the ~ character. For example, if I have the following log of commits
* 1990f31 Somme third commit message
* k135145 Somme second commit message
* 2c13521 Somme first commit message
then I can know which is the parent commit of 1990f31 with HEAD^1 (or equivalently with HEAD~1), which is k135145.
Now, I also read that one can combine ^ or ~ when it is a merge commit. Therefore, could someone provide an example to illustrate commit references when typing git show HEAD~n^k or alternatively by commuting this operators like git show HEAD^k~n?, where I'm considering n and k as abstractions to represent a positive integer.
~and^are operators which you can use to designate any ancestor within a commit's history.Let us start with a diagram :
~will go up the chain of first parents :aa~isbb,aa~2isgg,aa~3ishhcc~isdd,cc~2isff,cc~3is alsohh~alone, and starting fromaa, you will not be able to explore thesome/featurebranch : at the merge point (bb),ccis the second parent ofbb~nis a shortcut to~~...~repeated n times, eg :aa~3is the same asaa~~~^will only go up one level, but allows you to inspect then-thparent for commits that have several parentsxx^is the same asxx~: in both case, it is the first parent ofxxaais a regular commit, with one single parent: soaa^isbb(same asaa~),aa^2does not existbbis a merge commit, with 2 parents:bb^isgg(same asbb~),bb^2iscc,bb^3does not exist^nis not a shortcut to^^...^, e.g :bb^2is the second parent ofbb(it iscc)bb^^is the first parent of the first parent ofbb(it ishh)In some cases, you can have octopus merge commits, which may have
xx^3,xx^4... parents.By combining the operators, you can "navigate" to any ancestor commit.
For example, starting from
aa, one way to reachffis :In general, you can't switch
~and^sequences, because they would not point at the same commit.For example :
Anything that point to a commit can take the palce of
aa,bborccabove :HEAD, asha1,some/branch,some/tag...stash@{0}(items in the stash),HEAD@{3}(elements in the reflog ofHEAD),master@{2}(elements in the reflog of themasterbranch)