How do I delete unpushed git commits?
Delete the most recent commit, keeping the work you've done:
git reset --soft HEAD~1
Delete the most recent commit, destroying the work you've done:
git reset --hard HEAD~1
Do a git rebase -i FAR_ENOUGH_BACK
and drop the line for the commit you don't want.
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
I wonder why the best answer that I've found is only in the comments! (by Daenyth with 86 up votes)
git reset --hard origin
This command will sync the local repository with the remote repository getting rid of every change you have made on your local. You can also do the following to fetch the exact branch that you have in the origin as Cleary suggested in the comments.
git reset --hard origin/<branch>