Skip to content

Git Recover Tutorial

Overview

Recovering in Git involves various techniques to restore lost commits, branches, or files. This document covers methods to recover from common issues such as accidental deletions, lost commits, and overwritten files.

Recovering Deleted Branches

When a branch is deleted, it can be recovered if it still exists in the repository's reflog.

Example: Recovering a Deleted Branch

  1. Check the reflog to find the commit hash of the deleted branch:

    git reflog
    

    Look for the entry that shows the deletion of the branch.

  2. Create a new branch pointing to the commit hash:

    git checkout -b <branch-name> <commit-hash>
    

Example

git checkout -b recovered-branch abc1234

This creates a new branch named recovered-branch from the commit abc1234.

Recovering Lost Commits

If you have lost commits, you can often recover them using the reflog.

Example: Using Git Reflog to Recover Commits

  1. Check the reflog to find the commit hash of the lost commit:

    git reflog
    
  2. Check out the lost commit or create a new branch from it:

    git checkout <commit-hash>
    

    or

    git checkout -b <branch-name> <commit-hash>
    

Example

git checkout abc1234

or

git checkout -b recovered-commit abc1234

This checks out or creates a new branch from the lost commit abc1234.

Recovering Overwritten Files

To recover a file that has been overwritten, you can restore it to a previous version.

Example: Restoring a File to a Previous Version

  1. Find the commit hash where the file was in the desired state:

    git log -- <file-path>
    
  2. Checkout the file from the specific commit:

    git checkout <commit-hash> -- <file-path>
    

Example

git checkout abc1234 -- path/to/file.txt

This restores file.txt to its state in commit abc1234.

Undoing the Last Commit

You can undo the last commit using git reset or git revert.

Example: Using Git Reset to Undo a Commit

  1. Use git reset to undo the last commit:

    git reset --soft HEAD~1
    

    or

    git reset --hard HEAD~1
    
    • --soft: Keeps changes in the staging area.
    • --hard: Discards changes.

Example

git reset --soft HEAD~1

This undoes the last commit but keeps the changes staged.

Example: Using Git Revert to Undo a Commit

  1. Use git revert to create a new commit that undoes the changes of the specified commit:

    git revert <commit-hash>
    

Example

git revert abc1234

This creates a new commit that undoes the changes made in commit abc1234.

Useful Tips

  • Regular Backups: Regularly push your branches to a remote repository to ensure that you have backups.
  • Use Reflog: The reflog is a powerful tool for recovering lost commits and branches. Use it to find the commit hashes you need.
  • Commit Messages: Use descriptive commit messages to make it easier to identify specific commits in the log.

Summary

Recovering in Git involves using various commands and techniques to restore deleted branches, lost commits, and overwritten files. By understanding how to use reflog, reset, revert, and other Git tools, you can effectively recover from common issues and maintain a clean and reliable repository.