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 will not push to the remote branch.
I read git help, and I think the command I should use is git reset --hard HEAD
. Is this correct?
Another possibility is one of my personal favorite commands:
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.All the commands above restore the state of your work tree and index as they were before making the commit, but do not restore the state of the repository. If you look at it, the "removed" commit is not actually removed, it is simply not the one on the tip of the current branch.
I think that there are no means to remove a commit with porcelain commands. The only way is to remove it from the log and reflog and then to execute a
git prune --expire -now
.As you can see on above image i want to delete revert"test change 2" commit(SHA1 ID: 015b5220c50e3dfbb1063f23789d92ae1d3481a2(you can get SHA1 ID by using
gitk
command in git bash)).For that i can use(all below command work on local only. you need to push after delete):
git reset --hard 515b5220c50e3dfbb1063f23789d92ae1d3481a2
//it back-up you to that commit (SHA1 ID of test change 4 commit is 515b5220c50e3dfbb1063f23789d92ae1d3481a2)git reset --hard HEAD~1
// it back-up you before one commit.git reset --hard HEAD^
// To remove the last commit from gitafter delete:
Careful:
git reset --hard
WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.Assuming you are sitting on that commit, then this command will wack it...
The
HEAD~1
means the commit before head.Or, you could look at the output of
git log
, find the commit id of the commit you want to back up to, and then do this:If you already pushed it, you will need to do a force push to get rid of it...
However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.
If you already pushed, it may be better to use
git revert
, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.FYI --
git reset --hard HEAD
is great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.Lastly, if you need to find a commit that you "deleted", it is typically present in
git reflog
unless you have garbage collected your repository.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.
git reset --hard
git push origin HEAD --force
If one or more of the commits is tagged, delete the tag(s) first. Otherwise the tagged commit is not removed.