Skip to main content

Explore your training options in 10 minutes

Git: Restore Deleted File

James Gallagher - December 29, 2020

Git keeps a log of all the changes made to files within a repository. You can restore a file that you have deleted since a previous commit by using the git checkout command. This command lets you navigate to a previous point in your repository’s history.

This guide discusses how to restore a deleted file in a Git repository using the git checkout command. We’ll also discuss how to use the git rev-list command to find when a file was last modified in a repository.

Git: Restore Deleted File

We’re working on a repository called ck-git. Our repository once stored a file called which we now want to retrieve. This file contained some standard configuration settings for our project.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses

By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

We can use the ls command to verify that the file does not exist:


This command returns:    dev   	 docs

We know that we must have deleted the file in the last commit because it was in the repository before we made our most recent changes.

Because we know what commit our file was in, we can use the git checkout command to retrieve the file:

git checkout HEAD~1

HEAD refers to the current version of the repository we are viewing. In this case, we are viewing the master branch. This is because we have not navigated to any other branch. The ~1 tells Git to move back one commit from our current HEAD. refers to the name of the file we want to retrieve.

When you specify a file or folder with the git checkout command, Git will only retrieve that file and make it part of your local working copy of a repository.

If you don’t specify a file to retrieve, Git will move your repository into “detached HEAD” state . In this state, you can navigate around a repository at the point in history you specified.

Let’s take a look at the files in our project using the ls command:    dev   	 docs

The git checkout command has successfully retrieved the file. We can now commit this file to our repository so we do not lose it:

git add
git commit -m "feat: Reintroduce file"
git push

First, we add to the staging area. This is because it is a new file in our repository. We then add the file to a commit and push that commit to the remote version of our repository.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Find the Last Commit on a File

You can use the git rev-list (revision list) command to find the checksum of the last commit that affected a file. Using this information and the git checkout command, you can retrieve a file from a commit without knowing when the file was last modified.

We want to recover a file called from our repository. We know that it existed at some point in the history of our project, but we are not sure when we last modified the file.

To find the commit in which we last modified the file, use the git rev-list command:

git rev-list HEAD -- -n 1

We’ve used the rev-list command with a couple of options. This command tells Git to list all the commits which are reachable from our HEAD that changed the “” file. The -n 1 option instructs Git to limit the result of the command to one entry.

Let’s see what happens when we run this command:


We can see the checksum for the last commit in which our file was modified. We can use this information with the checkout command to retrieve our deleted file:

git checkout 9ad202d538c6ee6448e2c1ecc080c292cc761771^

This command retrieves from the commit before it was deleted. We have added the ^ to the end of the command to retrieve the commit before the one we specify. This is because the last commit that modifies a file is the one in which the file has been deleted.

When we run this command, there are no error messages returned. This means our file has been successfully retrieved. Let’s run the ls command to verify our file has been added to our repository:    dev   	 docs

Our file is in our local working directory.


You can restore a deleted file from a Git repository using the git checkout command. If you do not know when a file was last deleted, you can use git rev-list to find the checksum of the commit in which that file was deleted. Then, you can check out that commit.

Now you have the knowledge you need to restore a deleted file in Git like a pro!

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

James Gallagher

About the author: James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse,, Afrotech, and others.

Skip to main content