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

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

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

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

    Use git clean -f -d to make sure that directories are also removed.

    1. Don’t actually remove anything, just show what would be done.

      git clean -n
      

      or

      git clean --dry-run
      
    2. 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 the -f option twice if you really want to remove such a directory.

      git clean -fd
      

    You can then check if your files are really gone with git status.

    0 讨论(0)
  • 2020-11-22 01:01

    git clean -fd removes directory

    git clean -fX removes ignored files

    git clean -fx removes ignored and un-ignored files

    can be used all above options in combination as

    git clean -fdXx

    check git manual for more help

    0 讨论(0)
  • 2020-11-22 01:02

    A better way is to use: git clean

    git clean -d -x -f
    

    This removes untracked files, including directories (-d) and files ignored by git (-x).

    Also, 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.

    0 讨论(0)
  • 2020-11-22 01:04

    I am surprised nobody mentioned this before:

    git clean -i
    

    That stands for interactive and you will get a quick overview of what is going to be deleted offering you the possibility to include/exclude the affected files. Overall, still faster than running the mandatory --dry-run before the real cleaning.

    You will have to toss in a -d if you also want to take care of empty folders. At the end, it makes for a nice alias:

    git iclean
    

    That being said, the extra hand holding of interactive commands can be tiring for experienced users. These days I just use the already mentioned git clean -fd

    0 讨论(0)
  • 2020-11-22 01:04

    Simple Way to remove untracked files

    To remove all untracked files, The simple way is to add all of them first and reset the repo as below

    git add --all
    git reset --hard HEAD
    

    0 讨论(0)
  • 2020-11-22 01:06

    To remove the untracked files you should first use command to view the files that will be affected by cleaning

    git clean -fdn
    

    This will show you the list of files that will be deleted. Now to actually delete those files use this command:

    git clean -fd
    
    0 讨论(0)
提交回复
热议问题