Git 是一款免費、開源的分布式版本控制系統。這篇文章用來整理我在使用
Git 的過程中知道的一些小技巧。
1. windows 下 git pull / git push 免密碼
雖然網上有很多解決方案,但大多是將用戶名和密碼包含在遠程地址中,然后使用 git config --global credential.helper store
將 credential 信息以明文方式存儲在 .gitconfig 文件同目錄下的 .git-credentials 中,會造成一定的安全隱患,_netrc
也是一樣的。
git-credential-winstore
將 Git 登錄用戶名和密碼保存在 Windows 自帶的憑據管理系統中,比 credential.helper 方式更加安全。
- git-credential-winstore.exe 下載地址
- 安裝方法:如果 Git 路徑已包含在 PATH 環境變量中,直接雙擊運行即可。否則需要在命令行下用 -i 參數指定 Git 路徑,例如:
git-credential-winstore -i C:\path\to\git.exe
- 安裝完成之后首次 git push 時,會彈出 Windows 憑據管理系統的登錄窗口(如下圖所示),填寫用戶名和密碼。以后訪問該版本庫就不用再輸入密碼。
注: (1)git-credential-winstore 項目2015年10月起已不再維護,在 windows7 系統上運行正常,其他系統沒有測試過; (2)新版本的 Git 已經搭載了更好的 credential manager tool: https://github.com/Microsoft/Git-Credential-Manager-for-Windows,
2. 使用 Git Clone 部署項目至 Linux 服務器,項目文件呈現 Modified 狀態
這種情況的出現是因為項目文件在 linux 系統中的權限發生了變化,使用 git config 命令忽略這種變化:
git config core.fileMode false
# 當然如果你想對 全局/系統 級別的 config 進行修改,還可以增加參數
git config --add --global/system core.filemode false
3. .gitignore文件不生效
原因是 .gitignore 只能忽略那些原來沒有被 track 的文件,如果某些文件已經被納入了版本管理中,則修改 .gitignore 是無效的。那么解決方法就是先把本地緩存刪除(改變成未 track 狀態),然后再提交:
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
4. repository 中包含帶有 .git 文件夾的目錄
For example:
warning: adding embedded git repository: vendor/mrgoon/aliyun-sms
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> vendor/mrgoon/aliyun-sms
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached vendor/mrgoon/aliyun-sms
hint:
hint: See "git help submodule" for more information.
這樣會導致代碼托管服務器上沒有 vendor/mrgoon/aliyun-sms
目錄。解決方法正如警告中所提示的:
git rm --cached vendor/mrgoon/aliyun-sms
參考文章:
Git忽略規則及.gitignore規則不生效的解決辦法
使用git-credential-winstore保存https訪問密碼