DEV Community

Cover image for How to Revert to a Specific Git History After Deleting Files in a Recent Push
Jafaru Emmanuel
Jafaru Emmanuel

Posted on

How to Revert to a Specific Git History After Deleting Files in a Recent Push

Throughout your development journey, there would be days when you accidentally deleted important files and pushing the changes to your Git repository and for a beginner, this can be nerve-wracking, especially if it disrupts the project. Fortunately, Git’s powerful version control features allow you to revert to a previous commit and recover your deleted files.

In this guide, we'll walk through the steps to revert your repository to a specific commit and undo the accidental deletion, ensuring you don't lose any work.


Steps to Revert to a Previous Git Commit After Deleting Files

1. View Your Git Commit History

Before we can revert to a specific commit, we need to identify which commit represents the state before the files were deleted. You can do this by viewing the commit history using the following command:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

This will display a list of recent commits in the format:
The response will contain the commit hash and the commit message.

f5e4c72 Fixed bug in login page
a9c3f21 Added new tests for API
7b1e2f4 Removed old, unused files
be394fa Initial commit with setup
Enter fullscreen mode Exit fullscreen mode

Here, each commit has a SHA (a unique identifier) and a short description. Find the commit that you want to revert to, which occurred before the accidental deletion.

be394fa Initial commit with setup
the first part of this response is the commit-hash
While the last part holds the commit message as this will give you an idea of the commit where you last had the file.

2. Checkout to the Desired Commit

Once you’ve identified the commit you want to revert to, use the following command to "checkout" the project to that state:

git checkout <commit-hash>
Enter fullscreen mode Exit fullscreen mode

For example, if you want to go back to commit a9c3f21, you would run:

git checkout a9c3f21
Enter fullscreen mode Exit fullscreen mode

At this point, your working directory will reflect the state of the project at that specific commit, but this is just a temporary state. You're currently in a "detached HEAD" state, which means you are not on any branch but directly viewing the past commit.

3. Create a New Branch from the Commit (Optional)

If you want to work from this point forward, you can create a new branch from this commit to start fresh while preserving the previous history. This is useful if you want to create an alternative timeline of changes:

git checkout -b fix-deleted-files
Enter fullscreen mode Exit fullscreen mode

This creates a new branch named fix-deleted-files that starts from the commit a9c3f21.

4. Reset Your Branch to the Previous Commit

If you don’t want to create a new branch but instead want to reset your current branch to this specific commit (and remove the latest unwanted changes), you can do a hard reset.

Run the following command:

git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode

For example:

git reset --hard a9c3f21
Enter fullscreen mode Exit fullscreen mode

This will reset your current branch to the state of the commit a9c3f21 and discard any commits that were made after it.

5. Force Push the Changes to Remote

Now that your local branch is restored to the desired state, you need to force push it to the remote repository to reflect the changes.

Use the --force (or -f) option to overwrite the history on the remote branch:

git push origin <branch-name> --force
Enter fullscreen mode Exit fullscreen mode

For example:

git push origin main --force
Enter fullscreen mode Exit fullscreen mode

This will update the remote main branch to match your local branch, effectively undoing the changes made in the recent push, including the accidental deletion of files.


Summary of Commands

Here’s a quick summary of the commands to revert to a specific commit after accidentally deleting files:

  1. View commit history:
   git log --oneline
Enter fullscreen mode Exit fullscreen mode
  1. Checkout to a specific commit:
   git checkout <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  1. (Optional) Create a new branch:
   git checkout -b <new-branch-name>
Enter fullscreen mode Exit fullscreen mode
  1. Hard reset your branch to the desired commit:
   git reset --hard <commit-hash>
Enter fullscreen mode Exit fullscreen mode
  1. Force push the changes to remote:
   git push origin <branch-name> --force
Enter fullscreen mode Exit fullscreen mode

Conclusion

With Git’s powerful history management, reverting to a previous state is straightforward and effective. Whether you accidentally deleted files or introduced a bug in a recent commit, you can always revert back to a stable version and continue working without losing your progress. However, always be cautious when using --force to push changes, as it can overwrite history on the remote repository.

By following this guide, you can confidently recover from accidental deletions and keep your project intact.

If you found this article helpful, please like and leave a comment. I would like to see how many minutes of trials and debugging you had spent before finding this articl and how easy it was for you to follow through this guide.

Top comments (0)