> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Recovering Data by Using Git Commands

> This guide explains how to recover lost Git data using reflog and various Git commands.

Git’s built-in history tracking and reflog functionality serve as a powerful “time machine” for your repository. In this guide, you’ll learn how to:

* Restore deleted commits
* Undo recent commits
* Recover deleted branches

These techniques help you safely navigate and recover from mistakes in your Git workflow.

## 1. Restoring a Deleted Commit

When a commit disappears (e.g., via a force-push or a branch deletion), Git’s reflog can track its SHA-1 hash.

```bash theme={null}
git reflog
```

This displays a chronological list of all `HEAD` movements. Locate the desired commit hash in the reflog output.

```bash theme={null}
git checkout <commit-hash>
```

You’re now in a “detached HEAD” state. To preserve this commit on a branch:

```bash theme={null}
git checkout -b restore-branch <commit-hash>
```

This creates a new branch named `restore-branch` pointing at the recovered commit.

<Callout icon="lightbulb" color="#1CB2FE">
  By default, Git keeps reflog entries for 90 days. If you don’t find your commit, it may have been pruned. Configure retention with [`gc.reflogExpire`](https://git-scm.com/docs/git-config#Documentation/git-config.txt-gcreflogExpire).
</Callout>

## 2. Undoing the Last Commit

If you simply want to undo the very last commit on your current branch, use `git reset`. Choose between a **soft** or **hard** reset based on whether you need to preserve your worktree and index.

| Reset Type | Description                                      | Command                   |
| ---------- | ------------------------------------------------ | ------------------------- |
| Soft       | Undo commit, keep changes staged                 | `git reset --soft HEAD~1` |
| Mixed      | Undo commit, unstage changes (default behavior)  | `git reset HEAD~1`        |
| Hard       | Undo commit and discard staged & working changes | `git reset --hard HEAD~1` |

### 2.1 Soft Reset

```bash theme={null}
git reset --soft HEAD~1
```

* Moves the branch pointer back by one commit.
* Leaves your working directory and index untouched.

### 2.2 Hard Reset

```bash theme={null}
git reset --hard HEAD~1
```

* Moves the branch pointer back by one commit.
* Resets both your index and working directory to the new `HEAD`.

<Callout icon="triangle-alert" color="#FF6B6B">
  `git reset --hard` irreversibly discards all uncommitted changes. Make sure you really want to lose those changes.
</Callout>

For more on reset modes, see [Git Reset Documentation](https://git-scm.com/docs/git-reset).

## 3. Recovering a Deleted Branch

Accidentally deleted a branch? You can bring it back if its commits still exist in the reflog.

1. View the reflog:

   ```bash theme={null}
   git reflog
   ```

2. Find the commit hash where your branch last pointed.

3. Recreate the branch:

   ```bash theme={null}
   git checkout -b <branch-name> <commit-hash>
   ```

Your deleted branch is now restored, complete with its history.

***

## Links and References

* [Git Reflog](https://git-scm.com/docs/git-reflog)
* [Git Checkout](https://git-scm.com/docs/git-checkout)
* [Git Reset](https://git-scm.com/docs/git-reset)
* [Git Configuration](https://git-scm.com/docs/git-config)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/az-400/module/e7d3282b-80bc-4acd-8009-2fcf5dee0c86/lesson/a2a881b2-be7c-4537-8387-a94c9d93be60" />
</CardGroup>
