Git - How to undo a checkout of unstaged files which discards local changes

Git

Git Problem Overview


I accidentially discard my changes on files in my local working tree via git checkout. The files aren't staged at this time. Is it posible to "undo" this checkout?

Git Solutions


Solution 1 - Git

If you are using a "professional" IDE chances are good that you can restore files from a local History. In Rubymine for example you can right click files and watch a history of changes independent from the git changes, saved me a few times now ^^

Solution 2 - Git

I believe if a file is modified but not yet added (staged), it is purely "private".
Meaning it cannot be restored by GIT if overwritten with the index or the HEAD version (unless you have a copy of your current work somewhere).

A "private" content is one only visible in your current directory, but not registered in any way in Git.

Note: As explained in other answers, you can recover your changes if you use an IDE (with local history) or have an open editor (ctrl+Z).

Solution 3 - Git

If you are working in an editor like Sublime Text, and have file in question still open, you can press ctrl+z, and it will return to the state it had before git checkout.

Solution 4 - Git

Unfortunately your changes are lost. Your private modifications are simply overwritten. Unless you did git stash prior making checkout...

Take it from the brighter side: you can now implement things even better ;)

Solution 5 - Git

Developing on OS X? Using Xcode? You're likely to be in luck!

As described in a comment by qungu, OS X maintains an autosaved version history of files, even if you're not using time machine.

So, if you've blown away your unstaged local changes with a careless git checkout ., here's how you can probably recover all your work.

> If somebody finds this thread having destroyed some work in XCode, there is a way to get the AutoSave history. XCode itself does not have a menu entry to see the AutoSave history, but it does store it. If you open the files in question in TextEdit, you can revert and look through the AutoSave history under File > Revert.

Which is awesome, and recovered about a day of work for me, yesterday.


You might ask, "Why doesn't the git command-line UI, the premier VCS used for software engineering in 2016 2017 2018 2019 2020, at least back up files before just blowing them away? Like, you know, well written software tools for about the last three decades."

Or perhaps you ask, "Why is this insanely awesome file history feature accessible in TextEdit but not Xcode where I actually need it?"

… and both of those, I think, will tell you quite a lot about our industry. Or maybe you'll go and fix those tools. Which would be super.

Solution 6 - Git

Check local history in your IDE.

Solution 7 - Git

In VSCODE ctrl+z (undo) worked for me

I did git checkout .instead of git add . and all my file changes were lost.

But Now using command + z in my mac , recovered the changes and saved a tone of work for me.

Solution 8 - Git

An effective savior for this kind of situation is Time Machine (OS X) or a similar time-based backup system. It's saved me a couple of times because I can go back and restore just that one file.

Solution 9 - Git

In case you ever stashed the changes earlier (for example, prior to rebasing), this will likely help

https://stackoverflow.com/questions/89332/how-to-recover-a-dropped-stash-in-git

even if you have already 'stash pop'ed the changes.

Solution 10 - Git

Technically yes. But only on certain instances. If for example you have the code page up and you hit git checkout, and you realize that you accidently checked out the wrong page or something. Go to the page and click undo. (for me, command + z), and it will go back to exactly where you were before you hit the good old git checkout.

This will not work if your page has been closed, and then you hit git checkout. It only works if the actual code page is open

Solution 11 - Git

I just had that happen to me, I checked out an entire folder containing hours of work! Fortunately I found out that my IDE Netbeans keeps an history of each file, that allowed me to recuperate 99% of the stuff even though I needed to fix a few things manually.

Solution 12 - Git

Dude,

lets say you're a very lucky guy just like I've been, go back to your editor and do an undo(command + Z for mac), you should see your lost content in the file. Hope it helped you. Of course, this will work only for existing files.

Solution 13 - Git

If you work with a terminal/cmd prompt open, and used any git commands that would have showed the unstaged changes (diff, add -p, checkout -p, etc.), and haven't closed the terminal/cmd prompt since, you'll find the unstaged changes are still available if you scroll up to where you ran those aforementioned git commands.

Solution 14 - Git

I normally have all of my work in a dropbox folder. This ensures me that I would have the current folder available outside my local machine and Github. I think it's my other step to guarantee a "version control" other than git. You can follow this in order to revert your file to previous versions of your dropbox files

Hope this helps.

Solution 15 - Git

In JetBrains IDE's like PhpStorm, Webstorm and Pycharm you can right click each file or folder and select Local History > Show History.

This will open local history view of the file with all the recent changes listed on the left pane.

You can right click on the last change and Revert.

Solution 16 - Git

Maybe your changes are not lost. Check "git reflog"

I quote the article below:

"Basically every action you perform inside of Git where data is stored, you can find it inside of the reflog. Git tries really hard not to lose your data, so if for some reason you think it has, chances are you can dig it out using git reflog"

See details:

http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html

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
QuestionMichael WallaschView Question on Stackoverflow
Solution 1 - GitChristoph GeschwindView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - Gitroman-romanView Answer on Stackoverflow
Solution 4 - GitMarcin GilView Answer on Stackoverflow
Solution 5 - GitBenjohnView Answer on Stackoverflow
Solution 6 - GitMarcin SzymczakView Answer on Stackoverflow
Solution 7 - Gitishab acharyaView Answer on Stackoverflow
Solution 8 - GitsscirrusView Answer on Stackoverflow
Solution 9 - GitiksaView Answer on Stackoverflow
Solution 10 - Gitkdweber89View Answer on Stackoverflow
Solution 11 - GitPierre-Verthume LarivièreView Answer on Stackoverflow
Solution 12 - Gitcodemania23View Answer on Stackoverflow
Solution 13 - GitshoeView Answer on Stackoverflow
Solution 14 - GitDiegoView Answer on Stackoverflow
Solution 15 - GitGerard de VisserView Answer on Stackoverflow
Solution 16 - GitrobertobadoView Answer on Stackoverflow