KcFnMi KcFnMi - 1 month ago 5
Git Question

Move commit made in detached HEAD back to branch, but not on top


  1. I was on
    current
    branch and did commit
    good
    .

  2. Then I commited
    irrelevant
    .

  3. I went back, checked out
    good
    (detached HEAD now).

  4. I did commit
    one
    , in detached HEAD state.



I would like to have some help to put
one
just after
good
, in
current
branch.

It doesn't matter if I lost
irrelevant
.

Answer
  1. I went back, checked out good (detached HEAD now).
  2. I did commit one, in detached HEAD state.

I would like to have some help to put one just after good.

one is after good. git commit updates HEAD, every time, no matter what. When you check out a branch tip, git sets the HEAD reference as an alias for that branch tip reference, it "attaches" it to that branch tip reference. A detached HEAD is just a reference directly to a particular commit, so when git commit updates it, HEAD now refers to the new one. But git commit always adds the old HEAD commit as the parent of the new one.

So just

git checkout -B current one               # if you don't still have `one` checked out

or just

git checkout -B current                   # if you do
Comments