Question

I somehow did this:

A -- B -- C (init)
         /
        Z   (master)

I can't figure out how to delete the Z commit or ho to "rebase" it as a parent of A. Is that even possible?

Was it helpful?

Solution

If there is nothing of use in Z you can get rid of it with reset --hard

git reset --hard init (if init is a branch)

or

git reset --hard HEAD^ (moves one commit back)

If you need to keep Z but as early as possible (first possible is after A) then you can do an interactiv rebase

git rebase -i <sha of A>

and reorder de commits as

Z
B
C

(A is not show as it's the root commit)

OTHER TIPS

Why you need to delete it? Just revert that commit.

git revert <SHA-ID>

Or else, You want to nuke commit. The hard reset to HEAD-1 will set your working copy to the state of the commit before your wrong commit.

git reset --hard HEAD~1 

To make Z commit as the parent of A try this:

git checkout -b b_branch {B hash}
git checkout master
git rebase b_branch
git checkout -b a_branch {A hash}
git rebase master
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top