Reset a Git Repo to a Pristine State

Learn this one simple command to reset your git branch back to a pristine state

Sun, 28 Nov 2021

Every once in a while I run into an issue where I need to reset a repo to a specific branch and delete all files that are not tracked in the repo. Normally when this need arises, I delete the repo on my file system and then clone it again. This is a nice simple one liner that will allow you to reset all files and changes to the current branch that you have checked out

Windows Powershell

git reset --hard; git clean -fdx

Linux and Mac

git reset --hard && git clean -fdx

git reset --hard - resets all files that ARE tracked in the repo. This command leaves untracked files alone

git clean -fdx - f is force, d cleans all files and directories and x includes all untracked files

Combining this two commands will reset your repo to match the same exact head stored in origin

Buy Me A CoffeeDigitalOcean Referral Badge
Edward Beazer

Edward Beazer - I just like to build shit. Sometimes I get stuck for hours, even days while trying to figure out how to solve an issue or implement a new feature. Hope my tips and tutorials can save you some time.

DigitalOcean Referral Badge