Remove a git commit which has not been pushed

GitGit Commit

Git Problem Overview


I did a git commit but I have not pushed it to the repository yet. So when I do git status, I get '# Your branch is ahead of 'master' by 1 commit.

So if I want to roll back my top commit, can I just do:

git reset --hard eb27bf26dd18c5a34e0e82b929e0d74cfcaab316

given that when I do git log I get:

commit eb27bf26dd18c5a34e0e82b929e0d74cfcaab316
Date:   Tue Sep 29 11:21:41 2009 -0700

commit db0c078d5286b837532ff5e276dcf91885df2296 Date: Tue Sep 22 10:31:37 2009 -0700

Git Solutions


Solution 1 - Git

IF you have NOT pushed your changes to remote

git reset HEAD~1

Check if the working copy is clean by git status.

ELSE you have pushed your changes to remote

git revert HEAD

This command will revert/remove the local commits/change and then you can push

Solution 2 - Git

Actually, when you use git reset, you should refer to the commit that you are resetting to; so you would want the db0c078 commit, probably.

An easier version would be git reset --hard HEAD^, to reset to the previous commit before the current head; that way you don't have to be copying around commit IDs.

Beware when you do any git reset --hard, as you can lose any uncommitted changes you have. You might want to check git status to make sure your working copy is clean, or that you do want to blow away any changes that are there.

In addition, instead of HEAD you can use origin/master as reference, as suggested by @bdonlan in the comments: git reset --hard origin/master

Solution 3 - Git

git reset --hard origin/main

It works for other branch:

git reset --hard origin/master
git reset --hard origin/staging

to reset it to whatever the origin was at.

This was posted by @bdonlan in the comments. I added this answer for people who don't read comments.

Solution 4 - Git

I believe that one of those will fit your need

1 - Undo commit and keep all files staged: git reset --soft HEAD~

2 - Undo commit and unstage all files: git reset HEAD~

3 - Undo the commit and completely remove all changes: git reset --hard HEAD~

here is were I found the answer

Solution 5 - Git

There are two branches to this question (Rolling back a commit does not mean I want to lose all my local changes):

1. To revert the latest commit and discard changes in the committed file do:

git reset --hard HEAD~1

2. To revert the latest commit but retain the local changes (on disk) do:

git reset --soft HEAD~1

This (the later command) will take you to the state you would have been if you did git add.

If you want to unstage the files after that, do

git reset

Now you can make more changes before adding and then committing again.

Solution 6 - Git

Simply type in the console :

$ git reset HEAD~

This command discards all local commits ahead of the remote HEAD

Solution 7 - Git

Remove the last commit before push

git reset --soft HEAD~1

1 means the last commit, if you want to remove two last use 2, and so forth*

Solution 8 - Git

I have experienced the same situation I did the below as this much easier. By passing commit-Id you can reach to the particular commit you want to go:

git reset --hard {commit-id}

As you want to remove your last commit so you need to pass the commit-Id where you need to move your pointer:

git reset --hard db0c078d5286b837532ff5e276dcf91885df2296

Solution 9 - Git

This is what I do:

First checkout your branch (for my case master branch):

git checkout master

Then reset to remote HEAD^ (it'll remove all your local changes), force clean and pull:

git reset HEAD^ --hard && git clean -df && git pull

Solution 10 - Git

One way would be to delete the local branch and checkout that branch from the server if your local branch is ahead of remote by multiple commits and you need to uncommit all of them.

Solution 11 - Git

I just had the same problem and ended up doing:

git rebase -i HEAD~N

(N is the number of commits git will show you)

That prompts your text editor and then you can remove the commit you want by deleting the line associated with it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhap497View Question on Stackoverflow
Solution 1 - GitJeril KuruvilaView Answer on Stackoverflow
Solution 2 - GitBrian CampbellView Answer on Stackoverflow
Solution 3 - GitShadowebView Answer on Stackoverflow
Solution 4 - GitM. MassulaView Answer on Stackoverflow
Solution 5 - GitpapigeeView Answer on Stackoverflow
Solution 6 - GitmarcdahanView Answer on Stackoverflow
Solution 7 - GitKarina HaddadView Answer on Stackoverflow
Solution 8 - GitKamlesh PatidarView Answer on Stackoverflow
Solution 9 - GitAdnanView Answer on Stackoverflow
Solution 10 - Gitsaga123View Answer on Stackoverflow
Solution 11 - GitMrCasView Answer on Stackoverflow