How do I delete a commit from my branch history? Should I use git reset --hard HEAD?
42 Answers
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...
git reset --hard HEAD~1
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:
git reset --hard <sha1-commit-id>
If you already pushed it, you will need to do a force push to get rid of it...
git push origin HEAD --force
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.
git stash does the same except you can restore it later if you need, versus permanently delete with reset hard mode. Check your stashes by using git stash list and git stash show 'stash@123'
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.
19 Comments
HEAD~1 or just HEAD^. If you pushed, you should use git revert instead.HEAD~n to "go back" n commits from your head. Maybe from this point you can interpreted ... --hard HEAD also as HEAD~0 => deleting work in progress.reset --hard, and check the log --oneline --all, the commits still remain in the tree. How do we delete these commits from tree? Thanks.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.
9 Comments
push -f to force the push and replace the remote branch with your local one. If it's just your own remote repo, no problem. The trouble starts if somebody else has fetched in the meantime.-i and -p, but if you do, "Editing commits and rewording their commit messages should work fine, but attempts to reorder commits tend to produce counterintuitive results." I'm not sure how well deleting commits would work, but you could try it and then do a reset --hard to before the rebase (using the reflog) if it doesn't work out properly.rebase -i, the changes corresponding to the deleted commit are not preserved.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.
13 Comments
git rebase --continuegit rebase -i HEAD~1git rebase -i HEAD~1 really cleaned the repo up a lot! It's hard to tell exactly what it did, but the whole thing looks a lot neater. A little alarming, actually.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.
10 Comments
git reset --hard HEAD~1 your previous latest commit would be available via reflog (until you expire it); see also here: gitready.com/intermediate/2009/02/09/…Removing an entire commit
git rebase -p --onto SHA^ SHA
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#change_deep
Mind that -p has been deprecated, write --rebase-merges instead, see the remark below the answer and the link at git-rebase - Reapply commits on top of another base tip ==> --preserve-merges:
-p
--preserve-merges [DEPRECATED: use --rebase-merges instead] Recreate merge commits instead of flattening the history by replaying commits a merge commit introduces. Merge conflict resolutions or manual amendments to merge commits are not preserved.This uses the --interactive machinery internally, but combining it with the --interactive option explicitly is generally not a good idea unless you know what you are doing (see BUGS below).
See also INCOMPATIBLE OPTIONS below.
And the new parameter git-rebase - Reapply commits on top of another base tip ==> --rebase-merges[=(rebase-cousins|no-rebase-cousins)]:
-r
--rebase-merges[=(rebase-cousins|no-rebase-cousins)] By default, a rebase will simply drop merge commits from the todo list, and put the rebased commits into a single, linear branch. With --rebase-merges, the rebase will instead try to preserve the branching structure within the commits that are to be rebased, by recreating the merge commits. Any resolved merge conflicts or manual amendments in these merge commits will have to be resolved/re-applied manually.By default, or when no-rebase-cousins was specified, commits which do not have as direct ancestor will keep their original branch point, i.e. commits that would be excluded by git-log[1]'s --ancestry-path option will keep their original ancestry by default. If the rebase-cousins mode is turned on, such commits are instead rebased onto (or , if specified).
The --rebase-merges mode is similar in spirit to the deprecated --preserve-merges but works with interactive rebases, where commits can be reordered, inserted and dropped at will.
It is currently only possible to recreate the merge commits using the recursive merge strategy; Different merge strategies can be used only via explicit exec git merge -s [...] commands.
See also REBASING MERGES and INCOMPATIBLE OPTIONS below.
10 Comments
-p, --preserve-merges Recreate merge commits instead of flattening the history by replaying commits a merge commit introduces. Merge conflict resolutions or manual amendments to merge commits are not preserved.-p [DEPRECATED: use --rebase-merges instead]Say we want to remove commits 2 & 4 from the repo. (Higher the the number newer the commit; 0 is the oldest commit and 4 is the latest commit)
commit 0 : b3d92c5
commit 1 : 2c6a45b
commit 2 : <any_hash>
commit 3 : 77b9b82
commit 4 : <any_hash>
Note: You need to have admin rights over the repo since you are using --hard and -f.
git checkout b3d92c5Checkout the last usable commit.git checkout -b repairCreate a new branch to work on.git cherry-pick 77b9b82Run through commit 3.git cherry-pick 2c6a45bRun through commit 1.git checkout masterCheckout master.git reset --hard b3d92c5Reset master to last usable commit.git merge repairMerge our new branch onto master.git push -f origin masterPush master to the remote repo.
9 Comments
git push -f origin master there is no option --hardcommit 0 is older than commit 1. Please could you tell me why first run through commit 3 (by cherry-pick) and then by commit 1? After checkout of b3d92cd (commit 0) I would expect cherry-pick commit 1, then commit 3. Thanks.commit 0 is the newest commit, commit 4 is the oldest. Start by checking out commit 5, just before the commit that you don't want. Then cherry-pick commit 3, then cherry-pick commit 1, then do it for commit 0. Then checkout master and reset it to commit 5 and follow the rest of the steps.repair branch and combine commits before merging back in to master. The only comment I have is that you can consolidate the first two steps: git checkout -b repair b3d92c5git rebase -i HEAD~2
Here '2' is the number of commits you want to rebase.
'git rebase -i HEAD`
if you want to rebase all the commits.
Then you will be able to choose one of these options.
p, pick = use commit
r, reword = use commit, but edit the commit message
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message
x, exec = run command (the rest of the line) using shell
d, drop = remove commit
These lines can be re-ordered; they are executed from top to bottom. If you remove a line here THAT COMMIT WILL BE LOST. However, if you remove everything, the rebase will be aborted. Note that empty commits are commented out
You can simply remove that commit using option "d" or Removing a line that has your commit.
2 Comments
git rebase -i HEAD does nothing, not "rebase all commits".You have many alternatives, for example:
Alternative 1:
git rebase -i <YourCommitId>~1Change YourCommitId for the number of the commit which you want to revert back to.
Alternative 2:
git reset --hard YourCommitId git push <origin> <branch> --forceChange YourCommitId for the number of the commit which you want to revert back to.
I don't recommend this option because you may lose your in progress work.
Alternative 3:
git reset --soft HEAD~1You can keep your work and only undo the commit.
1 Comment
git reset --soft HEAD~1 worked perfectly for me. Makes sure I don't lost to changes from that commit. Thank you for your clear answer.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.
3 Comments
drop keyword is not defined. To delete a commit just remove the whole line.pick with drop did delete the commit. Note: the instructions are in the remarked-out section in the bottom portion of the text editor's contents. Using git version 2.24.3 (Apple Git-128)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
5 Comments
tree = log --all --graph --format=format:'%C(bold blue)%h%C(reset) %C(dim black)%s%C(reset)%C(bold red)%d%C(reset) %C(green)by %an, %ar%C(reset)'git reset --hard HEAD^) you "remove" commit only from current branch (actually move branch pointer), if there was some other branch this commit was on it would still be there. "Removing" commit with git reset just moves back branch pointer, making commit dangling and available for pruning... if not referenced by other ref (other branch, remote-tracking branch, or tag). Or did you use git revert HEAD?git reset --hard commitId
git push <origin> <branch> --force
PS: CommitId refers the one which you want to revert back to
1 Comment
If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:
git reset HEAD~1
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:
git reset HEAD~N
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.
git reset --hard HEAD~1
Likewise, if you want to discard the last N commits, and do not want to keep the code changes:
git reset --hard HEAD~N
1 Comment
If you want to delete the commit you are currently on but don't want to delete the data-
git reset --soft HEAD~1
If you want to delete the commit you are currently on and want to delete the related data-
git reset --hard HEAD~1
PS: these commands will only delete the local commits.
1 Comment
If you're an IntelliJ user, the interface is simply awesome. With a single click, you can see the effect immediately at the same time. Shortcut to this place: Cmd + 9 on MacOS.
2 Comments
Source: https://gist.github.com/sagarjethi/c07723b2f4fa74ad8bdf229166cf79d8
Delete the last commit
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
Comments
Here I just post one clear pipeline to do so
Step1: Use git log to get the commit ID.
git log
Step2: Use git reset to go back to the former version:
git reset --hard <your commit id>
1 Comment
git log origin/master.. to get unpushed commit. git reset --hard <sha1-commit-id> to sit on the commit, get reset --hard HEAD~1 to delete the 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.
2 Comments
git prune is actually one of the "porcelain" commands. Also, it is rare that you would want to completely clear out your reflog (one use case is to remove sensitive info from your repo, but like I said, that's a rare use case). More often than not, you'll want to keep old commits around in the reflog, in case you need to recover data. See Pro Git: 9.7 Git Internals - Maintenance and Data Recovery.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.
1 Comment
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.
Comments
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.
Comments
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.
1 Comment
git reset --hard HEAD~1
You will be now at previous head. Pull the branch. Push new code. Commit will be removed from git
1 Comment
// display git commit log
$ git log --pretty=oneline --abbrev-commit
// show last two commit and open in your default editor
// then delete second commit line and save it
$ git rebase -i HEAD~2


cherry-pickanddeletea single commit that may occurred a while ago.git rebase -i HEAD~10does address the question, as it does let you arbitrarily pick commits to delete. Git applies the commits in the range you specify one-by-one, ignoring commits you have removed from the log. I used this command today to get rid of the second and third most recent commits to my repo while keeping the top one. I agree that none of the other answers are satisfactory.git reset --soft HEAD~1is exactly what you need. In such case you will undo commit and save your work.reset --hardwill remove commit completely.