Blankman
Blankman

Reputation: 267320

Rollback to last git commit

I just did a

git commit -m "blah"

then I added some files, how do I rollback and remove what is in my current files that have not yet been added/committed?

Upvotes: 161

Views: 239903

Answers (7)

majorgear
majorgear

Reputation: 337

There are several ways, the simplest way to revert to the previous commit of the current branch is

git checkout HEAD~1

You can use HEAD~n , where "n" is an arbitrary number, to go back further.

You can also use the commit id of the "commit before last" to revert to that commit. Like so:

git checkout <commit id>

You can list commits in a convenient form with the git log command. To see the previous 2 commits and their messages:

git log --oneline -2

Upvotes: 0

Joe Hanink
Joe Hanink

Reputation: 4857

Edited Answer - edited over time to be more helpful

Caveat Emptor - Destructive commands ahead.

Mitigation - git reflog can save you if you need it.


  1. UNDO local file changes and KEEP your last commit

    git reset --hard

  2. UNDO local file changes and REMOVE your last commit

    git reset --hard HEAD^

  3. KEEP local file changes and REMOVE your last commit

    git reset --soft HEAD^

Upvotes: 295

Terje Norderhaug
Terje Norderhaug

Reputation: 3689

An easy foolproof way to UNDO local file changes since the last commit is to place them in a new branch:

git branch changes
git checkout changes
git add .
git commit

This leaves the changes in the new branch. Return to the original branch to find it back to the last commit:

git checkout master

The new branch is a good place to practice different ways to revert changes without risk of messing up the original branch.

Upvotes: 8

Abhishek saharn
Abhishek saharn

Reputation: 967

If you want to just uncommit the last commit use this:

git reset HEAD~

work like charm for me.

Upvotes: 25

Paul Pladijs
Paul Pladijs

Reputation: 20506

If you want to remove newly added contents and files which are already staged (so added to the index) then you use:

git reset --hard

If you want to remove also your latest commit (is the one with the message "blah") then better to use:

git reset --hard HEAD^

To remove the untracked files (so new files not yet added to the index) and folders use:

git clean --force -d

Upvotes: 55

Sebastian Oliva
Sebastian Oliva

Reputation: 355

You can revert a commit using git revert HEAD^ for reverting to the next-to-last commit. You can also specify the commit to revert using the id instead of HEAD^

Upvotes: 6

SpliFF
SpliFF

Reputation: 39014

git reset --hard will force the working directory back to the last commit and delete new/changed files.

Upvotes: 22

Related Questions