The staging area is where Git politely asks what you mean to commit and what was a momentary lapse of judgement. Look before you commit and you avoid that 2am rollback while muttering swear words. Use git status
and git diff --staged .
to see exactly what is queued up for the next commit. It is the best way to stop surprise commits and preserve your reputation with reviewers and future you.
First do the boring but necessary checks. Run these commands in the command line to confirm what is staged and what is not.
git status
to get a quick summarygit diff --staged
to inspect staged diffsgit diff
to inspect unstaged editsSeeing the diffs will prevent accidental commits of debug prints and other crimes against production.
If you added a file by mistake and want to leave the file changes in your working tree for more tinkering use the modern command
git restore --staged filename
It removes the file from the staging area but keeps your edits in place. If you prefer the old school option use
git reset HEAD filename
Both commands do the same practical thing. Choose whichever makes you feel more cultured.
When you staged too much and need a fresh start you can unstage everything in the current directory. Run one of these
git restore --staged .
git reset HEAD .
Remember that the dot means current directory so double check you are in the right place before nuking your staging area. This will not discard your working changes, it only clears what is queued for commit.
If you do want to throw away local edits entirely use
git restore filename
That restores the file from the index or HEAD depending on context. Use this only when you know you are fine losing local edits. If you feel even a tiny sliver of doubt keep reading.
If you are unsure and want to hide your changes for a while stash them. Stashing is the developer version of sticking things in a closet and hoping they do not explode later. Useful commands
git stash push
to save workgit stash pop
to restore the most recent stashgit stash list
to see your stash itemsStash is a small life insurance policy for developer mistakes. Use it before performing aggressive cleans.
If you want fine grained control stage hunks instead of whole files. This keeps commits focused and makes reviews and reverts much less painful.
git add -p
to stage hunks interactivelygit diff --staged
to confirm staged contentSmaller commits mean fewer mysteries later and fewer awkward explanations to teammates.
git status
and git diff --staged
git restore --staged filename
or git reset HEAD filename
git restore --staged .
or git reset HEAD .
git restore filename
only when you are suregit stash
if you want a safety netFollow these steps and the next person who opens your repo will either be grateful or suspicious. Both outcomes beat the alternative shock of discovering a commit full of accidental debug code.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.