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?
use git revert https://git-scm.com/docs/git-revert .It will revert all code then you can do next commit.Then head will point to that last commit. reverted commits never delete but it will not affect on you last commit.
If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:
This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.
If you want to uncommit N commits, but keep the code changes in your working directory:
If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.
Likewise, if you want to discard the last N commits, and do not want to keep the code changes:
Removing an entire commit
Obviously replace "SHA" with the reference you want to get rid of. The "^" in that command is literal.
http://sethrobertson.github.io/GitFixUm/fixup.html
PS: CommitId refers the one which you want to revert back to
The mistake:
I
git rebase -i --root
'ed my branch, ignorantly thinking I could reword the first commit differing from the master (the GitHub for Windows default view is the comparison to master, hiding it's entirety).I grew a Silicon Valley beard while 900+ commits loaded themselves into Sublime. Exiting with no changes, I charged my battery then proceeded to shave, as all 900+ individual commits nonchalantly rebased - resetting their commit times to now.
Determined to beat Git and preserve the original times, I deleted this local repository and re-cloned from the remote.
Now it had re-added a most recent unneeded commit to master I wished to remove, so proceeded like so.
Exhausting the options:
I didn't wish to
git revert
- it would create an additional commit, giving Git the upper hand.git reset --hard HEAD
did nothing, after checking thereflog
, the last and onlyHEAD
was the clone - Git wins.To get the most recent SHA, I checked the remote repository on github.com - minor win.
After thinking
git reset --hard <SHA>
had worked, I updated another branch to master and 1... 2... poof! the commit was back - Git wins.Checking back out to master, time to try
git rebase -i <SHA>
, then remove the line... to no avail, sad to say. "If you remove a line here THAT COMMIT WILL BE LOST". Ah...glossed over new feature troll the n00b in the 2.8.3 release notes.The solution:
git rebase -i <SHA>
thend, drop = remove commit
.To verify, I checked out to another branch, and voila - no hiding commit to fetch/pull from the master.
https://twitter.com/holman/status/706006896273063936
Good day to you.
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:
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