Git Undo Operations (original) (raw)

Last Updated : 7 May, 2026

Undoing in Git refers to reversing changes in a repository, similar to an undo operation in a text editor. It helps restore previous states and correct mistakes efficiently.

Common Scenarios for Undoing the Changes

Common scenarios include undoing staged, unstaged, committed, or merged changes to restore the desired project state.

Git Undo Operations

Return to a Previous Commit

Allows moving HEAD to an earlier commit to revisit or modify history.

**1. Move to a Specific Commit (Detached HEAD)

Moves HEAD to a specific commit without affecting branches.

git checkout

**Example:

git checkout B

Screenshot-2026-03-19-141826

Output

**2. Create a Branch from That Commit

Creates a new branch from a previous commit to continue work safely.

git checkout -b

**Example:

git checkout -b new-branch B

Screenshot-2026-03-19-142606

Output

**3. Reset While Keeping Changes Staged

Moves HEAD back while keeping changes in the staging area.

git reset --soft

**Example:

git reset --soft C

Screenshot-2026-03-19-143154

Output

**4. Reset and Keep Changes Unstaged

Moves HEAD back and keeps changes in working directory.

git reset HEAD~

**Example:

git reset HEAD~2

Screenshot-2026-03-19-144041

Output

**5. Reset and Discard Changes Completely

Moves HEAD back and deletes all changes after that commit.

git reset --hard

**Example:

git reset --hard C

Screenshot-2026-03-19-145334

Output

Undoing Local Changes

Reverts modifications in the working directory before committing.

**1. Undo Changes in a File

Restores a specific file to its last committed version.

git checkout --

**Example:

git checkout -- file.txt

Screenshot-2026-03-19-152009

Output

**2. Undo All Local Changes

Discards all changes in the working directory.

git checkout -- .

**Example:

Screenshot-2026-03-19-152310

Output

Using Reflog (Recovery Mechanism)

Tracks all HEAD movements and helps recover lost commits.

**1. View Reflog History

Displays history of HEAD changes.

git reflog

**Example:

Screenshot-2026-03-19-154105

Output

**2. Restore Previous State

Moves repository back to a previous state.

git reset --hard

**Example:

git reset --hard HEAD@{1}

Screenshot-2026-03-19-154337

Output

Undoing Merge

A merge combines changes from one branch into another. Undoing it depends on whether the merge has been pushed and whether you want to keep history intact.

**1. Not Pushed

Removes merge commits before pushing to remote.

**Undo Merge Commit

Removes the latest merge commit.

git reset --hard HEAD~

**Example:

Screenshot-2026-03-19-155532

Output

**2. Already Pushed

Safely reverses a merge commit without rewriting history.

**Revert Merge Commit

Reverses the changes introduced by a merge.

git revert - m 1

**Example:

git revert -m 1 M

**Note: Running git revert opens an editor to edit the commit message. You can accept the default message or use --no-edit to skip the editor.

Screenshot-2026-03-19-163505

Output

Reverting Commits

Creates a new commit to undo changes from previous commits.

**1. Revert Last Commit

Creates a new commit undoing the latest commit.

git revert HEAD

**Example:

Screenshot-2026-03-19-165449

Output

**2. Revert Specific Commit

Reverses changes from a selected commit.

git revert

**Example:

git revert B

Screenshot-2026-03-19-171231

Output