Zygro Zygro - 2 months ago 8
Git Question

How to overwrite a commit with detached HEAD commit?

I have a branch that looks like this

---A-----------B
\
\
C (Detached HEAD)


I got it by this process:

git checkout branch
git checkout HEAD~
git commit -m "C"


and I want
C
to overwrite
B
(discard all changes in B)

Best would be without merging temp branch because I want the branch graph to look as simple as possible

Answer

Force delete the branch and create branch on the C commit. Do force push if branch is in any remote repository.

If you can repeate the commit (i.e. you have a diff saved somewhere), you can reset branch's HEAD to HEAD~ and the do the commit.