How to check out an old commit in Git

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

Git is a powerful version control system that tracks changes in the source code throughout the development lifecycle. One of the most substantial benefits of using Git is its capacity for revisiting earlier points in a project’s history. This tutorial illuminates the methods to check out old commits in Git, guiding users through a series of scenarios from rudimentary to intricate.

Before delving into the process, ensure you have Git installed on your system and possess a basic comprehension of operating within a Git repository. Familiarity with command-line tools is advantageous for following along.

Basic Checkout

To commence, it’s vital to understand the simple operation of checking out a specific commit. This action sets your repository to the state it was in at the time of that commit. Initiating this is straightforward:

  1. Identify the commit hash you wish to check out. You can find it by using:
    git log
    This command shows a list of recent commits, including their hashes.
  2. Checkout the commit by running:
    git checkout [commit-hash]
    Replace [commit-hash] with the full or abbreviated hash of the commit you desire to inspect.

For example:

git checkout 1a2b3c4d

Your repository now mirrors the state it displayed at the time of the inputted commit. Bear in mind, you’re now in a ‘detached HEAD’ state, a scenario where any new commits you create won’t belong to any branch.

Checking out to a new branch

If you plan to make changes or want to keep the state of that old commit, it’s advisable to create a new branch:

  1. Perform the checkout command with the additional branch flag to create a new branch and switch to it automatically:
    git checkout -b [new-branch-name] [commit-hash]

For instance:

git checkout -b feature-backup 1a2b3c4d

Any changes committed will now reside on this new branch, securing the master branch from any direct amendments.

Navigating through History with Git Reset

An alternative method to reflect an earlier state is to use git reset. This has three main modes: –soft, –mixed, and –hard.

  1. To preserve your changes but reset the commit history, use –soft:
    git reset --soft [commit-hash]
  2. If you need to discard changes and reset the staging area, choose –mixed:
    git reset --mixed [commit-hash]
  3. To completely discard all changes and align your working directory with the past commit, select –hard:
    git reset --hard [commit-hash]

For example, to strictly align your repository to an old commit, you’d use:

git reset --hard 1a2b3c4d

Warning: The –hard option will permanently erase changes. Proceed with caution.

Advanced Usage: Reflog and Reverting

Git reflog is a command that lists changes to the local repository’s HEAD. If you’ve lost track of an old state, reflog can prove to be quite beneficial.

git reflog
This command displays each movement of the HEAD pointer, permitting you to identify the commit before a critical change.

To revive that state, perform a checkout:

git checkout [commit-hash]

Another approach is using git revert, which negates the effects of one specific commit:

git revert [commit-hash]

The aforementioned command will prompt you to create a new commit that reverses the specified one, perpetuating the project’s history while revoking the undesired effects.

Conclusion

This tutorial has covered a range of techniques for revisiting old commits in a Git repository. Ranging from the elementary use of git checkout to the more sophisticated traversal with git reset and git reflog, these methods empower developers to efficiently manage their version control. Whether for retrieving forgotten code or rolling back to a stable iteration, these Git capacities are invaluable tools for software development.