Master Git Basics: 3 Steps, 4 Areas, 5 States and How to Undo Changes
This guide explains Git's core workflow—adding, committing, and pushing—breaks down the four Git areas and five file states, shows how to inspect changes with various git diff commands, and provides step‑by‑step commands to undo modifications at each stage, all illustrated with practical examples.
Basic Concepts
Git has existed for over a decade, yet many users still struggle with its workflow. This article simplifies Git to three basic steps (add, commit, push) on a single master branch, ignoring branches and tags for clarity.
Three Steps
Run git add . to stage all changes.
Run git commit -m "comment" to record the staged changes in the local repository.
Run git push to upload the local commits to the remote repository.
Four Areas
Working Area (Working Directory)
Staging Area (Stage)
Local Repository
Remote Repository
Five States
Origin (Unmodified)
Modified
Staged
Committed
Pushed
Inspecting Changes
All inspection uses git diff with different parameters: git diff – compares Working Area vs. Staging Area. git diff --cached – compares Staging Area vs. Local Repository. git diff master origin/master – compares Local Repository vs. Remote Repository.
Undoing Modifications
Modified but Not Staged
To discard changes in the Working Area: git checkout . or
git reset --hardStaged but Not Committed
To move changes back to the Working Area: git reset or git reset --hard After resetting, you may still need git checkout . to return to the unmodified state.
Committed but Not Pushed
To revert the local commit and align with the remote branch:
git reset --hard origin/masterPushed
If the commit has already been pushed, first reset the local branch, then force‑push the corrected state:
git reset --hard HEAD^ git push -fConclusion
All four undo scenarios can be handled with git reset --hard, often combined with git checkout . or a forced push. Mastering these commands eliminates the fear of making irreversible mistakes in Git.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
