How To Go Back to Previous Commits: Understanding Git Reset vs. Git Checkout

Ketan Gupta
2 min readSep 26, 2023

Introduction:

Git provides developers with powerful tools to manage version control, and two common commands for navigating your commit history are `git reset` and `git checkout`. In this tutorial, we’ll explore both methods step by step and help you understand when to choose one over the other.

Choosing the Right Method:

1. Use `git reset` when you want to make permanent changes to your commit history or reset your branch to a previous state. It’s useful for discarding changes or undoing commits.

2. Use `git checkout` when you want to explore a specific commit without affecting your branch’s history. It’s ideal for inspecting code, creating a new branch for experimentation, or temporarily working on a previous version.

Method 1: Using `git reset`

Step 1: Identify the Target Commit
Before you can use `git reset`, you need to identify the specific commit you want to reset to. Use `git log` to view your commit history and find the commit’s hash.

Step 2: Decide on Reset Type
Git reset offers three reset types: soft, mixed, and hard.
- Soft: Moves your branch pointer and staging area to the target commit while keeping your changes in your working directory.
- Mixed (default): Moves your branch pointer to the target commit, resets the staging area, but keeps changes in your working directory.
- Hard: Moves your branch pointer, resets the staging area, and discards all changes made after the target commit.

Step 3: Perform the Reset
Execute the appropriate reset command:

# Soft reset
git reset - soft commit-hash

# Mixed reset (default)
git reset commit-hash

# Hard reset
git reset - hard commit-hash

Method 2: Using `git checkout`

Step 1: Identify the Target Commit
As with `git reset`, begin by identifying the commit you want to navigate to using `git log`.

--

--

Ketan Gupta

Blockchain enthusiast and writer exploring crypto, NFTs and decentralized tech. Providing fresh perspectives and thought-provoking insights. Follow for more.