Can git undo a checkout of unstaged files

0 votes
asked Apr 22, 2010 by herr-w

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?

11 Answers

0 votes
answered Apr 22, 2010 by vonc

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.

0 votes
answered Apr 22, 2010 by marcin-gil

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 ;)

0 votes
answered Apr 20, 2012 by christoph-geschwind

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 ^^

0 votes
answered Jan 20, 2013 by robertobado

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:

0 votes
answered Apr 9, 2013 by roman-roman

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.

0 votes
answered Apr 9, 2013 by marcin-szymczak

Check local history in your IDE.

0 votes
answered Jan 15, 2015 by diego

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.

0 votes
answered Jan 23, 2015 by kdweber89

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

0 votes
answered Jan 7, 2016 by benjohn

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, 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 awesome.

0 votes
answered Jan 18, 2016 by sscirrus

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter