git http方式鑑權

git的鑑權方式分爲 ssh 和 https,ssh 需要使用 ssh 祕鑰,這樣就可以在不輸入用戶名和密碼的情況下安全地傳輸數據;而http協議來說是不可能的 —— 每一個連接都是需要用戶名和密碼的。 這在使用雙重認證的情況下會更麻煩,因爲你需要輸入一個隨機生成並且毫無規律的 token 作爲密碼。

接下來,我們採用http鑑權的方式來做一個例子:

1、克隆代碼:

git clone https//....
//需要輸入用戶名、密碼

克隆出代碼後,在.git目錄下,查看config文件可以看到使用http的方式。可以通過修改項目的git配置文件: .git/config 中的url部分,從而改變鑑權方式。

[remote "origin"]
    url = https://github.com/youyouzh/admin.git
    fetch = +refs/heads/*:refs/remotes/origin/*

2、add、commit:

當我們在本地對代碼做了修改,需要add、commit。執行commit -m “。。。”後,看到:

$ git commit -m "update**"
[master 0529efa] update**
 Committer: root <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

看提示:git模式使用了系統的用戶名(root)和對應email作爲提交的name、email。這時生成~/.gitconfig 文件,內容和git config --global --edit的輸出一樣 ,內容如下:

# This is Git's per-user configuration file.
[user]
# Please adapt and uncomment the following lines:
#       name = root
#       email = [email protected]

3、push:

提交後,通過push將更改發送到遠程,如下:

git push 
//需要輸入用戶名、密碼

 成功後,我們在gitlab上看到的提交信息是root。

4、修改name、email信息:

我們可以通過git config --global --edit命令(該命令等效直接修改~/.gitconfig文件)來修改name、email;

也可以使用下面兩個命令:

git config --global user.name "Your Name"
git config --global user.email [email protected]

無論通過那種方式做了修改,最終都會體現在~/.gitconfig文件中。這樣,再次修改代碼,add、commit、push後,在gitlab上就會顯示正確的user了。

5、保存密碼:

由於使用的是http方式clone的,所以後面的push、pull都要求輸入用戶名、密碼。如何保存密碼呢?幸運的是,Git 擁有一個憑證系統來處理這個事情。 下面有一些 Git 的選項:

  • 默認所有都不緩存。 每一次連接都會詢問你的用戶名和密碼。
  • “cache” 模式會將憑證存放在內存中一段時間。 密碼永遠不會被存儲在磁盤中,並且在15分鐘後從內存中清除。
  • “store” 模式會將憑證用明文的形式存放在磁盤中,並且永不過期。 這意味着除非你修改了你在 Git 服務器上的密碼,否則你永遠不需要再次輸入你的憑證信息。 這種方式的缺點是你的密碼是用明文的方式存放在你的 home 目錄下。

如果你使用的是 Mac,Git 還有一種 “osxkeychain” 模式,它會將憑證緩存到你係統用戶的鑰匙串中。 這種方式將憑證存放在磁盤中,並且永不過期,但是是被加密的,這種加密方式與存放 HTTPS 憑證以及 Safari 的自動填寫是相同的。

如果你使用的是 Windows,你可以安裝一個叫做 “Git Credential Manager for Windows” 的輔助工具。 這和上面說的 “osxkeychain” 十分類似,但是是使用 Windows Credential Store 來控制敏感信息。 可以在 https://github.com/Microsoft/Git-Credential-Manager-for-Windows 下載。

你可以設置 Git 的配置來選擇上述的一種方式。接下來,我們看一些示例:

1)cache:

$ git config credential.helper cache
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

[work for 5 more minutes]
$ git push http://example.com/repo.git
[your credentials are used automatically]

也可以設置一個超時時間:

$ git config credential.helper 'cache --timeout=300'

https://git-scm.com/docs/git-credential-cache

2)store:

$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

[several days later]
$ git push http://example.com/repo.git
[your credentials are used automatically]

store” 模式可以接受一個 --file <path> 參數,可以自定義存放密碼的文件路徑(默認是 ~/.git-credentials )。

$ git config --global credential.helper 'store --file ~/.my-credentials'

參考:https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%87%AD%E8%AF%81%E5%AD%98%E5%82%A8

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章