2016-07-25 64 views
2

我希望使用shell命令从git中的未跟踪文件中删除特定文件。但找遍了,只有像一个解决方案Git Shell:我们如何从未跟踪的文件中删除特定文件

-f - force, 
-d - directories too, 
-x - remove ignored files too. 

让考虑文件像gitignore.gitignore,上午未能每当我试图为这种担忧命令的git RM gitignore.gitignore。会感谢任何预期的结果。

+0

你问如何添加文件到gitignore? – Thilo

回答

11

如果文件没有被git跟踪,那么它不是git的工作。只需使用普通的shell命令,如rm而不是git rm

如果你想删除所有未跟踪的文件,你可以做一个git clean

2

如果你有你的工作树&要删除的任何文件,你会使用git rm

直起doc

git-rm - 从工作树,并从删除文件索引

现在为什么要使用git rm而不是rm用于从您的工作树中移除文件

答案是 - 如果您只是使用rm,则需要使用git add <fileRemoved>来关注它。 git rm只需一步即可完成。

您也可以使用git rm --cached,它将从索引中删除文件(暂存它以便在下一次提交时删除),但保留您的副本在本地文件系统中。

这部分是从https://stackoverflow.com/a/7434558

采取删除您可以使用rm为它包含在你的源代码树跟踪文件未经跟踪的文件,你会使用git rm

1

可能只需要一-n很方便地仔细检查你在做什么。

NAME 
     git-clean - Remove untracked files from the working tree 

OPTIONS 
     -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. 

     -n, --dry-run 
      Don't actually remove anything, just show what would be done. 

但是,如果你有未提交的更改你需要使用git checkout -- .

相关问题