Rewind to a Specific Commit in Git
Labby
Posted on November 12, 2024
Introduction
This article covers the following tech skills:
🧑💻 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:
- Clone the repository to your local machine:
git clone https://github.com/labex-labs/git-playground.git
- Navigate to the repository:
cd git-playground
- View the commit history of the repository:
git log --oneline
- Make sure that the commit message you want to rewind to is the "Initial commit" commit hash.
- Use the command
git reset <commit>
to rewind back to the specified commit. For example, you want to rewind back to the commit with hash3050fc0d3
:
git reset 3050fc0d3
- View the commit history of the repository again:
git log --oneline
- 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 hashc0d30f305
:
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.
🚀 Practice Now: Rewind to a Specific Commit
Want to Learn More?
- 🌳 Learn the latest Git Skill Trees
- 📖 Read More Git Tutorials
- 💬 Join our Discord or tweet us @WeAreLabEx
Posted on November 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.