1567

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
4

14 Answers 14

1997

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

Sign up to request clarification or add additional context in comments.

10 Comments

This answers to "Remove the latest git commit which has not been pushed" (closest answer IMHO)
Also, it keeps the local changes done in the last commit, while git reset --hard does not
for me, it will undo the last pushed commit and unpushed commit
git revert HEAD for me just deleted all files that I had ready to push. Be careful!
How would I undo git reset HEAD~1?
|
861

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

9 Comments

Or git reset --hard origin/master, to reset it to whatever the origin was at.
Another useful pointer you can reset to is ORIG_HEAD or its generalization utilizing reflog HEAD@{1} (the last position of HEAD).
Reader, before you git reset your code. Do your future self a favor: The difference between reset, reset --soft, and reset --hard (What happens to your earlier git add aka "your work" :) Picture: link
Note that this will delete whatever was in the commit you want removed (so a git status will show no changes and the commit will be removed / changes lost).
NOTE: Be careful when using git reset --hard HEAD^ as you will lose changes in the commited files (you changes will be deleted). There are two branches to this question: To revert a commit but still have changes on disk do so git reset --soft HEAD~1
|
411

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

4 Comments

If you are working in a custom branch: git reset --soft origin/{Branch}
NOTE git reset HEAD~ remove all files you've created
NOTE #2: the first option restores the changes of your undone commit back into your staged changes.
NOTE #3: git reset HEAD~ will return you to still have the "untracked files" you had before adding and committing them. So if you accidentally do "git add ." and "git commit" and mean it to be on a few files whose changes you want but forget that you have a bunch of artifacts that you forgot to delete, you do git reset HEAD~ and this takes you back to being able to delete the artifacts (untracked files) and then re-add and commit your changes, which will be right there.
325
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.

5 Comments

This question was asked nearly 5 years ago, and this is already in one of the comments.
What if current local branch differs from master - should I use origin/mybranch then?
I don't usually read the comments when I'm in a hurry for an answer... Thanks for putting as an answer (a simple one that works)
Warning: be aware that this will remove all your existing code changes, other than git reset HEAD~ which only sets back the commit into code
you should explain that --hard totally destroys all the changes. That's not what everybody wants.
129

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.

Comments

68

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*

3 Comments

Hi Karina, the mention of --soft is great addition to the possible answers, but could you also mention why? Explaining your solution is always helpful. Also try to stick with English. Thanks
--soft - ensures that you do not lose changes in the file whose commit you are trying to undo
Thank you! This is what I'm looking for, because I've made a commit in a wrong branch and do not want to lose changes in the files, but only commit and push in a different branch. Thank you again.
65

Simply type in the console :

$ git reset HEAD~

This command discards all local commits ahead of the remote HEAD

4 Comments

Can you explain what your answer adds over the existing answers?
This clears the current commit prior to pushing - without reverting changes you've made locally - like the other answers - which could be a severe hair pullout moment
Short and sweet; I like it! We need more answers like this, that's what this 'adds' over the existing answers. Subtracts might be a better explanation of this answers value. I hope no poor soul used any of the --hard noise.
Exactly what I needed, thanks for the clear solution!
24

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

2 Comments

Just a warning for newbies like myself - back up any files where you want to KEEP the changes, as they will be reverted to the earlier version. My scenario was that I accidentally put a database dump in my repo directory and then committed - I obviously didn't want to commit those files to the repo but DID want to keep changes I had made to other files. So I had to copy and paste the required changes from the backup I made BEFORE doing the git reset.
git reset {commit-id} or git reset {commit-id} --mixed preserves the changes. They can then be added back to staging. git reset {commit-id} --mixed preserves the changes too, but also keeps the files in staging so they don't have to be added back.
12

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

1 Comment

It is very nuclear option. I would suggest soft reset to limited commit.
4

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.

1 Comment

brilliant answer - it allows to easy click it in sourcetree :)
3

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.

Comments

2

Use "git log" to see the commits.

git reset HEAD~: resets the current HEAD to the commit just before the current HEAD commit.

git reset HEAD~1: same as above.

git reset HEAD~2: resets the current HEAD to two commits just before the current HEAD commit.

Comments

-2

To delete folder from commit

git rm -r --cache <folder name>

To delete file from commit

git rm --cache <file name>

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
yeah this is a terrible answer
-3

If anyone else is looking for the opposite i.e., reverting back to the commit you initially thought you should delete, use git reflog credit: Murtuzaali Surti's article on git reflog to find the missing commits.

I luckily didn't manage to delete the commit I needed back, even after attempting multiple frantic git reset --soft/hard commands and honestly I'm becoming more and more confident that git has my back no matter what, at least if a commit has been made. You will still need to git reset --hard <SHA-found-with-reflog> But see the link to the article for guidance.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.