Skip to content
Undoing Changes in Git

Summary - Undoing Changes In Git

Going Back in Time in Git

Introduction

Going Back in Time: Revisit earlier versions of your project.
Purpose: Fix issues, recover lost work, or compare different versions.

Understanding the Concept

Detached HEAD: Viewing a specific commit without being on any branch.
Example: Like reverting to an earlier saved version of a document.

Commands to Explore

git checkout
git clean
git restore
git revert
git reset

Discarding Changes in Git

Introduction

Discarding Changes: Undo recent modifications, restoring files to their original state.
Purpose: Efficiently manage your project's history and correct mistakes.

Key Commands for Discarding Changes

Git Clean: Removes untracked files.
Git Restore: Discards changes or unstages files.
Git Revert: Creates a new commit that undoes changes.

Using Git Clean

Remove Untracked Files:
git clean -n: Preview.
git clean -i: Interactive mode.
git clean -f: Force removal.

Using Git Restore

Unstage Changes:
git restore --staged <file>
Discard Changes:
git restore <file>

Using Git Revert

Undo Specific Commits:
git revert <commit-hash> --no-edit

Introduction to Resetting

Introduction

Resetting in Git: Revert to a previous state by manipulating the staging area and working directory.
Purpose: Undo changes, modify commit histories, and clean up local modifications.

Resetting vs. Committing vs. Checkout

Commit: HEAD and branch pointers move forward.
Checkout: HEAD moves, branch pointer remains.
Reset: HEAD and branch pointers move to a specific commit.

Types of Reset Operations

Soft Reset
Mixed Reset
Hard Reset

Caution and Best Practices

Risk of Data Loss: Especially with --hard.
Altering Shared Branch Histories: Can cause conflicts; communicate with your team.

Soft Reset

Introduction to Soft Reset

Soft Reset: Undo the last commit while keeping all changes staged.
Command: git reset --soft <commit>

Key Concepts

Undoing the Last Commit: Keeps changes staged.
Staging Area: Changes ready for recommit.
Working Directory: Unchanged.

Mixed Reset

Introduction to Mixed Reset

Mixed Reset: Undo the last commit, moving changes to the working directory.
Command: git reset --mixed <commit>

Key Concepts

Undoing the Last Commit: Moves changes to the working directory.
Working Directory: Changes need to be restaged.
Comparison with Soft Reset: Soft keeps changes staged; mixed moves them to the working directory.

Hard Reset

Introduction to Hard Reset

Hard Reset: Completely reverts the project to a previous commit, removing all changes made after that commit.
Command: git reset --hard <commit>

Key Concepts

Commit History: Resets to the specified commit, erases following history.
Working Directory and Staging Area: Removes all changes after the specified commit.
Usage Caution: Can lead to data loss; use carefully.

Comparison Table

Table 1
Mode
Command
Commit History
Staging Area
Working Directory
Use Case
Soft
git reset --soft <commit>
Reset to specified commit
Changes remain staged
Unchanged
Reorganizing commits, preparing for a new commit
Mixed
git reset --mixed <commit>
Reset to specified commit
Changes are unstaged
Unchanged
Editing changes without removing from working directory
Hard
git reset --hard <commit>
Reset to specified commit
Changes are removed
Reset to specified commit
Completely discarding changes, resetting to a previous state
There are no rows in this table
Soft Reset: Keeps changes staged, allowing for easy recommitting.
Mixed Reset: Moves changes back to the working directory for further edits.
Hard Reset: Completely removes changes, resetting the working directory and staging area to a previous commit.
Understanding Git is crucial for effective project management and collaboration. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.