2016-07-25 63 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 -- .

相關問題