Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear my local working directory in Git? [duplicate]

Tags:

git

How can I clear my working directory in Git?

like image 773
Hans Sjunnesson Avatar asked Mar 23 '09 13:03

Hans Sjunnesson


People also ask

How do I clear my working directory?

The easiest way to clear your Git cache is to use the “git rm” command with the “–cached” option. You can choose to remove one file or to remove an entire working directory.

Will git reset delete local files?

Remember, your code repository holds the main branch of a project. Running git reset will typically delete files and commits. And without those, you don't have a project! This is why it's critical to plan ahead when using it, so you don't end up deleting elements that are critical for your project.

What does git clean do?

Summary. To recap, git clean is a convenience method for deleting untracked files in a repo's working directory. Untracked files are those that are in the repo's directory but have not yet been added to the repo's index with git add .


2 Answers

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 -- <file>..." 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 (warning: using the -x flag as below will cause Git to delete ignored files):

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.

Relevant links:

  • git-reset man page
  • git-clean man page
  • git ready "cleaning up untracked files" (as Marko posted)
  • Stack Overflow question "How to remove local (untracked) files from the current Git working tree")
like image 87
dbr Avatar answered Sep 20 '22 15:09

dbr


Use:

git clean -df 

It's not well advertised, but git clean is really handy. Git Ready has a nice introduction to git clean.

like image 42
Marko Avatar answered Sep 20 '22 15:09

Marko