Git 的一些使用小技巧

Git Logo

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訪問密碼

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • 聲明:這篇文章來源于廖雪峰老師的官方網站,我僅僅是作為學習之用 Git簡介 Git是什么? Git是目前世界上最先...
    橫渡閱讀 3,987評論 3 27
  • 文/恰恰天藍 || 邀你一起,生命不息,日更不止 前段時間,侄女在縣城里購置的新房裝修進火,我因工作關系沒能出席,...
    恰恰天藍閱讀 233評論 3 3
  • 52周回顧 1.參加年目標課程 完成 2.做完目前實驗,開始整理數據 完成 3.運動 完成 4.寫日記 未完成 5...
    幸福的小牙閱讀 310評論 0 1
  • 淡水這個名字時常都會在歌中聽見,空閑的禮拜天于是計劃去淡水,一早起床就一直在哼唱著盧廣仲的歌“就快要愛上了淡水~除...
    霹靂酷錘錘閱讀 5,745評論 27 99