162

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?

2
  • 20
    no, this is not what the OP is asking -- it is clear that he wants to rollback to the last commit, not undo the last commit. he did not say he added some files and then commited them. so clearly he wants the last commit. Commented Oct 21, 2016 at 17:41
  • 7
    It's too hard to find an answer to this super common question Commented May 9, 2020 at 20:35

7 Answers 7

297

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^

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

10 Comments

This command will delete your previous commit, so use with caution! git reset --hard is safer
WTF is this doing as the correct answer with 36 up votes. I've just lost a days work to this... because I read the answer not the comment. This is not the correct answer to the question!
OK all is not lost! You can do git reflog this will allow you to see commits you did before the reset. You can then checkout those commits
For what it's worth, I had interpreted the OP's question as "how do I rollback the commit and remove the current files", but it was and remains ambiguous. I would have understood differently if the OP had simply said "how do I remove the current files that have not been added or committed"
Git is the antithesis of Alan Kays statement Simple things should be simple, complex things should be possible. ... especially of its first part. Still, thank you for your answer. it helped.
|
55

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 

3 Comments

does git reset --hard HEAD^ remove my latest commit permanently? like i can't go back to it?
it seems "git clean --force -d" also removes .gitignored folders
@AlexeyTimokhin Yes, that's the purpose of it. Remove -d if you want to avoid this.
25

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

git reset HEAD~ 

work like charm for me.

2 Comments

this is the exact answer to the question
this should be the approved answer
22

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

4 Comments

did that, but the files are still there. (untracked files are still there)
in this case you could do a "git stash"
upvoted because this is the only clear and unambigous answer to the OP's clear and unambiguous question.
@Blankman Then first execute git add -A, git status, and finally git reset --hard. git add -A will prevent the untracked files to be excluded from your git hard reset
8

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.

Comments

6

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^

1 Comment

This doesn't "revert" in the sense you're saying. This creates a reverse-commit.
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.