Tutorials  /  Linux Basics

Git Reset Tutorial: Undo Commits, Unstage Files & Discard Changes

Ccentron Redaktion · September 2025 ·5 min read ·Linux Basics, Tutorial

The git reset command allows you to undo commits, unstage files, or remove changes from your working directory. It functions by adjusting Git’s three key states: HEAD, Index, and Working Directory. While it is a very powerful tool, it can also be destructive if used carelessly—especially with the --hard option. Below, you’ll learn how it works and when each reset type should be used.

Git Reset Options Overview

Git provides three main reset options. Each one influences the repository’s HEAD, staging area (Index), and working directory differently. The following table summarizes their effects:

Flag HEAD Staging Area Working Directory Summary
--soft Yes No No Keeps changes staged
--mixed Yes Yes No Unstages but preserves file edits
--hard Yes Yes Yes Removes all committed changes
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

git reset --soft

Command Syntax and Breakdown

git reset --soft <commit>

  • git reset: The base command that alters Git history.
  • --soft: Moves the HEAD pointer but leaves the staging area and working directory unchanged.
  • : The target commit (e.g., HEAD~1, a commit hash).

What the Flag Does

  • Moves HEAD to the chosen commit.
  • Leaves changes staged in the index.
  • Does not modify the working directory.

Command Demonstration

Console
$ git log --oneline

Output:

abc1234 Second commit

def5678 Initial commit

Console
$ git reset --soft HEAD~1

This removes abc1234 from history but keeps changes staged. Confirm status:

Console
$ git status

Output:

On branch main

Changes to be committed:

modified: file.txt

Console
$ git commit -m "Amended commit"

This creates a new commit with the same changes, allowing adjustments to the commit message.

git reset --mixed (default)

Command Syntax

git reset --mixed <commit>

  • --mixed: Resets HEAD and staging area to the commit. Leaves working directory unchanged.

Command Demonstration

Console
$ git log --oneline

Output:

abc1234 Second commit

def5678 Initial commit

Console
$ git reset --mixed HEAD~1

This removes the last commit from history, unstages changes, but keeps file edits in the working directory.

Console
$ git status

Output:

On branch main

Changes not staged for commit:

modified: file.txt

Console
$ git add file.txt
Console
$ git commit -m "Revised commit"

This lets you recommit with changes, useful for splitting or revising work.

git reset --hard

Command Syntax

git reset --hard <commit>

  • Resets HEAD, staging area, and working directory to match the target commit.
  • Use with caution, as it permanently discards changes unless recovered with git reflog.

What the Flag Does

  • Moves HEAD to the chosen commit.
  • Clears the staging area.
  • Reverts working directory files to that commit’s state.

Command Demonstration

Console
$ git log --oneline

Output:

abc1234 Buggy commit

def5678 Stable baseline

Console
$ git reset --hard HEAD~1

This discards the buggy commit and restores files to the stable baseline. Confirm with:

Console
$ git status

Output:

On branch main

nothing to commit, working tree clean

Reset Changes in Git

You can use git reset to move your repository back to a chosen commit. Below is the syntax and examples:

git reset [--soft|--mixed|--hard] <commit>

  • --soft: Keeps changes staged.
  • --mixed: Unstages changes, keeps edits.
  • --hard: Discards all changes.

Reset to a Specific Commit

Console
$ git log --oneline

Output:

a1b2c3d Fix typo

9f8e7d6 Add login feature

8e7f6a5 Initial commit

Examples:

  • Soft reset: git reset --soft a1b2c3d
  • Mixed reset: git reset --mixed a1b2c3d
  • Hard reset: git reset --hard a1b2c3d

Use git reflog if you need to recover after an accidental reset.

Undo Reset Changes in Git

Command Syntax

git reflog shows HEAD movements including resets.

Console
$ git reset --hard HEAD@{n}

HEAD@{n} refers to how many steps back (e.g., HEAD@{1} is one action ago).

Command Demonstration

Console
$ git reset --hard HEAD~1
Console
$ git reflog

Output:

abc1234 HEAD@{0}: reset: moving to HEAD~1

def5678 HEAD@{1}: commit: Add login feature

Console
$ git reset --hard HEAD@{1}

This restores the previous state before the reset. git reflog works with all reset types but is most vital with --hard, since it deletes working directory and index changes.

Conclusion

By mastering git reset, you can undo commits, unstage changes, and discard unwanted edits in your project history. Use the reset type suited for your situation:

  • --soft: Undo commits while keeping changes staged.
  • --mixed: Unstage changes while keeping your working directory intact.
  • --hard: Discard all changes and fully reset your repository. Use cautiously.
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren