0

Let's say that I have a repo where A is the initial commit, where HEAD points to commit B, and where branch "foo-branch" points to commit C:

              A
              |
      HEAD -> B
              |
foo-branch -> C
              |
              D

I know I can get foo-branch to point to commit A (two parents up), via relative ref to HEAD, by running

git branch -f foo-branch HEAD~1

...but let's say that instead of going to a parent commit, I want to go to the child commit of foo-branch, commit D. How do I do this using a relative ref to HEAD?

As a note, in a purely fictional world, if the # token could be used to reference the opposite of ~, I could run

git branch -f foo-branch HEAD#2

and it would point foo-branch to commit D. Purely fictional, but it gets my point across.

P.S. - I know that there could be issues if children of commit C branch anywhere down the chain, but for simplicity's sake, let's not incorporate branches into this question.

j08691
  • 204,283
  • 31
  • 260
  • 272
JCollier
  • 1,102
  • 2
  • 19
  • 31
  • What if there is a commit E on a branch paralell to D branching out from C? which one would it choose? You could investigate possible solutions [here](https://stackoverflow.com/questions/2263674/how-do-i-find-the-next-commit-in-git-child-children-of-ref) – fredrik Oct 03 '19 at 20:32
  • See [this answer](https://stackoverflow.com/a/58226704/1256452) which I started on before thinking about looking for duplicates. – torek Oct 03 '19 at 20:50

0 Answers0