2

When I have a detached HEAD that's several commits ahead of an existing branch, what's the right command(s) to advance that branch to the detached HEAD without changing anything in my working directory?

Context: I just ran git rebase --onto mybranch SHA1 SHA2, and now I've got a detached HEAD at REBASED-SHA2. Now I want to advance mybranch to REBASED-SHA2. I could create a new branch at REBASED-SHA2 (git checkout -b temp), delete the old branch (git branch -d mybranch), and rename the new branch (git branch -m temp mybranch). But that's a lot of typing, and might require even more typing if I had to set up tracking to the remote mybranch.

Is there a shorter way to do what I want, which is just to "advance" mybranch to a later commit?

Justin Grant
  • 44,807
  • 15
  • 124
  • 208
  • 1
    `git checkout mybranch && git merge --ff-only SHA2` – Guildenstern Apr 26 '23 at 21:19
  • @Guildenstern - Ahh, yeah. I should have thought of `merge` for this case. For some reason I was thinking that merge only works for branches, but of course it works for SHAs too. Thanks! BTW, I had a mistake in my question (that I just edited to fix) because the detached head won't be at SHA2, it'll be at a new commit that resulted from rebasing SHA2 on top of `mybranch`. If you want to turn your comment into an answer (with the correct target of REBASED-SHA2) then I'll happily accept it! – Justin Grant Apr 26 '23 at 22:32

1 Answers1

1

git checkout -B mybranch moves the mybranch tip label to (or creates it at) your current checkout, because since no specific target commit was given @ aka HEAD is the default.

jthill
  • 55,082
  • 5
  • 77
  • 137