Git - How to undo a checkout of unstaged files which discards local changes
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?
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 ^^
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).
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.
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 ;)
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.
Check local history in your IDE.
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.
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.
In case you ever stashed the changes earlier (for example, prior to rebasing), this will likely help
How to recover a dropped stash in Git?
even if you have already 'stash pop'ed the changes.
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
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.
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.
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.
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.
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.
If you're using intellij IDE then this can be undone.
- Do right click on the file in project explorer for which you want to get the lost changes.
- Local History -> Show History
- In the left pane you ca see different-2 timestamps select those
- Changes will be visible on the right.
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
精彩评论