Fundamentals 8 min read

Master Git: Safely Undo Commits and Reset Versions

This guide walks through essential Git habits, explains how to inspect changes, and provides step‑by‑step commands for resetting local or remote commits, handling renamed files, and understanding core concepts like the working directory, repository, staging area, and HEAD pointer.

ITPUB
ITPUB
ITPUB
Master Git: Safely Undo Commits and Reset Versions

Essential Git Habits

Before making changes, always check the state of your working directory with git status. If files are modified, use git diff to view the exact differences.

Viewing History and Commit IDs

Use git log to list commits, or git log --pretty=oneline for a concise view. The long hexadecimal strings shown (e.g., 144b8216657d) are commit IDs generated by SHA‑1.

Resetting to a Previous Commit

To discard the most recent commit (when it hasn't been pushed), run: git reset --hard HEAD^ To reset to a specific commit, replace HEAD^ with the desired commit ID: git reset --hard 144b8216 After resetting, the working directory reflects the state of the chosen commit.

Scenario 1 – Undo a Local Commit

If you have committed changes locally but have not pushed them, you can simply reset as shown above. The HEAD pointer moves back, and the files in the working tree are updated accordingly.

Scenario 2 – Undo a Pushed Commit

When the unwanted commit has already been pushed, first reset locally with git reset --hard <commit_id>, then force‑push the corrected history: git push origin <branch_name> --force Be aware that --force overwrites the remote branch, so coordinate with teammates.

Key Git Concepts

Working Directory : The visible files on your computer (e.g., the learngit folder).

Repository (.git) : The hidden folder that stores all commits, the staging area (index), and branch pointers.

Staging Area (Stage/Index) : Where git add places changes before they are committed.

HEAD : A pointer to the current commit on the checked‑out branch (usually master).

Adding and Committing Files

To include new or modified files, run git add <file>. After staging, commit the snapshot with git commit -m "message". This creates a new commit that advances HEAD.

Example workflow:

git status
git add readme.txt LICENSE
git commit -m "Add README and LICENSE"

Running git status again shows a clean working directory and an empty staging area.

Recovering Lost Commit IDs

If you close the terminal and lose the commit ID, use git reflog to list recent HEAD movements and retrieve the needed ID.

Summary

By mastering git status, git diff, git log, git reset, and git push --force, you can safely navigate and correct your project's history, whether changes are local or already shared with a remote repository.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

workflowGitVersion Controlgit commandsresetrevert
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.