Git Commands Tutorials and Example: Git Clean – How to clean git workspace?

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

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x