Git & GitHub / Git Basics
Undoing and Resetting Changes in Git
This tutorial will guide you through the process of undoing changes and resetting commits in Git, providing you with the ability to easily revert your code to a previous state.
Section overview
5 resourcesIntroduces the fundamental concepts of Git, including version control, commits, and repositories.
1. Introduction
1.1 Goal
The goal of this tutorial is to provide a comprehensive guide on how to undo changes and reset commits in Git. This will empower you to revert your code to a previous state with ease and precision.
1.2 Learning Outcomes
By the end of this tutorial, you will learn:
- Fundamental Git commands for undoing changes (
git stash,git checkout,git reset,git revert) - The difference between these commands and when to use them
- How to navigate through your commit history
1.3 Prerequisites
Basic knowledge of Git and familiarity with the command line are recommended. If you haven't yet installed Git, you can follow the instructions here.
2. Step-by-Step Guide
Git offers several commands for undoing changes. They each serve different purposes, and it's important to understand which to use in different scenarios.
git stash: Temporarily saves changes that you don't want to commit immediately.git checkout: Discards changes in the working directory.git reset: Unstages changes, or optionally discards them.git revert: Undoes a committed snapshot.
3. Code Examples
3.1 Using git stash
# Make some changes in your working directory
echo "Temporary changes" > temp.txt
# Stash changes
git stash
# Shows the list of stashes
git stash list
In the code above, we first make some temporary changes. We then use git stash to save these changes. git stash list shows all the stashed changes.
3.2 Using git checkout
# Make some changes in your working directory
echo "Unwanted changes" > unwanted.txt
# Discard changes in the working directory
git checkout -- unwanted.txt
In this example, we make some unwanted changes and then use git checkout to discard them.
3.3 Using git reset
# Make some changes and stage them
echo "Bad changes" > bad.txt
git add bad.txt
# Unstage changes
git reset
# Discard changes
git reset --hard
In this code, we first stage some changes. We then use git reset to unstage them, and git reset --hard to discard them completely.
3.4 Using git revert
# Commit some changes
echo "Wrong commit" > wrong.txt
git add wrong.txt
git commit -m "Wrong commit"
# Revert the last commit
git revert HEAD
In this example, we commit some changes and then use git revert to create a new commit that undoes those changes.
4. Summary
In this tutorial, we learned about undoing changes and resetting commits in Git. We explored the git stash, git checkout, git reset, and git revert commands, and learned when to use each one.
5. Practice Exercises
-
Exercise: Create a new file, add some content, and then stash your changes.
Solution:echo "Some content" > file.txt ; git stash -
Exercise: Add some content to a file, stage the changes, and then unstage them.
Solution:echo "More content" >> file.txt ; git add file.txt ; git reset -
Exercise: Commit some changes, and then create a new commit that undoes those changes.
Solution:echo "Bad commit" >> file.txt ; git add file.txt ; git commit -m "Bad commit" ; git revert HEAD
Continue practicing these commands until you feel comfortable with them. Remember, the key to mastering Git (or any other tool) is consistent practice. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article