git undo all uncommitted or unsaved changes

GitCommand LineUndoGit Reset

Git Problem Overview


I'm trying to undo all changes since my last commit. I tried git reset --hard and git reset --hard HEAD after viewing this post. I responds with head is now at 18c3773... but when I look at my local source all the files are still there. What am I missing?

Git Solutions


Solution 1 - Git

  • This will unstage all files you might have staged with git add:

      git reset
    
  • This will revert all local uncommitted changes (should be executed in repo root):

      git checkout .
    

    You can also revert uncommitted changes only to particular file or directory:

      git checkout [some_dir|file.txt]
    

    Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):

      git reset --hard HEAD
    
  • This will remove all local untracked files, so only git tracked files remain:

      git clean -fdx
    

    > WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for preview of files to be deleted.


To sum it up: executing commands below is basically equivalent to fresh git clone from original source (but it does not re-download anything, so is much faster):

git reset
git checkout .
git clean -fdx

Typical usage for this would be in build scripts, when you must make sure that your tree is absolutely clean - does not have any modifications or locally created object files or build artefacts, and you want to make it work very fast and to not re-clone whole repository every single time.

Solution 2 - Git

If you wish to "undo" all uncommitted changes simply run:

git stash
git stash drop

If you have any untracked files (check by running git status), these may be removed by running:

git clean -fdx

git stash creates a new stash which will become stash@{0}. If you wish to check first you can run git stash list to see a list of your stashes. It will look something like:

stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes

Each stash is named after the previous commit messsage.

Solution 3 - Git

What I do is

git add . (adding everything)
git stash 
git stash drop

One liner: git add . && git stash && git stash drop

A shorter version as pointed out by M. Justin

git stash -u && git stash drop

Solution 4 - Git

there is also git stash - which "stashes" your local changes and can be reapplied at a later time or dropped if is no longer required

more info on stashing

Solution 5 - Git

> Adding this answer because the previous answers permanently delete your changes

The Safe way

git stash -u

Explanation: Stash local changes including untracked changes (-u flag). The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Want to recover the changes later?

git stash pop

Explanation: The command will reapply the changes to the top of the current working tree state.

Want to permanently remove the changes?

git stash drop

Explanation: The command will permanently remove the stashed entry

Link to git stash documentation

Solution 6 - Git

I'm using source tree.... You can do revert all uncommitted changes with 2 easy steps:

  1. just need to reset the workspace file status

enter image description here 2) select all unstage files (command +a), right click and select remove

enter image description here

It's that simple :D

Solution 7 - Git

For those who reached here searching if they could undo git clean -f -d , by which a file created in eclipse was deleted,

You can do the same from the UI using "restore from local history" for ref:[Restore from local history][1]

[1]: http://help.eclipse.org/mars/index.jsphttp://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-87b.htm "restore from local history" "restore from local history"

Solution 8 - Git

Another option to undo changes that weren't staged for commit is to run:

git restore <file>

To discard changes in the working directory.

Solution 9 - Git

States transitioning from one commit to new commit

0. last commit,i.e. HEAD commit
1. Working tree changes, file/directory deletion,adding,modification.
2. The changes are staged in index
3. Staged changes are committed

Action for state transitioning

0->1: manual file/directory operation
1->2: git add .
2->3: git commit -m "xxx"

Check diff

0->1: git diff
0->2: git diff --cached
0->1, and 0->2: git diff HEAD
last last commit->last commit: git diff HEAD^ HEAD

Revert to last commit

2->1: git reset
1->0: git checkout .     #only for tracked files/directories(actions include modifying/deleting tracked files/directories)
1->0: git clean -fdx     #only for untracked files/directories(action includes adding new files/directories)
2->1, and 1->0: git reset --hard HEAD

Equivalent of git clone, without re-downloading anything

git reset && git checkout . && git clean -fdx

Solution 10 - Git

git restore [filename_path]

For example I need to discard my last changes in index.html file:

git restore /usr/myPC/folder/index.html

Solution 11 - Git

Use this to remove unwanted changes after last commit.

git reset --hard HEAD

Solution 12 - Git

If you want to "undo" all uncommitted changes or local changes simply run:

git add . 
git stash 
git stash drop
git clean -fdx

Solution 13 - Git

The following defines a reusable Git command alias to remove any local changes, which can then be used any time in the future to delete any uncommitted changes:

git config --global alias.remove-changes '!git stash push --include-untracked && git stash drop'

Using the alias is straightforward:

git remove-changes

The alias pushes all changes to the stash (including uncommitted ones) using git stash push --include-untracked, then drops the newly created stash entry using git stash drop.

Solution 14 - Git

There are three options in Git that help to undo your local changes¶ To view the changes that have been made in your working directory, you should run git status:

git status

Undoing changes with git stash To discard all local changes, but also to save them for later use, you can run the git stash command:

git stash

Undoing changes with git checkout To discard local changes to a file permanently, you can run:

git checkout --

Undoing changes with git reset To discard all local changes to all the files permanently, you can do:

git reset --hard

SOURCE: https://www.w3docs.com/snippets/git/how-to-discard-unstaged-changes.html

Solution 15 - Git

# Navigate to project root, `.` works too.
git restore *

git status showed that I had some files that were changed, but I wanted to get rid of those and start a new branch. Until today, I had been using git reset approach, which I do like for jumping back to other specific commits.

> https://www.git-tower.com/learn/git/commands/git-restore/

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
QuestionAntarr ByrdView Question on Stackoverflow
Solution 1 - GitmvpView Answer on Stackoverflow
Solution 2 - GitAbramView Answer on Stackoverflow
Solution 3 - GitRalphView Answer on Stackoverflow
Solution 4 - GitkeshavView Answer on Stackoverflow
Solution 5 - GitbshepsView Answer on Stackoverflow
Solution 6 - Gituser1872384View Answer on Stackoverflow
Solution 7 - GitAbdul Rahman KView Answer on Stackoverflow
Solution 8 - GitRtmYView Answer on Stackoverflow
Solution 9 - GitZiiView Answer on Stackoverflow
Solution 10 - GitSingle96View Answer on Stackoverflow
Solution 11 - GitNavanee SubburajView Answer on Stackoverflow
Solution 12 - GitSantosh KumarView Answer on Stackoverflow
Solution 13 - GitM. JustinView Answer on Stackoverflow
Solution 14 - GitXab IonView Answer on Stackoverflow
Solution 15 - GitKalanosView Answer on Stackoverflow