Categories
git

Throw away local commits in Git

2039

Due to some bad cherry-picking, my local Git repository is currently five commits ahead of the origin, and not in a good state. I want to get rid of all these commits and start over again.

Obviously, deleting my working directory and re-cloning would do it, but downloading everything from GitHub again seems like overkill, and not a good use of my time.

Maybe git revert is what I need, but I don’t want to end up 10 commits ahead of the origin (or even six), even if it does get the code itself back to the right state. I just want to pretend the last half-hour never happened.

Is there a simple command that will do this? It seems like an obvious use case, but I’m not finding any examples of it.


Note that this question is specifically about commits, not about:

  • untracked files
  • unstaged changes
  • staged, but uncommitted changes

1

3319

+150

If your excess commits are only visible to you, you can just do
git reset --hard origin/<branch_name>
to move back to where the origin is. This will reset the state of the repository to the previous commit, and it will discard all local changes.

Doing a git revert makes new commits to remove old commits in a way that keeps everyone’s history sane.

8

  • 130

    git reset --hard <commit hash, branch, or tag> if you want to go to a specific reference other than a remote branch.

    Jan 5, 2014 at 0:51


  • 76

    This will not only discard local commits, but also throw away everything in your work tree (ie. you local files). If all you want to do is uncommit, but leave your work intact, you should do “git reset HEAD^” … per stackoverflow.com/questions/2845731/…

    Jan 28, 2016 at 2:02

  • 6

    You may want to do a Fetch after this. This fixed the number-of-commits-waiting-to-be-pushed counter on SourceTree.

    – Stan

    Apr 14, 2017 at 7:05

  • 5

    git reset --hard origin/<branch_name> It will reset the project configuration as well, so take care of this. I have a big .cfg file which was reset to default. I had to spend hours on that again.

    – Ram

    Dec 6, 2018 at 11:55

  • 1

    Please update your answer to include that this will actually delete all the work you did also (like @aaronbauman mentioned back in 2016). I just lost a lot of work when all I wanted was to undo my last commit, without using revert, so that I could take out a sensitive value before pushing.

    Jan 14 at 19:37

386

Delete the most recent commit, without destroying the work you’ve done:

git reset --soft HEAD~1

Delete the most recent commit and remove changes:

git reset --hard HEAD~1

8

  • 15

    Useful answer. Thank you! I used git reset –soft origin/master

    Aug 6, 2018 at 10:40

  • 3

    @TarunKumar THANK YOU! I am using VS integration, and your solution was the only way I was able to wipe out a bunch of merge commits that I didn’t want in a branch I didn’t have permission to check in.

    – DVK

    Nov 13, 2018 at 14:46

  • 2

    Thanks, just what I was looking for, “git reset –soft HEAD~1” did the work as I committed unintentionally and wanted to revert but had other files that I didn’t want to be destroyed after revert.

    Nov 28, 2019 at 20:47

  • 2

    in Windows --hard "[email protected]{1}"

    – O-9

    Nov 9, 2020 at 15:57

  • 1

    Having the –soft answer come first in the answer above might help some overzealous StackExchange skimmers… nothing lost, no knowledge gained

    – charo

    Nov 7, 2021 at 15:08

373

Simply delete your local master branch and recreate it like so:

git branch -D master
git checkout origin/master -b master

8

  • 4

    This works well when backtracking your changes would cost too much time, which happened to me after a couple of rebases.

    – aross

    May 15, 2014 at 10:49

  • 1

    Useful for subtree pull/push problems among team members!

    Sep 11, 2014 at 18:15

  • This is perfect when you want to restore a branch instead of just master.

    Nov 24, 2014 at 6:36

  • 15

    this is not a good way to delete single local commit. Better to use git reset --hard origin/<branch_name>

    Sep 5, 2016 at 6:18


  • 1

    Maybe that solution will work, but that doesn’t mean it’s the proper one.

    – Adly

    May 6, 2019 at 10:33