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.

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Introduces 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

  1. Exercise: Create a new file, add some content, and then stash your changes.
    Solution: echo "Some content" > file.txt ; git stash

  2. 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

  3. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help