How to clean workspace in git?
To reset a specific file to the last-committed state (to discard uncommitted changes in a specific file):
git checkout thefiletoreset.txt
This is mentioned in the git status output:
(use “git checkout — …” to discard changes in working directory)
To reset the entire repository to the last committed state:
git reset –hard
To remove untracked files, I usually just delete all files in the working copy (but not the .git/ folder!), then do git reset –hard which leaves it with only committed files.
A better way is to use git clean:
git clean -d -x -f
will remove untracked files, including directories (-d) and files ignored by git (-x). Replace the -f argument with -n to perform a dry-run or -i for interactive mode and it will tell you what will be removed.
you delete local files from your current branch?
git clean -f
But beware… there’s no going back. Use -n or –dry-run to preview the damage you’ll do.
If you want to also remove directories, run git clean -f -d
If you just want to remove ignored files, run git clean -f -X
If you want to remove ignored as well as non-ignored files, run git clean -f -x
Note the case difference on the X for the two latter commands.
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn



I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND