0 votes
1 view
in Devops and Agile by (20k points)

How can I clear my working directory in git?

1 Answer

0 votes
by (28.4k points)

In order to reset a specific file to the last-committed state (to discard uncommitted changes in a specific file):

$ git checkout file_to_reset.txt

In order to reset the entire repository to the last committed state:

$ git reset --hard

In order to remove untracked files, in most cases just deleting all files in the working copy (but not the .git/ folder!), then performing git reset --hard does the work. But a better way is to use git clean: (Warning: using the -x flag as below will cause git to delete ignored files.)

$ git clean -d -x -f

The above command 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !