The Wayback Machine - https://web.archive.org/web/20240830105938/https://www.geeksforgeeks.org/how-to-restore-a-deleted-branch-or-commit-with-git/
Open In App

How to Restore a Deleted Branch or Commit with Git?

Last Updated : 04 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Deleting a branch or a commit in Git might seem like a disaster, especially if it was done unintentionally. However, Git provides robust tools to help you recover deleted branches or commits. Here’s a detailed guide on how to restore a deleted branch or commit in Git.

Understanding the Basics

Git, by design, retains a history of changes which makes it possible to recover deleted branches or commits. The key command is git reflog, which records updates to the tip of branches and other references.

Restoring a Deleted Branch

Step 1: Find the Branch’s Last Commit

First, use git reflog to find the commit at the deleted branch’s tip. git reflog shows a log of all changes to the repository’s branches and can be used to locate the SHA-1 hash of the commit.

1. Open your terminal or command prompt.

2. Navigate to your repository.

3. Run git reflog:

git reflog

This command will display a list of recent actions, including those that affected the branch you deleted. Look through the list to find the commit hash (a long string of characters) associated with the deleted branch.

Step 2: Create a New Branch from the Commit

Once you have identified the commit hash, you can create a new branch pointing to it.

1. Copy the commit hash.

2. Create a new branch from that commit:

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

Example:

git checkout -b restored-branch abc1234

This command creates and switches to a new branch named restored-branch at the commit abc1234.

Restoring a Deleted Commit

If a specific commit was deleted, you can also use git reflog to find and restore it.

Step 1: Identify the Commit

Use git reflog to find the commit hash for the deleted commit:

1. Run git reflog to see recent changes:

git reflog

2. Look for the commit you want to restore. Note the commit hash.

Step 2: Apply the Commit

You have a couple of options to restore the commit:

Option 1: Create a New Branch from the Commit

You can create a new branch starting from the deleted commit:

git checkout -b restored-commit abc1234
3456

git reflog

Option 2: Cherry-Pick the Commit

If you want to apply the commit on top of your current branch, use git cherry-pick:

1. Make sure you are on the branch where you want to apply the commit:

git checkout main

2. Cherry-pick the commit:

git cherry-pick abc1234

This command applies the changes from the commit abc1234 onto your current branch.

Important Points

  • Safety First: Always make sure to verify the commit hash before restoring to avoid confusion or conflicts.
  • Branch Names: Use descriptive branch names when restoring to avoid ambiguity.
  • Backup: Regularly push your branches to a remote repository like GitHub or GitLab to prevent data loss.

Conclusion

Restoring a deleted branch or commit in Git is easy if you know the right commands. By using git reflog, you can track down the necessary commit hashes and use git checkout or git cherry-pick to restore your work.


Previous Article
Next Article

Similar Reads

How to Find And Restore a Deleted File in a Git Repository?
Deleting a file in a Git repository is a common task, whether intentional or accidental. Fortunately, Git's powerful version control system makes it possible to find and restore deleted files with relative ease. This article will walk you through the steps necessary to recover your lost data. Steps to Find and Restore Deleted FilesStep 1: Identify
3 min read
How to Move the Most Recent Commit(s) to a New Branch With Git?
Git offers powerful features for managing your codebase efficiently. Sometimes, you may find yourself needing to move the most recent commit(s) to a new branch in Git. This could be due to various reasons, such as realizing that changes were made on the wrong branch or wanting to isolate experimental work. Fortunately, Git provides simple ways to a
4 min read
How To Move Tag On a Git Branch To Different Commit?
Git is a powerful version control system that allows you to manage and track changes in your codebase. One useful feature of Git is tagging, which allows you to mark specific points in your project's history as important. Sometimes, you might need to move a tag from one commit to another, perhaps due to an error or an update in the code. This artic
2 min read
How to Create Branch From a Previous Commit Using Git?
Creating a branch from a previous commit in Git is a common task that allows developers to revisit and build on a specific point in their project's history. This can be useful for bug fixes, new feature development, or exploring alternate project paths without affecting the current state of the project. In this article, we will explore methods to c
2 min read
Git - Create a Branch From Another Branch
Branching in Git allows developers to work on isolated features or fixes without affecting the main codebase. However, what if you need to create a new branch based on the changes in another branch? That's where the concept of branching from another branch comes into play. In this guide, we'll see the process of creating a branch from another branc
3 min read
How to Make an Existing Git Branch Track a Remote Branch?
Managing branches in Git can sometimes be confusing, especially when you need to make an existing local branch track a remote branch. This process ensures that your local branch can easily fetch updates from and push changes to the corresponding remote branch. In this article, we will walk through the steps to set up tracking for an existing Git br
3 min read
How to Compare a Local Git Branch with its Remote Branch ?
When working with Git, it's often necessary to compare your local branch with its remote counterpart to understand the differences in terms of commits and changes. This helps in keeping your local work in sync with the remote repository and managing potential conflicts. This article will guide you through various methods to compare a local Git bran
3 min read
How to Make the Current Git Branch a Master Branch?
Managing branches effectively is a key part of using Git for version control. Occasionally, you might find yourself in a situation where the current branch you're working on should be the new master branch. This could be due to the current branch evolving to become the main line of development, or simply a restructuring of your project's branch str
3 min read
How to Replace Master Branch with Another Branch in GIT?
In Git, the "master" branch traditionally serves as the primary development branch in many repositories. However, there are situations where you might want to replace the contents of the "master" branch with those of another branch. This could be due to a variety of reasons, such as renaming the default branch to something more inclusive or reflect
2 min read
How to Create a Branch In Git from Another Branch?
Branching is a fundamental aspect of version control systems like Git, which helps developers work on multiple features or bug fixes simultaneously without interfering with the main codebase. Creating a branch from another branch is a common practice, especially when you want to build upon existing work or isolate changes for a specific feature. In
3 min read
Git Merge Hotfix Branch Into Feature Branch
When working on a software project, you may encounter situations where a hotfix needs to be applied to a feature branch. A hotfix typically addresses urgent issues in the codebase that need immediate attention and correction. Integrating these changes into a feature branch ensures that the fixes are incorporated into ongoing development, maintainin
4 min read
How To Replace Local Branch With Remote Branch in Git?
Managing branches in Git can sometimes require drastic measures, such as replacing a local branch entirely with its remote counterpart. This can be useful in scenarios where the local branch has diverged too much from the remote branch, leading to complexities that are easier to resolve by completely overriding the local branch. This article will g
4 min read
Difference Between "git commit" and "git push"?
Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break down their differences. What is git commit?git commit
2 min read
How to Change the Commit Author for a Single Commit?
Changing the author of a single commit in Git can be done using an interactive rebase. This process involves rewriting the commit history, so it's important to proceed carefully, especially if you've already shared the commits with others. Here’s a step-by-step guide on how to change the commit author for a single commit Table of Content Steps to C
3 min read
How to Amend a Commit Without Changing Commit Message?
The git commit --amend command is used to modify the most recent commit. By default, this command opens the commit message editor, allowing you to update the commit message. However, you can amend the commit without changing its message, which can be particularly useful when you need to include forgotten changes or correct a mistake. Table of Conte
2 min read
How to Find a Deleted File in the Project Commit History?
In Software development, it's not uncommon to accidentally delete a file or realize later that a deleted file is still needed. Fortunately, Git's powerful version control system makes it possible to locate and recover deleted files from the project’s commit history. This article will guide you through the steps to find and restore deleted files in
4 min read
How To Move Branch Pointer To Different Commit Without Checkout?
Git pros frequently utilize this strong approach to rebase branches, correct errors, and modify branch histories: shifting the branch pointer to a different commit without checking out. With this approach, you can modify the commit that a branch points to without affecting the status of your working directory. When you need to make corrections to t
1 min read
Difference Between Git remote prune, Git prune and Git fetch --prune
Git is a version control system that helps developers manage and track changes in their codebase. Among its many commands, git remote prune, git prune, and git fetch --prune are essential for maintaining a clean and organized repository. This article will explain the differences between these commands, their syntax, uses, and provide examples to il
4 min read
How To Find Out Which Remote Branch A Local Branch Is Tracking?
Git is a powerful version control system that allows developers to manage and track changes in their code repositories. One of the key features of Git is its ability to track relationships between local and remote branches. Knowing which remote branch a local branch is tracking can help you manage your workflow more efficiently, especially when col
3 min read
What is Git Restore?
In this article, we will learn about a very interesting git command called 'git restore'. We will be discussing different use cases and real-life examples, and in the end, we will see one demo of 'git restore'. Use cases of git restoregit restore help us 'unstage' or even discard 'uncommitted' local changes.It can be used to undo the effect of 'git
2 min read
Git - Filtering the Commit History
Git source code versioning tool provides a lot of features. One of the most important and useful features is log or history. We can use git log command in order to list, filter, view commit history in different ways. Here we will examine git log command usage in detail with examples. List Commit History: We will start with git log command without a
4 min read
How To Revert A Commit With Git Revert?
Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. It is free, open-source, and used by almost 95% of developers across the world. There are many popular offerings of Git reposit
5 min read
How to Back Commit in Git?
In this article, we are covering how to undo commits in Git. Sometimes, you might make mistakes or realize you need to revert changes you made in a previous commit. Luckily, Git offers a few ways to undo commits. Table of Content Approach 1: Using 'git revert'Approach 2: Using 'git reset' Approach 1: Using 'git revert''git revert' is the generally
4 min read
How to Delete Local Commit in Git?
Deleting a local commit in Git can be done in a few ways, depending on whether the commit is the most recent one or if it is further back in the commit history. Here’s an article on how to delete a local commit in Git, tailored to whether you want to keep the changes made in the commit or discard them entirely: Table of Content Keeping the Changes
3 min read
How to Change Commit Message in Git?
Changing a commit message in Git can be done in a few different ways, depending on whether the commit is the most recent one or an earlier commit. Here's an article on how to change a commit message in Git, covering scenarios both before and after the commit has been pushed to a remote repository.These are the following topics that we are going to
3 min read
How to Delete Commit in Git?
Deleting a commit in Git can be done in several ways, depending on whether the commit is local or has already been pushed to a remote repository. Here’s an article on how to delete a commit in Git, covering both recent and older commits, as well as considerations for working with remote repositories. Deleting the Most Recent CommitIf the commit onl
3 min read
How to Remove a Large File from Commit History in Git?
Simply removing a large file through a commit won't truly eliminate it. Git stores all versions of files in its history, even deleted ones, to allow for easy recovery. This can lead to wasted disk space. We will discuss the various approaches for removing a large file from the commit history in Git: Table of Content Using git filter-branchUsing git
4 min read
How to Delete Last Commit in Git?
Git, the popular version control system, provides rich tools for managing changes in your codebase. Sometimes, you may need to delete the last commit due to an error, unnecessary changes, or other reasons. In this article, we will walk you through the steps to safely delete the last commit in Git, whether the commit has been pushed to a remote repo
4 min read
How to list All Files in a Commit in Git?
Working with Git, it's often essential to inspect the contents of your commits. Whether you need to review changes, debug issues, or understand the history of your project, knowing how to list all files in a commit is a fundamental skill. In this article, we’ll walk you through the steps to list all files in a commit in Git, providing you with the
3 min read
How to see the Changes in a Git commit?
Understanding the changes introduced in each commit is crucial for effective collaboration and version control in Git. Whether you are reviewing someone else's work or tracking your own modifications, Git provides powerful tools to inspect changes. This article will guide you through the various methods to see changes in a Git commit. What is a git
4 min read
Article Tags :