場景
我們在自己的私有測試分支上調試項目邏輯,給文件做了一些特定的修改,但是文件不想被git提交,不想執行git status命令時出現在modified列表里;再比如,我們本地的數據庫和測試環境的數據庫配置是不一樣的,但是在項目開發中每次提交過程中忽略數據庫配置文件。那么你這里就可以把不想提交的文件忽略。
當然關於git忽略文件的方式有很多,我這里使用的是git update-index --assume-unchanged命令。
代碼舉例
D:\code\sp_edaijia (dev/wangteng/test_20181130 -> origin)
git status //私有分支修改過兩個文件,然后執行git status 命令 On branch dev/wangteng/test_20181130 Your branch is up to date with 'origin/dev/wangteng/test_20181130'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: sp_edaijia/protected/controllers/ApiController.php //文件① modified: sp_edaijia/protected/vendors/ApikeyConfigService.php //文件②
執行忽略命令,注意文件的路徑,否則會報錯
D:\code\sp_edaijia (dev/wangteng/test_20181130 -> origin) git update-index --assume-unchanged sp_edaijia/protected/controllers/ApiController.php //這里忽略ApiController.php 文件
然后再執行git status 命令查看狀態
D:\code\sp_edaijia (dev/wangteng/test_20181130 -> origin) git status On branch dev/wangteng/test_20181130 Your branch is up to date with 'origin/dev/wangteng/test_20181130'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: sp_edaijia/protected/vendors/ApikeyConfigService.php //只有一個文件
no changes added to commit (use "git add" and/or "git commit -a")
OK~忽略完畢~
恢復跟蹤
git update-index --no-assume-unchanged sp_edaijia/protected/controllers/ApiController.php //恢復跟蹤
如果忽略的文件多了,可以使用以下命令查看忽略列表
git uls-files -v | grep '^h\ '
提取文件路徑,方法如下
git ls-files -v | grep '^h\ ' | awk '{print $2}'
所有被忽略的文件,取消忽略的方法,如下
git ls-files -v | grep '^h' | awk '{print $2}' |xargs git update-index --no-assume-unchanged
