Delete commits from a branch in Git


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.


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?

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

To delete in local branch, use

git reset --hard HEAD~1

To delete in a remote branch, use

git push origin HEAD --force

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 \

This will erase all commits after the current one in your local repository but only for that one branch.

Take backup of your code in to temp folder. Following command will reset same as server.

git reset --hard HEAD
git clean -f
git pull

Then copy the required files alone from temp folder, then commit

git 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.

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.


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.

What I do usually when I commit and push (if anyone pushed his commit this solve the problem):

git reset --hard HEAD~1

git push -f origin

hope this help

I'm appending this answer because 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.