If your working tree looks like a crime scene and you are not sure which files to keep, breathe and run git status
. This shows modified staged and untracked files and prevents deleting something that you actually meant to keep. Treat this command as your safety net and your therapist.
Run the single line check to see what is going on. It takes two seconds and saves you from heroic mistakes.
git status
If a tracked file has local edits that you want to throw away and pretend they never happened use git restore
. This replaces the working copy with the version from your last commit so the file matches history again.
git restore path/to/file
Keep in mind this overwrites local edits. If you might regret this later stash first or make a quick patch branch.
If you added something to the index by mistake use restore to move it out of staging while keeping the working copy intact. This is the gentle undo move.
git restore --staged path/to/file
When build artifacts or forgotten temp files clutter the tree use git clean
to nuke them. Always preview first unless you enjoy surprises.
git clean -n
git clean -f
git clean -fd
Do not run these commands from the wrong directory if you value your free time.
If you want the working tree and index to match the last commit exactly use reset hard. This is the nuclear option. It will discard all local modifications for tracked files so double check git status
first.
git reset --hard
When you want to pause work and not delete it stash is your friend. It shelves changes and lets you switch context without creating a half baked commit history.
git stash push -m 'wip note'
# apply and remove from stash
git stash pop
# list stashed entries
git stash list
git status
to inspect the scenegit restore path/to/file
to discard edits to a tracked filegit restore --staged path/to/file
to unstage without losing editsgit clean -n
to preview untracked removalsgit clean -f
or git clean -fd
to remove untracked files or foldersgit reset --hard
to reset working tree and index to the last commitgit stash push -m 'note'
to shelve work for laterThis short guide covered safe ways to inspect a repo discard single tracked file edits unstage staged changes clean away untracked clutter reset the whole working tree and stash work for later. Use git status
as your first stop and pick the command that matches your bravery level. If in doubt stash and live to regret less later.
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.