I accidentally committed to the wrong branch. How do I delete that commit?
+2
A:
delete the most recent commit:
git reset --hard HEAD~1
Delete the most recent commit, without destroying the work you've done:
git reset --soft HEAD~1
dbyrne
2010-07-07 17:50:19
make sure HEAD is pointing at the branch.. (check it out first)
Frank Schwieterman
2010-07-07 17:52:54
And make sure HEAD~1 is the commit... You could also do `git reset --hard origin`
Daenyth
2010-07-07 17:53:46
A:
Do a git rebase -i FAR_ENOUGH_BACK
and drop the line for the commit you don't want.
Hank Gay
2010-07-07 17:55:13
A:
If you want to move that commit to another branch, get the SHA of the commit in question
git rev-parse HEAD
Then switch the current branch
git checkout other-branch
And cherry-pick
the commit to other-branch
git cherry-pick <sha-of-the-commit>
Alexander Groß
2010-07-07 17:55:27
+2
A:
Don't delete it: for just one commit git cherry-pick
is enough.
But if you had several commits on the wrong branch, that is where git rebase --onto
shines:
Suppose you have this:
x--x--x--x <-- master
\
-y--y--m--m <- y branch, with commits which should have been on master
, then you can mark master
and move it where you would want to be:
git checkout master
git branch tmp
git checkout y
git branch -f master
x--x--x--x <-- tmp
\
-y--y--m--m <- y branch, master branch
, reset y branch where it should have been:
git checkout y
git reset --hard HEAD~2 # ~1 in your case,
# or ~n, n = number of commits to cancel
x--x--x--x <-- tmp
\
-y--y--m--m <- master branch
^
|
-- y branch
, and finally move your commits (reapply them, making actually new commits)
git rebase --onto tmp y master
git branch -D tmp
x--x--x--x--m'--m' <-- master
\
-y--y <- y branch
VonC
2010-07-07 17:58:27