How do I clear my local working directory in git?

This question already has an answer here:

  • How to remove local (untracked) files from the current Git working tree? 31 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)
  • Stackoverflow question "How do you remove untracked files from your git working copy?"

  • git clean -df
    

    Edit: It's not well advertised but git clean is really handy. Git Ready has a nice intro to git clean .

    Update: removed the x flag based on the suggestion in the comment below


    All the answers so far retain local commits. If you're really serious, you can discard all local commits and all local edits by doing:

    git reset --hard origin/branchname
    

    For example:

    git reset --hard origin/master
    

    This makes your local repository exactly match the state of the origin (other than untracked files).

    If you accidentally did this after just reading the command, and not what it does :), use git reflog to find your old commits.

    链接地址: http://www.djcxy.com/p/3264.html

    上一篇: 告诉git可以删除未跟踪的文件

    下一篇: 如何在git中清除我的本地工作目录?