1357

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
frederj
  • 1,483
  • 9
  • 20
hap497
  • 154,439
  • 43
  • 83
  • 99
  • 11
    This question appears to be a duplicate of another of your own questions: http://stackoverflow.com/questions/1338728/how-to-delete-a-git-commit – Brian Campbell Oct 23 '09 at 03:43
  • 1
    Possible duplicate of [Delete commits from a branch in Git](https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – Leonardo Alves Machado Sep 18 '17 at 17:33
  • 1
    Possible duplicate of [How to undo the last commits in Git?](https://stackoverflow.com/questions/927358/how-to-undo-the-last-commits-in-git) – Jan Zerebecki Nov 19 '17 at 16:55
  • 12
    DANGER: `reset --hard` can result in loss of work, because doing so results in git overwriting your local files (your new work) with the ones from the web (happened to me). Questions and answers about git should explicitly state what their commands are doing and what the risks are for readers following. – JoseOrtiz3 Dec 12 '18 at 23:03

12 Answers12

1642

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

Jeril Kuruvila
  • 17,190
  • 1
  • 20
  • 23
839

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

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
  • 460
    Or `git reset --hard origin/master`, to reset it to whatever the origin was at. – bdonlan Oct 23 '09 at 03:25
  • 1
    Another useful pointer you can reset to is ORIG\_HEAD or its generalization utilizing reflog HEAD@{1} (the last position of HEAD). – Jakub Narębski Oct 23 '09 at 09:26
  • 15
    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](https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard) – user18099 Jun 09 '17 at 10:00
  • @user18099 I'd go ahead and make a new answer with that info, or just edit this one? – gdvalderrama Nov 08 '17 at 10:05
  • 3
    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). – Bjørn Børresen Dec 11 '18 at 15:12
  • When executing `git reset --hard HEAD^` on windows cmd and getting `?More`, you can try `git reset --hard HEAD~1` – Beytan Kurt Jan 09 '19 at 06:43
  • 25
    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` – papigee Feb 22 '19 at 18:27
  • 1
    "Beware when you do any git reset --hard, as you can lose any uncommitted changes you have." This is misleading. The line before it (`git reset --hard HEAD^`) will ALSO delete committed non-pushed changes. – Skeets May 10 '19 at 00:58
  • Someone had merged prototype branches into out RC branch but had not pushed the changes. `git reset --hard HEAD^` worked for me where `git reset --hard HEAD~`suggested by other answers was not working. Thank you. - EDIT: On a later review, I think the other suggestion needed to include `;`, as in `git reset --hard HEAD~;` - I'll verify this later. – Francisco Zarabozo Apr 21 '21 at 15:13
312

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

M. Massula
  • 3,730
  • 2
  • 11
  • 14
  • 2
    If you are working in a custom branch: git reset --soft origin/{Branch} – P.O.W. Oct 04 '20 at 16:48
  • 3
    NOTE `git reset HEAD~` remove all files you've created – Skizo-ozᴉʞS ツ Jan 17 '22 at 20:09
  • NOTE #2: the first option **restores the changes of your undone commit back into your staged changes**. – l -_- l Dec 08 '22 at 11:11
  • 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. – Andy Weinstein Dec 29 '22 at 12:50
286
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.

Shadoweb
  • 5,812
  • 1
  • 42
  • 55
121

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.

papigee
  • 6,401
  • 3
  • 29
  • 31
63

Simply type in the console :

$ git reset HEAD~

This command discards all local commits ahead of the remote HEAD

Álvaro Loza
  • 411
  • 4
  • 21
marcdahan
  • 2,654
  • 25
  • 25
  • 9
    Can you explain what your answer adds over the existing answers? – nvoigt Sep 04 '18 at 16:20
  • 2
    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 – Grant Dec 02 '19 at 03:22
  • 2
    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. – Craig Wilcox Dec 22 '20 at 22:17
59

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*

Baptiste Mille-Mathias
  • 2,144
  • 4
  • 31
  • 37
Karina Haddad
  • 591
  • 4
  • 3
  • 3
    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 – Vlastimil Ovčáčík Aug 22 '18 at 18:18
  • 3
    `--soft` - ensures that you do not lose changes in the file whose commit you are trying to undo – papigee Mar 01 '19 at 03:14
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
1ac0
  • 2,875
  • 3
  • 33
  • 47
Kamlesh Patidar
  • 241
  • 2
  • 2
  • 4
    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. – user1063287 May 19 '16 at 10:22
  • `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. – Philippe Jul 05 '23 at 17:29
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
Adnan
  • 2,001
  • 2
  • 26
  • 28
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.

saga123
  • 41
  • 4
2

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.

MrCas
  • 51
  • 5
-1

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.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34748639) – Ram Chander Aug 01 '23 at 09:26