How to Restore a Git Repository

August 1, 2022

Introduction

Accidentally deleted a critical Git repository when cleaning up? Forcefully pushed a new commit before fetching the latest version on a different machine?

Whatever the situation, there are two solutions to try and restore a missing Git repository.

This article provides two possible methods to restore a Git repository.

How To Restore A Git Repository

Prerequisites

  • A GitHub account and access to a web browser.
  • Access to the command line/terminal.
  • Git installed and configured.

How to Restore Git Repository

The sections below offer two options to restore a Git repository. The methods don't guarantee success, and the best way to avoid similar situations in the future is by creating a backup and recovery plan.

Option 1: Restore Deleted Git Repository Using GitHub Interface

To restore a deleted Git repository using the GitHub interface, do the following:

1. Log into the account where the GitHub repository was.

2. Open the dropdown menu in the top right corner and choose Settings.

GitHub interface settings menu

3. Click the Repositories menu item on the left.

GitHub interface Repositories menu item

4. Select Deleted Repositories.

GitHub deleted repositories restore interface

5. Choose the repository to restore from the list and click the Restore button.

Note: For recently deleted repositories, it takes up to an hour for the list to update with the information. If the repository had a fork or was forked, it does not show up.

6. Click the I understand, restore this repository button to confirm the restore.

GitHub restore repository confirmation

The repository restores in a public state and without any collaborators. Change the options in the repository settings if necessary.

Option 2: Restoring Overwritten Git Repository with Ref API

If the local version differs from the remote version, pushing a new commit shows a warning to do a git pull first.

git push -f terminal output

Forcing the push with the -f flag overwrites the remote without any warnings.

To restore an overwritten commit into a new branch, do the following:

1. Use curl to request the GitHub Events API via terminal. Replace the <USERNAME> and <REPOSITORY> with the correct values:

curl https://api.github.com/repos/<USERNAME>/<REPOSITORY>/events
curl git events api response terminal output

The output shows an event history for the provided repo in JSON format. Use the time and commit message to locate and narrow the search for the exact commit.

2. Use the GitHub Refs API to retrieve the commit into a new branch:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: token <TOKEN>" -X POST -d '{"ref":"refs/heads/<NEW BRANCH NAME>","sha":"<SHA>"}' https://api.github.com/repos/<USERNAME>/<REPOSITORY>/git/refs

The command requires the following information:

  • "Authorization: token <TOKEN>" requires a personal token generated in GitHub profile settings. Select the public_repo scope from the checklist and click Generate Token at the end of the list. Copy and paste the value into the command in place of <TOKEN>.
  • "ref":"refs/heads/<NEW BRANCH NAME>" creates the new branch reference.
  • "sha":"<SHA>" expects the SHA value of the commit. Copy and paste the value from the JSON response.
  • Lastly, replace the <USERNAME> and <REPOSITORY> in the final API link with the correct information.
curl restore repository response 201 terminal output

Running the command sends an HTTP response and creates the restoration branch. If all the values are correct, the response is 201.

3. Pull the new branch:

git pull

The previous commit is now in the branch.

4. Show the branch with:

git branch -a | grep <branch name>
git branch restore terminal output

Compare and merge changes with your work. If you run into merge conflicts, check out our guide on resolving merge conflicts in Git.

Note: Learn more about working with Git repositories by referring to our article How To Pull The Latest Git Submodule. Git submodules allow multiple repositories to be hosted as subdirectories of the main repository.

Conclusion

After following the steps in this guide, you have a good chance of restoring a deleted or overwritten Git repository. For more Git guides, check out how to revert the last commit.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
Git Revert Commit: How to Undo Last Commit
March 3, 2022

Git has a lot of features that help you manage your project and easily access...
Read more
How To Unstage Files on Git
September 15, 2020

Unstaging in Git means removing queued changes from the index. This guide covers several different ways to...
Read more
How to Remove a Git Remote From Repository
November 17, 2020

When a remote repository moves to another host or a team member stops working on a...
Read more
Git Commands Cheat Sheet
March 10, 2020

Git, the popular version control system, has a plethora of commands for managing your...
Read more