Discard local changes and untracked files in Git repo |Video upload date:  · Duration: PT3M59S  · Language: EN

Fast guide to discard local changes and remove untracked files from a Git repo using reset and clean commands

If your working tree looks like a junk drawer and you just want a fresh start without soul crushing regret later this guide will help. We will walk through checking the repo status resetting tracked files and removing untracked clutter while giving you safe escape hatches.

Check the repo status before you wreck things

Run git status to see modified tracked files and untracked files. This command shows what is about to be nuked. If any filenames look important take a breath and read them.

Reset tracked files back to the last commit

When you want to abandon local edits and return the working tree to the last commit use

git reset --hard HEAD

This forces the index and working tree to match HEAD and overwrites local changes. If that sounds terrifying and it should you have a couple of safer options.

  • Stash your work with git stash push -m "save" so you can recover it later.
  • Create a temporary branch and commit your work there with git checkout -b temp-save or git switch -c temp-save.

Remove untracked files and directories

To delete files that are not tracked by Git use the clean command. Be warned this does not go to the recycle bin.

git clean -fd

To preview what will be deleted run a dry run first. This shows the targets without removing anything.

git clean -nd

If you also want to remove files that are ignored by .gitignore add the extra flag but only use it when you really mean it.

git clean -fdx

Recovery options if you panic

If you stashed changes restore them with git stash pop or git stash apply. If you committed to a temp branch you can merge or cherry pick the good bits back into your main branch.

Quick checklist

  • Run git status and actually read the file list
  • Stash or commit to a temp branch if anything matters
  • Run git clean -nd to preview deletions
  • Run git reset --hard HEAD only when you are sure

Bottom line do not be cavalier. These commands are powerful and final by default. Use the dry run and stash tricks to make sure you do not accidentally delete something you will regret 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.