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
Forcefully Change History
Assuming you don't just want to delete the last commit, but you want to delete specific commits of the last n commits, go with:
git rebase -i HEAD~<number of commits to go back>
, so git rebase -i HEAD~5
if you want to see the last five commits.
Then in the text editor change the word pick
to drop
next to every commit you would like to remove. Save and quit the editor. Voila!
Additively Change History
Try git revert <commit hash>
. Revert will create a new commit that undoes the specified commit.
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
.
Source: https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8
For example your last commit
git push origin +aa61ab32^:master
Now you want to delete this commit then an Easy way to do this following
Steps
First reset the branch to the parent of the current commit
Force-push it to the remote.
git reset HEAD^ --hard git push origin -f
For particular commit, you want to reset is following
git reset bb676878^ --hard
git push origin -f
Here's another way to do this:
Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit". I think the command line is:
git reset --hard COMMIT_ID
Since you just checked out your branch from remote, you're not going to have any local changes to worry about losing. But this would lose them if you did.
Then navigate to your repository's local directory and run this command:
git -c diff.mnemonicprefix=false -c core.quotepath=false \
push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME
This will erase all commits after the current one in your local repository but only for that one branch.
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 the reflog
, the last and only HEAD
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>
then d, 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.
To delete in local branch, use
git reset --hard HEAD~1
To delete in a remote branch, use
git push origin HEAD --force