7

I accidentaly deleted local file changes on git repository. They were NOT commited or even pushed.

What I did: git status (then files not staged for commit showed and I accidentaly removed whole folder called "smdr" by this comand): git checkout -- smdr

Then files changes disappeared.

How can I recover those files (birng everything back before that git checkout -- smdr comand)?

3
  • If they weren't committed, your best bet is to check the Trash or Recycle Bin on whatever computer the local files were on. If you're using Windows, there's a "Previous versions" you can revert back to in the properties of the folder (if you have this feature on)
    – Avantol13
    Commented Jun 4, 2015 at 12:03
  • @Avantol13 when you delete something in the cli, you usually never can find them back in the trash
    – edi9999
    Commented Jun 4, 2015 at 14:52
  • it happened to me the same... you might find this discussion useful stackoverflow.com/questions/1836742/…
    – jjrr
    Commented Oct 18, 2022 at 13:50

3 Answers 3

15

You can't with Git. The files were not committed so they are not in history. You just got the (inexistant) version in the index with git checkout.

Your only hope is your backup system.

1
  • 1
    If you previously stashed (and dropped), and you know the content would've been part of the stash. Then try to recover the stash. That way it would be possible to at least partially recover the files affected by git checkout.
    – vallentin
    Commented Sep 3, 2019 at 18:43
6

You can use any of the given options:

Git reflog

Type git reflog and checkout the commit you need, it will "revert" your repository to the "deleted" commit.

Git revert

Another option is use git revert SHA-1 which will revert your commit. It will simply undo your changes

Git reset

Git reset will checkout the content of the given sha-1. It will set your branch to be at the same state as the SHA-1

2
  • but my local changes weren't commited? Can I use one of these anyway?
    – user4076384
    Commented Jun 4, 2015 at 12:00
  • 2
    No, you can't. All these commands deal with committed history. Commented Jun 4, 2015 at 12:27
1

In addition to Matthieu Moy's accepted answer, I found this obvious but surprisingly good practice we can follow to prevent such "checkout" accidents from happening again.

"Here's another crazy idea: don't run 'git checkout ...' on a dirty work tree. Problem solved."

This(1) stuck out to me the most that I stopped ever since using git checkout . to discard any unstaged/uncommitted changes. I now use the command which was made to be more suitable for this: git reset.

git reset to unstage changes and git reset --hard when you're absolutely positively sure you want to discard your changes permanently. git stash is also your friend, doesn't hurt to stash your changes before you discard them.

Making these two a habit and using git checkout only for switching between branches (mostly) worked really well for me.

Update: A year later, I've adopted and have been using for quite some time now two new practices which may be of help. I've replaced git reset with the newer git restore. I combine it with a "are you sure?" confirmation function in bash(2) for extra caution. confirm && git restore . is my go-to command now for removing all unstaged changes.

The second is the -p, --patch option for checkout (it's there for many other commands like add which is really useful). It lets you interactively select hunks of code with a confirmation. Thus, git checkout -p is way safer now if you want to just remove some changes and it doesn't affect staged changes by default.


(1) Relevant part of a comment made by fr0sty on May 13, 2010 in a debate on Hacker News. I got this from the answer by VonC for "Get back the changes after accidental checkout?"

(2) See how to make a general purpose confirm function for use with important commands at the answer for "In Bash, how to add 'Are you sure [Y/n]' to any command or alias?"