204

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?

1
  • No, we can not , But if you are using eclipse we can check Team-->Local History
    – USM
    Commented Feb 5, 2019 at 5:33

17 Answers 17

196

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

20
  • 46
    FYI this feature is available in the whole JetBrains family of "professional" IDE's: Pycharm, IDEA, PHPStorm, Webstorm. Saved my bakken today. Thanks, Christoph!
    – B Robster
    Commented Dec 3, 2012 at 22:08
  • 6
    I have to thank you. I use WebStorm, and completely forgot about that feature, even though I use it quite often. I was just so caught up the loss of code, I didn't think about it! Commented Apr 5, 2013 at 3:16
  • 8
    In eclipse you can right click on file -> compare with -> local history
    – Maragues
    Commented May 31, 2013 at 15:05
  • 5
    superb. Sublime2 undo also rolled back changes made by accidental git checkout on disk.
    – shuckc
    Commented Dec 10, 2013 at 15:03
  • 10
    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.
    – qingu
    Commented Nov 28, 2014 at 16:16
133

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

I mentioned the VSCode Timeline view in Feb. 2020 with VSCode 1.44, as one way to recover changes with an IDE.

9
  • 6
    I understand that changes to a 'private' file cannot be undone by git. However, if the file was modified by git (e.g. via git checkout --), I'd expect it to be able to undo that operation, maybe via the reflog. Is that wrong expectation ? Commented Nov 27, 2016 at 22:30
  • 2
    @CiprianTomoiaga reflog is for past unreferenced commits. If what you want to restore was not committed (or staged), reflog won't help you.
    – VonC
    Commented Nov 27, 2016 at 22:32
  • 4
    @CiprianTomoiaga for any kind of private content (not yet added), you would rely solely on your editor or IDE feature. Example for eclipse: help.eclipse.org/neon/…
    – VonC
    Commented Nov 27, 2016 at 22:33
  • 14
    Wow! Unbelieable. I needed to check out one file so I meant to type git checkout folders/subfolders/file but hit enter by accident after getting to folder and lost all my work. / is 1cm from enter on my keyboard and I switch between machines with different keyboard layouts often so there's often 10-15 minutes of my fingers getting used to new positions. Hard to believe in 2018 that git trashs files even though it says in the git book that git does out of it's way not to lose work.
    – gman
    Commented Jul 4, 2018 at 5:47
  • 2
    @gman does the answer below about "local history" provided by an IDE help you recover your work?
    – VonC
    Commented Jul 4, 2018 at 6:02
76

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.

3
  • 1
    I stumbled on this fix by accident just now, and was about to post a new answer. It looks like Sublime captures Git changing the file state's in its file modification history.
    – Timmah
    Commented Jun 9, 2017 at 7:07
  • Worked for me in Ecipse.
    – Anomaly
    Commented Jul 17, 2018 at 19:25
  • 5
    Worked for Visual Studio Code too
    – intotecho
    Commented Sep 9, 2018 at 11:59
47

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
34

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

10
  • 3
    I can confirm that this works. This just saved me hours of work! Commented Jan 5, 2018 at 15:15
  • did not work for me because the file concerned was the Model unfortunately
    – Siempay
    Commented Jan 25, 2018 at 16:46
  • @brahimm the Model?
    – Benjohn
    Commented Jan 25, 2018 at 18:26
  • 1
    This just saved me a tonne of heartache. Thank you so much.
    – squarefrog
    Commented Apr 19, 2018 at 10:49
  • 1
    Very interesting: I right-clicked on my source file and looked at the "Open With" menu. Both TextEdit 1.14 and TextEdit 1.6 were there as choices. Only TextEdit 1.14 had the option to revert to older revisions. The UI was very nice, just like TimeMachine.
    – sv_lane
    Commented Mar 17, 2019 at 21:55
29

Check local history in your IDE.

2
  • Just did a mistake of not staging my changes and forced a checkout. I checked my local history in Android Studio (view -> recent changes) and reverted the last changes I made. More info here jetbrains.com/help/idea/2016.3/…
    – emen
    Commented Dec 21, 2016 at 2:26
  • This works perfectly well in RubyMine, the action of wiping out your changes will be referenced as "External change." Thanks so much for this Marcin, it just saved me an hour of lost work! Commented Jan 18, 2017 at 19:17
11

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 on my mac, I recovered the changes and saved a ton of work for me.

1
  • is there any other way around ? Commented Mar 2, 2020 at 14:30
3

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.

1
  • 1
    Dear downvoter - could you please explain how I might improve this answer?
    – sscirrus
    Commented Dec 13, 2016 at 5:59
3

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.

0
2

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

2
  • This saved a couple of hours worth of work (in an angular project using visual code). thankfully the ones not open were just configurations that are easy to do
    – waleed
    Commented Sep 2, 2020 at 12:20
  • 1
    This worked for me in InteliJ... Phewwww :D
    – visc
    Commented Feb 1, 2022 at 13:14
2

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.

2

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.

1

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.

1
  • You could better explain how to do you: Copy the diff output to a textfile and run git apply --reject --whitespace=fix <filename with the diff output> Commented Jul 30, 2021 at 19:34
1

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.

1
  • 1
    Saved me a couple hours - just looked at the project entire folder worked well for me Commented Aug 15, 2022 at 17:39
1

If you're using intellij IDE then this can be undone.

  1. Do right click on the file in project explorer for which you want to get the lost changes.
  2. Local History -> Show History
  3. In the left pane you ca see different-2 timestamps select those
  4. Changes will be visible on the right.
0

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.

-1

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

3
  • 3
    Don't get your hopes up - get reflog only helps if you've been interacting with git in a meaningful way. Just editing a file, then blowing it away via a git checkout? git never had a chance. Commented Sep 20, 2013 at 16:09
  • 1
    +1 from me - Think its worth mentioning the git reflog here, I found this answer when I was searching for undoing a git checkout HEAD . - I had meant to type git reset HEAD . - I had just done a 'git reset --soft HEAD~1' and didn't know about the git reflog so was able to get back the work I had done :) Commented Mar 28, 2016 at 12:10
  • 1
    Unfortunately git checkout <revision> and git checkout <path> are two completely different and unrelated commands. The former keeps your local changes, the latter does not.
    – Lake
    Commented Jun 21, 2018 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.