1108

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,323
  • 8
  • 20
hap497
  • 141,763
  • 43
  • 79
  • 93
  • 10
    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
  • 10
    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. – OrangeSherbet Dec 12 '18 at 23:03

11 Answers11

1215

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
  • 12,882
  • 1
  • 18
  • 22
800

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
  • 306,970
  • 56
  • 356
  • 335
  • 437
    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
  • 14
    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
226
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
  • 4,984
  • 1
  • 36
  • 50
195

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

Friedrich
  • 1,739
  • 1
  • 9
  • 24
M. Massula
  • 2,372
  • 2
  • 7
  • 11
111

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
  • 4,895
  • 3
  • 23
  • 29
61

Simply type in the console :

$ git reset HEAD~

This command discards all local commits ahead of the remote HEAD

Álvaro Loza
  • 391
  • 3
  • 19
marcdahan
  • 2,360
  • 24
  • 24
  • 8
    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
49

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
  • 1,948
  • 3
  • 29
  • 36
Karina Haddad
  • 491
  • 4
  • 3
23

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,811
  • 3
  • 30
  • 46
Kamlesh Patidar
  • 231
  • 2
  • 2
  • 2
    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
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
  • 1,991
  • 2
  • 26
  • 26
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