How to remove local (untracked) files from the current Git working tree

前端 未结 30 1903
死守一世寂寞
死守一世寂寞 2020-11-22 00:07

How do you delete untracked local files from your current working tree?

相关标签:
30条回答
  • 2020-11-22 00:44

    User interactive approach:

    git clean -i -fd
    
    Remove .classpath [y/N]? N
    Remove .gitignore [y/N]? N
    Remove .project [y/N]? N
    Remove .settings/ [y/N]? N
    Remove src/com/arsdumpgenerator/inspector/ [y/N]? y
    Remove src/com/arsdumpgenerator/manifest/ [y/N]? y
    Remove src/com/arsdumpgenerator/s3/ [y/N]? y
    Remove tst/com/arsdumpgenerator/manifest/ [y/N]? y
    Remove tst/com/arsdumpgenerator/s3/ [y/N]? y
    

    -i for interactive
    -f for force
    -d for directory
    -x for ignored files(add if required)

    Note: Add -n or --dry-run to just check what it will do.

    0 讨论(0)
  • 2020-11-22 00:45

    git clean -f -d -x $(git rev-parse --show-cdup) applies clean to the root directory, no matter where you call it within a repository directory tree. I use it all the time as it does not force you to leave the folder where you working now and allows to clean & commit right from the place where you are.

    Be sure that flags -f, -d, -x match your needs:

    -d
           Remove untracked directories in addition to untracked files. If an
           untracked directory is managed by a different Git repository, it is
           not removed by default. Use -f option twice if you really want to
           remove such a directory.
    
    -f, --force
           If the Git configuration variable clean.requireForce is not set to
           false, git clean will refuse to delete files or directories unless
           given -f, -n or -i. Git will refuse to delete directories with .git
           sub directory or file unless a second -f is given. This affects
           also git submodules where the storage area of the removed submodule
           under .git/modules/ is not removed until -f is given twice.
    
    -x
           Don't use the standard ignore rules read from .gitignore (per
           directory) and $GIT_DIR/info/exclude, but do still use the ignore
           rules given with -e options. This allows removing all untracked
           files, including build products. This can be used (possibly in
           conjunction with git reset) to create a pristine working directory
           to test a clean build.
    

    There are other flags as well available, just check git clean --help.

    0 讨论(0)
  • 2020-11-22 00:45

    For me only following worked:

    git clean -ffdx
    

    In all other cases, I was getting message "Skipping Directory" for some subdirectories.

    0 讨论(0)
  • 2020-11-22 00:48

    If untracked directory is a git repository of its own (e.g. submodule), you need to use -f twice:

    git clean -d -f -f

    0 讨论(0)
  • 2020-11-22 00:48

    OK, deleting unwanted untracked files and folders are easy using git in command line, just do it like this:

    git clean -fd
    

    Double check before doing it as it will delete the files and folders without making any history...

    Also in this case, -f stands for force and -d stands for directory...

    So, if you want to delete files only, you can use -f only:

    git clean -f
    

    If you want to delete(directories) and files, you can delete only untracked directories and files like this:

    git clean -fd
    

    Also, you can use -x flag for including the files which are ignored by git. This would be helpful if you want to delete everything.

    And adding -i flag, makes git asking you for permission for deleting files one by one on the go.

    If you not sure and want to check things first, add -n flag.

    Use -q if you don't want to see any report after successful deletion.

    I also create the image below to make it more memorable, especially I have seen many people confuse -f for cleaning folder sometimes or mix it up somehow!


    0 讨论(0)
  • 2020-11-22 00:48
    git clean -f
    

    will remove the untracked files from the current git

    git clean -fd
    

    when you want to remove directories and files, this will delete only untracked directories and files

    0 讨论(0)
提交回复
热议问题