Rollback to last git commit

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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^

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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

git reset HEAD~

work like charm for me.

Solution 5 - Git

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.

Solution 6 - Git

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^

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - GitJoe HaninkView Answer on Stackoverflow
Solution 2 - GitPaul PladijsView Answer on Stackoverflow
Solution 3 - GitSpliFFView Answer on Stackoverflow
Solution 4 - GitAbhishek saharnView Answer on Stackoverflow
Solution 5 - GitTerje NorderhaugView Answer on Stackoverflow
Solution 6 - GitSebastian OlivaView Answer on Stackoverflow