I would like to know how to delete a commit.
By delete
, I mean it is as if I didn\'t make that commit, and when I do a push in the future, my changes wi
If you've already pushed, first find the commit you want to be at HEAD ($GIT_COMMIT_HASH_HERE), then run the following:
git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force
Then each place the repo has been cloned, run:
git reset --hard origin/master
git reset --hard commitId
git push <origin> <branch> --force
PS: CommitId refers the one which you want to revert back to
If you want to keep the history, showing the commit and the revert, you should use:
git revert GIT_COMMIT_HASH
enter the message explaining why are you reverting and then:
git push
When you issue git log
you'll see both the "wrong" commit and revert log messages.
I'm appending this answer because I don't see why anyone who has just tried to commit work would want to delete all that work because of some mistake using Git!
If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):
git reset --soft HEAD~1
Do not use the --hard flag unless you want to destroy your work in progress since the last commit.
If you just messed up your last commit (wrong message, forgot to add some changes) and want to fix it before pushing it to a public repo why not use:
git commit --amend -m "New message here"
If you have newly staged changes they'll be combined with the last commit (that you're trying to get rid of) and will replace that commit.
Of course if you amend a commit after you've pushed it, you're rewriting history so if you do that be sure to understand the implications.
You can also pass the '--no-edit' option instead of '-m' if you would prefer to use the previous commit's message.
Docs: http://git-scm.com/docs/git-commit.html
If you have not yet pushed the commit anywhere, you can use git rebase -i to remove that commit. First, find out how far back that commit is (approximately). Then do:
git rebase -i HEAD~N
The ~N
means rebase the last N
commits (N
must be a number, for example HEAD~10
). Then, you can edit the file that Git presents to you to delete the offending commit. On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.
The Git Book has a good section on rebasing with pictures and examples.
Be careful with this though, because if you change something that you have pushed elsewhere, another approach will be needed unless you are planning to do a force push.