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
Another possibility is one of my personal favorite commands:
git rebase -i <commit>~1
This will start the rebase in interactive mode -i
at the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.
I have already pushed. Need to return some commits back remotly. Have tried many variations, but only this from Justin via git bush is working fine for me:
git reset --hard $GIT_COMMIT_HASH_HERE
git push origin HEAD --force
git reset --hard HEAD~1
You will be now at previous head. Pull the branch. Push new code. Commit will be removed from git
If you didn't publish changes, to remove latest commit, you can do
$ git reset --hard HEAD^
(note that this would also remove all uncommitted changes; use with care).
If you already published to-be-deleted commit, use git revert
$ git revert HEAD
You have many alternatives, for example:
Alternative 1:
git rebase -i <YourCommitId>~1
Change YourCommitId for the number of the commit which you want to revert back to.
Alternative 2:
git reset --hard YourCommitId
git push <origin> <branch> --force
Change YourCommitId for the number of the commit which you want to revert back to.
I don't recommend this option because you can lost your work in progress.
Alternative 3:
git reset --soft HEAD~1
You can keep your work and only undo the commit.
Assuming you have not pushed to the remote repository, you could re-clone the repository. This has been my method of choice a few times.