Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git undo all uncommitted or unsaved changes

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?

like image 264
Antarr Byrd Avatar asked Dec 28 '12 20:12

Antarr Byrd


People also ask

How do I undo uncommitted changes git?

Try Git checkout --<file> to discard uncommitted changes to a file. Git reset --hard is for when you want to discard all uncommitted changes. Use Git reset --hard <commit id> to point the repo to a previous commit.

Does git reset remove uncommitted changes?

Use git reset to Remove Uncommitted Changes in Git Another way to remove uncommitted changes using git reset is with option --hard and params HEAD . The --hard option specifies Git to throw ALL changes between the current state and the commit in the last argument.

Can I get back uncommitted changes?

The "restore" command helps to unstage or even discard uncommitted local changes. On the one hand, the command can be used to undo the effects of git add and unstage changes you have previously added to the Staging Area.

How do I get rid of not committed changes?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.


2 Answers

  • 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.

like image 128
mvp Avatar answered Oct 20 '22 06:10

mvp


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.

like image 22
Abram Avatar answered Oct 20 '22 07:10

Abram