DEV Community

Cover image for Rewind to a Specific Commit in Git
Labby for LabEx

Posted on

Rewind to a Specific Commit in Git

Introduction

This article covers the following tech skills:

Skills Graph

πŸ§‘β€πŸ’» New to Git or LabEx? We recommend starting with the Quick Start with Git course.

Git is a powerful version control system that allows developers to track changes made to their codebase. One of the most useful features of Git is the ability to rewind back to a specific commit. This can be helpful when you need to undo changes or revert to an earlier version of your code.

Rewind to a Specific Commit

As a developer, you may need to undo changes made to your codebase. For example, you may have made a mistake and need to go back to an earlier version of your code. In this challenge, you will use Git to rewind back to a specific commit in a repository.

To complete this lab, you will use the Git repository git-playground from https://github.com/labex-labs/git-playground.git. Follow these steps to complete the challenge:

  1. Clone the repository to your local machine:
git clone https://github.com/labex-labs/git-playground.git
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the repository:
cd git-playground
Enter fullscreen mode Exit fullscreen mode
  1. View the commit history of the repository:
git log --oneline
Enter fullscreen mode Exit fullscreen mode
  1. Make sure that the commit message you want to rewind to is the "Initial commit" commit hash.
  2. Use the command git reset <commit> to rewind back to the specified commit. For example, you want to rewind back to the commit with hash 3050fc0d3:
git reset 3050fc0d3
Enter fullscreen mode Exit fullscreen mode
  1. View the commit history of the repository again:
git log --oneline
Enter fullscreen mode Exit fullscreen mode
  1. If you want to delete the changes and revert to the earlier version of your code, use the command git reset --hard <commit>. For example, you want to delete the changes and revert to the commit with hash c0d30f305:
git reset --hard c0d30f305
Enter fullscreen mode Exit fullscreen mode

This is the result of running git log --oneline:

c0d30f305 (HEAD -> master) Initial commit
Enter fullscreen mode Exit fullscreen mode

Summary

Rewinding back to a specific commit is a useful feature of Git that allows developers to undo changes or revert to an earlier version of their code. In this lab, you used Git to rewind back to a specific commit in a repository. Remember to use git reset to rewind back to a specific commit and git reset --hard to delete changes and revert to an earlier version of your code.


πŸš€ Practice Now: Rewind to a Specific Commit


Want to Learn More?

Top comments (0)