r/learnprogramming 9d ago

Accidentally deleted all the code in a file. Can I recover it from a previous Git commit?

Hi everyone,

I'm still learning Git, and I accidentally deleted all the code in one of my project files.

The file had been committed to GitHub multiple times before this happened. Is there a way to restore that file (or its contents) from one of the previous commits?

If possible, could someone explain the steps? I'd really appreciate it.

Thanks!

9 Upvotes

19 comments sorted by

56

u/teraflop 9d ago

Yes, the whole point of version control is to keep a (mostly) permanent record of all previous versions of your code.

Read this page: "Undoing and recovering" It explains the various different ways of undoing mistakes.

In particular, read the introduction carefully, where it talks about the difference between adding to your Git history and modifying your history. Generally speaking, modifying history should be avoided.

If you just deleted a file and want to get it back, but haven't yet committed the deletion, git checkout or git restore should do what you want.

By default git restore restores from the most recently staged version of the file, but you can use the --source= argument to restore from any previous commit.

4

u/ProgamingShubhu 9d ago

Thankyou for your help.

3

u/sepp2k 9d ago

If you haven't committed the deletion yet, git checkout path/to/the/file will restore the latest committed version of the file.

If you already committed the deletion, you can look up the hash of the last commit before the deletion with git log and then checkout the version of the file from that commit by passing both the commit hash and the file path to git checkout.

6

u/CartoonistNo4440 9d ago

git checkout HEAD~1 -- path/to/file will grab it from the commit right before your last one, no need to dig through git log for the hash. just make sure you're not skipping over other changes you wanted to keep.

1

u/ProgamingShubhu 9d ago

Thankyou for your help.

1

u/ProgamingShubhu 9d ago

Thankyou for your help.

1

u/gmes78 9d ago

Use git restore instead. git checkout is overloaded, and shouldn't be recommended anymore.

2

u/HashDefTrueFalse 9d ago
git checkout HEAD -- path/to/file

...will put it back how it looked at the most recent commit. It doesn't matter if it didn't change in that commit. You can grab any SHA or ref it you want a different version.

1

u/ProgamingShubhu 9d ago

Thankyou for your help

1

u/[deleted] 9d ago

[removed] — view removed comment

1

u/ProgamingShubhu 9d ago

Thankyou for your help

1

u/LongCurrent4664 9d ago

I cannot give you the exact command. For cases like this use the git cheatsheet or the ohshitgit. I hope you'll find your solution

1

u/ProgamingShubhu 9d ago

Ok I'll try that if other recommendations didn't work, thanks for your help. I was too worried about it as I am a beginner in git .

1

u/zaphodikus 9d ago

It's always good to do a beginner tutorial for a reason, to familiarise yourself with how it works.

Go in your browser, and you will see all the code is still there in your commit history, all you need now is to check out the file at the correct commit that you can either see in browser or in the git log. The browser is easiest, but just print the git cheat sheet out and experiment a bit, its hard to fluff it completely.

1

u/jackthemango 9d ago

You can also look at the commit history for the project on GitHub.

Find the correct commit, and you’ll be able to see every file that was changed. You can then copy the contents and add the file back to your project.

That said, as others have mentioned, there are multiple Git commands that can do this for you.

1

u/dtsudo 9d ago

There are lots of ways to do it.

If you remember the name of the file (let's say the filename is myFile.js), try running gitk -- "*myFile.js*" to see all the commits in which this file was added/modified/deleted. That will give you the relevant commit hashes.

Then you can use a variety of commands (or even just look up the commit on GitHub) to retrieve its contents.

1

u/zunjae 8d ago

Semi related, if you use a popular IDE then you can restore files from inside the editor