Rewind to a Specific Commit

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12756{{"`Rewind to a Specific Commit`"}} end

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
  1. Navigate to the repository:
cd git-playground
  1. View the commit history of the repository:
git log --oneline
  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
  1. View the commit history of the repository again:
git log --oneline
  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

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

c0d30f305 (HEAD -> master) Initial commit

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.

Other Git Tutorials you may like