目錄結構如下
project bin lib src ......
執行如下的操作
git add . git commit -m "add bin/ lib/ src/" git push origin master
突然發現原來 lib 目錄不需要提交到版本庫,但是現在遠程已經存在該目錄,what should I do.(吐出去的東西還能收回來嗎)
萬能的Git啊,help me!
功夫不負有心人,找到了解決問題的方法,其實就是 git rm 的命令行參數。
git rm 命令參數
-n --dry-run Don’t actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command. -r Allow recursive removal when a leading directory name is given. --cached Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
解決方法
git rm -r -n --cached "bin/" //-n:加上這個參數,執行命令時,是不會刪除任何文件,而是展示此命令要刪除的文件列表預覽。 git rm -r --cached "bin/" //最終執行命令. git commit -m" remove bin folder all file out of control" //提交 git push origin master //提交到遠程服務器
此時 git status 看到 bin/目錄狀態變為 untracked
可以修改 .gitignore 文件 添加 bin/ 並提交 .gitignore 文件到遠程服務器,這樣就可以不對bin目錄進行版本管理了。
以后需要的時候,只需要注釋 .gitignore 里 #bin/ 內容,重新執行 git bin/ ,即可重新納入版本管理。
