In GIT, there are the concepts of Working Directory, Staging Area, and HEAD (Last Commit)
Working Directory (WD)
Your actual files on disk — what you see in your code editor. Reflects all changes (staged + unstaged).
Staging Area
A preview zone where you prepare specific changes to be committed. You move changes here using git add.
HEAD
Points to the latest committed snapshot in your current branch, it's your last saved state.
The git restore and reset commands are your saviour when it comes to restoring and resetting your files from these different areas
**
Git restore
**
Let's see how to use Git restore for staged (git add is already executed for these files) and unstaged files.
Suppose you edited file.txt, then:
•You change some content
•You run git add file.txt → file is now staged
•You change it again → now it has staged AND unstaged changes
So:
•Working directory (WD) ≠ Staging area (Index) ≠ Last commit (HEAD)
Let’s explore different restore options 👇
Scenario 1: Discard unstaged local changes
•Removes changes in your working directory
•Keeps what’s staged for commit
git restore file.txt
Scenario 2: Unstage the file (keep working changes)
•Pulls file out of staging
•But keeps the working directory changes
git restore --staged file.txt
OR
git reset file.txt
Scenario 3: Reset file completely (staged + unstaged)
•Fully resets the file to the last commit
•Clears both staging and working changes
git restore --staged --worktree file.txt
OR
git restore --source=HEAD --staged --worktree file.txt
Scenario 4: Bring back a file from another branch
git restore --source origin/main file.txt
Scenario 5: Bring back a file from another commit
git restore --source
**
Git reset
**
Now let's dive into git reset to see how we can use GIT reset
GIT reset moves the HEAD pointer and modifies the staging area and working directory, optionally.
Scenario 1: Undo a commit but keep all changes staged
git reset --soft HEAD~1
•You committed too early but want to keep all changes ready to recommit.
•Commit is undone. Changes stay staged.
Scenario 2: Undo a commit and unstage changes (but keep them)
•You want to cancel the commit and decide which files to stage again.
•Commit is undone. Changes go back to the working directory, no longer staged.
git reset --mixed HEAD~1
Scenario 3: Undo a commit and discard all changes
•You want to completely remove the commit and lose all related changes.
•This deletes changes from both staging and working directory — no going back unless backed up.
git reset --hard HEAD~1
Scenario 4: Unstage specific files (without affecting others)
•You added a file by mistake using git add, and want to unstage it.
•File is removed from staging, but your edits remain.
git reset file.txt
OR
git restore --staged file.txt
Scenario 5: Move to a specific commit (not HEAD~1)
•You want to reset your branch to a known good commit.
•HEAD now points to abc1234, changes after that are moved to working directory.
**
Git stash
**
While restoring and resetting, if you need to stash any changes, let's see a few git commands for stashing
1. List all stashes
git stash list
2. View stash content (diff)
git stash show stash@{0}
3. For a detailed diff:
git stash show -p stash@{0}
4. Restore a stash (apply it)
git stash apply stash@{0}
or just apply the most recent stash:
git stash apply
5. Restore and remove the stash
Use pop to apply and delete the stash in one go:
git stash pop stash@{0}
OR
git stash pop
6. Delete a specific stash
git stash drop stash@{0}
7. Clear all stashes
git stash clear