Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.4k points)

How can I clear my working directory in git?

1 Answer

0 votes
by (27.5k 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!

29.3k questions

30.6k answers


104k users

Browse Categories