202

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?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
Michael Wallasch
  • 2,499
  • 2
  • 17
  • 17
  • No, we can not , But if you are using eclipse we can check Team-->Local History – USM Feb 05 '19 at 05:33

17 Answers17

193

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

Christoph Geschwind
  • 2,602
  • 1
  • 13
  • 15
  • 43
    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 Dec 03 '12 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! – Tyson Phalp Apr 05 '13 at 03:16
  • 8
    In eclipse you can right click on file -> compare with -> local history – Maragues May 31 '13 at 15:05
  • 4
    Thank you so much Christoph for posting that. Pycharm just paid for itself with that feature – Lobe Sep 22 '13 at 04:25
  • 2
    Vim: add `set hidden` to your vimrc. – Ciro Santilli OurBigBook.com Nov 19 '13 at 10:35
  • 5
    superb. Sublime2 undo also rolled back changes made by accidental git checkout on disk. – shuckc Dec 10 '13 at 15:03
  • 3
    Can just agree with everyone else! This saved my day! WebStorm local history... puh! – Per T Apr 30 '14 at 15:23
  • 3
    I just did the same mistake. NetBeans local history saved my ass, too. – albogdano Jul 15 '14 at 18:21
  • 9
    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 Nov 28 '14 at 16:16
  • 1
    Android Studio saved my hide. I had executed `git checkout .` instead of `git reset HEAD .`, after 4 hours of creating 5 clases and debugging them! – xyz Jun 08 '15 at 13:06
  • To add to @Maragues comment...In eclipse right click on file -> compare with -> local history. Then in the `History` window double-click an entry to view the diffs, When you find the diff you want to recover, right click the entry in the `History` window and select "Get Contents" – Brad Sep 24 '15 at 10:47
  • 2
    A simple ctrl+z (undo) in VS saved me, after I had accidentally done a git checkout on a single file in the working tree in git bash – EriF89 Nov 03 '15 at 14:06
  • In the JetBrains IDE's (PyCharm, Android Studio, IDEA, PHPStorm, AppCode, Webstorm, etc.) this is visible if you go to `View -> Recent Changes`. If you haven't changed anything else you can click on whatever's on the top of the list. It will then show you a diff and you can click the "Revert" button to undo the deletion. – plowman Dec 01 '15 at 22:47
  • 1
    ctrl+z inside the IDE. Saved me! – John Jan 15 '16 at 14:20
  • 1
    @qingu Thank you! You've just let me recover a full day of work. Thank you. Thank you. Thank you! How on earth isn't this part of Xcode? How on earth does git have such a hideous sharp edge of not bothering to put some kind of totally basic file recover folder somewhere? Extraordinary! Your comment definitely ought to be an answer as it's quite hidden in these comments. Would you want to make it in to one? I'm happy to, but I think you'll get up votes for it! – Benjohn Jun 06 '16 at 16:10
  • 2
    Thanks @Benjohn - I'm glad this was useful to you. I didn't want to make it an answer as it doesn't answer the original question. Feel free to post an answer yourself if you think it will improve visibility ;) – qingu Jun 06 '16 at 18:00
  • Is there a reason git doesn't have it's own implementation of local history? Is it simply because that's outside the scope of what git it supposed to do? – cjsimon Apr 26 '18 at 22:21
  • In Eclipse: Eclipse didn't find any local history, but simply pressing Ctrl-Z restored the lost changes. – Anomaly Jul 17 '18 at 19:25
131

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 ? – Ciprian Tomoiagă Nov 27 '16 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 Nov 27 '16 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: http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Ftasks%2Ftasks-87b.htm – VonC Nov 27 '16 at 22:33
  • 12
    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 Jul 04 '18 at 05:47
  • 2
    @gman does the answer below about "local history" provided by an IDE help you recover your work? – VonC Jul 04 '18 at 06:02
  • 2
    Thanks to the tip @VonC, You saved my life... I did git checkout but I added the . (dot) in the end and it restored all of my files... So, thanks to your tip because the Webstorm has the History feature and I restored all of my files... Thanks so much! – Biruel Rick Oct 19 '18 at 21:34
  • I just lost my work in a file. @gman, I agree with you. I really think git should have a trash folder it uses when it `rm`s data via checkout or whatever, even if it is limited to only the last few commands and gets cleaned often. I made the simple mistake of `git checkout -- path/to/dir` instead of `git checkout /path/to/dir/file1.txt`, thereby causing other files to lose their changes in that dir :( :( – Gabriel Staples Jul 28 '23 at 23:08
  • For anyone using VSCode, you can try the [Timeline feature mentioned here](https://stackoverflow.com/a/72417398/4561887). – Gabriel Staples Jul 28 '23 at 23:09
  • 1
    @GabrielStaples True. I have edited the answer to reference that feature, that I had [mentioned before](https://stackoverflow.com/a/60013101/6309). – VonC Jul 28 '23 at 23:13
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.

roman-roman
  • 2,746
  • 19
  • 27
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 ;)

Marcin Gil
  • 68,043
  • 8
  • 59
  • 60
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.

Benjohn
  • 13,228
  • 9
  • 65
  • 127
28

Check local history in your IDE.

Marcin Szymczak
  • 11,199
  • 5
  • 55
  • 63
  • 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 https://www.jetbrains.com/help/idea/2016.3/restoring-a-file-from-local-history.html – emen Dec 21 '16 at 02: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! – Andrew Smith Jan 18 '17 at 19:17
10

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.

Redmen Ishab
  • 2,199
  • 18
  • 22
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.

sscirrus
  • 55,407
  • 41
  • 135
  • 228
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.

Community
  • 1
  • 1
iksa
  • 31
  • 1
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

kdweber89
  • 1,984
  • 2
  • 19
  • 29
  • 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 Sep 02 '20 at 12:20
  • 1
    This worked for me in InteliJ... Phewwww :D – visc Feb 01 '22 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.

codemania23
  • 913
  • 11
  • 19
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.

shoe
  • 952
  • 1
  • 20
  • 44
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.

Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
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.
infiniteLearner
  • 3,555
  • 2
  • 23
  • 32
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.

Diego
  • 594
  • 2
  • 10
  • 29
-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
    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. – Bob Gilmore Sep 20 '13 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 :) – Russell England Mar 28 '16 at 12:10
  • 1
    Unfortunately git checkout and git checkout are two completely different and unrelated commands. The former keeps your local changes, the latter does not. – Lake Jun 21 '18 at 10:33