How to Restore a Deleted Branch or Commit with Git

Accidentally deleting a Git branch or losing an important commit is a common mistake, especially when working in fast-paced development environments. The good news is that Git is designed with powerful recovery mechanisms that make it possible to restore deleted branches and commits in most cases.

This comprehensive guide explains how to restore a deleted branch or commit with Git using beginner-friendly explanations, real-world examples, and practical code samples. By the end of this article, you will understand Git recovery concepts and confidently recover lost work.

Understanding Git History and Recovery

Before learning how to restore deleted branches or commits in Git, it is important to understand how Git stores history.

  • Git does not immediately delete commits when a branch is removed
  • Commits remain in Git’s object database for a certain period
  • Git references (branches and tags) point to commits

This design allows Git to recover lost commits using tools like git reflog and commit hashes.

Common Scenarios Where Git Data Is Lost

Here are real-world situations where developers often need Git recovery:

  • Accidentally deleting a local branch
  • Deleting a remote branch by mistake
  • Resetting or rebasing and losing commits
  • Force-pushing changes that overwrite history

How to Restore a Deleted Branch with Git

Using git reflog to Recover a Deleted Branch

The most reliable way to restore a deleted branch in Git is by using git reflog. The reflog records every movement of HEAD, even if a branch was deleted.

git reflog

This command displays a list of recent actions, including commits that are no longer visible in the branch history.

Example Output

a1b2c3d HEAD@{0}: checkout: moving from feature-x to main f4e5d6c HEAD@{1}: commit: Added payment validation

In this example, the commit hash f4e5d6c belongs to the deleted branch.

Recreating the Deleted Branch

Once you identify the commit hash, recreate the branch using the following command:

git checkout -b feature-x f4e5d6c

This restores the deleted branch and points it to the last known commit.

How to Restore a Deleted Commit with Git

Recovering a Lost Commit Using git reflog

If a commit was lost due to reset or rebase, git reflog is again the best solution.

git reflog

Find the commit hash and restore it using one of the following methods.

Option 1: Restore Commit Using git checkout

git checkout f4e5d6c

This places you in a detached HEAD state. You can then create a new branch.

git checkout -b recovered-commit

Option 2: Restore Commit Using git cherry-pick

If you want to apply the lost commit to your current branch:

git cherry-pick f4e5d6c

This is useful when selectively restoring commits.

Restoring a Deleted Remote Branch

If the branch was deleted locally but still exists on the remote repository:

git fetch origin git checkout -b feature-x origin/feature-x

If the branch was deleted on the remote but still exists locally, push it back:

git push origin feature-x

Comparison of Git Recovery Methods

Scenario Best Command Use Case
Deleted local branch git reflog Recover recently deleted branches
Lost commit after reset git cherry-pick Apply specific commits
Remote branch recovery git fetch Restore remote references


Learning how to restore a deleted branch or commit with Git is an essential skill for every developer. Thanks to Git’s robust history tracking, most deleted branches and commits can be recovered using tools like git reflog, git checkout, and git cherry-pick.

By understanding Git internals and following best practices, you can confidently recover lost work and minimize development disruptions.

Frequently Asked Questions (FAQs)

1. Can Git always restore a deleted branch?

Git can restore a deleted branch as long as the commit objects have not been garbage-collected. Using git reflog increases the chances of recovery.

2. How long does Git keep deleted commits?

By default, Git keeps unreachable commits for around 30 days, depending on garbage collection settings.

3. What is git reflog used for?

git reflog tracks changes to HEAD and branch pointers, making it the most powerful tool for Git recovery.

4. Can I restore a deleted commit without reflog?

If you know the commit hash, you can restore it directly. Without reflog or the hash, recovery becomes difficult.

5. Is restoring a deleted branch safe?

Yes, restoring a deleted branch is safe because it does not modify commit history, only recreates references.

line

Copyrights © 2024 letsupdateskills All rights reserved