gerrit-ssh方式拉取代碼操作
- 提前準(zhǔn)備
- 簡單說明過程
- 可能用到的命令
- 過程
- 生成公私秘鑰gerrit
- 配置公鑰git
- 配置user.name 和 user.email
- 獲取代碼測試
5.注意事項(xiàng)配置多個公私鑰匙
1. 提前準(zhǔn)備
gerrit賬戶【賬戶已經(jīng)添加郵箱】
-
可執(zhí)行ssh-keygen命令的工具蝠检,windows系統(tǒng) 推薦 git for windows
2. 簡單說明過程
- 生成公私秘鑰
- gerrit配置公鑰
- git 配置user.name 和 user.email
- 獲取代碼測試
3. 可能用到的命令
# 切換到.ssh目錄
cd ~/.ssh
# 查看文件 ~/.ssh/config
cat ~/.ssh/config
# 生成公私秘鑰
ssh-keygen
# 配置user.name []里的東西選填
git config [--global] user.name "example"
# 配置user.email []里的東西選填
git config [--global] user.email "xxx@xx.xx"
# 從代碼倉庫首次獲取代碼
git clone ssh://example@xxxx/xxx
# 代碼倉庫獲取更新
git pull
# 代碼已提交到本地并無任何沖突,提交代碼
git push
4. 過程
生成公私秘鑰
-
【注意】
若已經(jīng)存在一些鑰匙
需要添加新鑰匙
- 見注意事項(xiàng)【配置多個公私鑰匙】
-
進(jìn)入用戶ssh配置目錄
若安裝ssh工具雷绢,則會有~/.ssh這個目錄
若無則創(chuàng)建,并進(jìn)入
-
命令
cd ~/.ssh
-
生成公私秘鑰
ssh-keygen有很多參數(shù)如需其他操作仓洼,請自行百度了解
-
命令【可加其他參數(shù)】
ssh-keygen
-
默認(rèn)生成公私秘鑰會在用戶【家目錄】下的【.ssh】目錄下座韵,內(nèi)容有
- 【私鑰】id_rsa
- 【公鑰】id_rsa.pub
gerrit配置公鑰
- 到達(dá)ssh配置位置
- 打開并登錄gerrit網(wǎng)站
- 點(diǎn)擊【頭像】
- 點(diǎn)擊【settings】
- 點(diǎn)擊左邊菜單【ssh keys】
- 添加公鑰
- 將上步【生成的公鑰內(nèi)容】復(fù)制進(jìn)去
- 可以通過記事本或者其他編輯器打開公鑰文件進(jìn)行復(fù)制
- 點(diǎn)擊【add new ssh key】
- 若網(wǎng)頁上進(jìn)行了刷新险绘,并且已添加一個ssh-key,則成功
- 將上步【生成的公鑰內(nèi)容】復(fù)制進(jìn)去
git配置user.name 和 user.email
-
目的
ssh的方式誉碴,git 在拉取和提交代碼時宦棺,會進(jìn)行user.name 和 user.emal的校驗(yàn),因此需要配置
-
配置的方式
-
全局方式
- 只需配置一次翔烁,則在任何地方都可用
-
文件夾方式
- 只在相應(yīng)的git本地文件夾中配置
-
命令配置差異
全局:
git config --global user.name "name..."
文件夾
cd 相應(yīng)的文件夾
git config user.name "..."
-
-
例子全局方式
# email 用 gerrit 里面配置的 email 這里使用 example@test.com # name 可以用一個隨便使用 但不可沒有 這里使用 xxx git config --global user.name "xxx" git config --global user.email "example@test.com"
獲取代碼測試
-
在gerrit中獲取有權(quán)限項(xiàng)目ssh地址連接
# example: git clone ssh://example@xxxx/xxx
-
在本地找一個地方執(zhí)行上述命令
- 進(jìn)行一次拉去代碼嘗試
git pull
- 進(jìn)行文件操作并提交到本地倉庫渺氧,知道提交到遠(yuǎn)程倉庫
-
git push
若成功則OK
-
- 進(jìn)行一次拉去代碼嘗試
5.注意事項(xiàng)
配置多個公私鑰匙
若已存在公私鑰并且需要添加公私鑰
# ssh-keygen生成公私鑰 # [] 中內(nèi)容可不加 # -t 選擇加密方式 # -C 指定郵箱 # -f 指定文件【若新生成,文件則為不存在的文件】 ssh-keygen [-t rsa] [-C "xxxx@xx.xx"] -f ~/.ssh/xxx-rsa # 之后會在~/.ssh目錄下生成相關(guān)文件 cd ~/.ssh ls | grep "xxx-rsa" # 上述命令輸出如下 xxx-rsa xxx-rsa.pub # 由于開始已經(jīng)存在了一個公私鑰 # 因此添加新的鑰匙的時候 # 需要在~/.ssh目錄下添加一個config文件進(jìn)行配置 # config用于對所拉取倉庫對應(yīng)的地址的不同公鑰區(qū)分 # 添加文件并編輯 vi ~/.ssh/config # 內(nèi)容 # 第一個代碼倉庫 # host 192.168.1.2 # port 9412 # hostname xxx1.xx # file ~/.ssh/1-rsa # 第二個代碼倉庫 # host 192.168.1.3 # port 9412 # hostname xxx2.xx # file ~/.ssh/xxx-rsa
查看文件內(nèi)容
cat ~/.ssh/config
文件內(nèi)容
Host 192.168.1.2 HostName xxx1.xx Port 29413 PreferredAuthentications publickey IdentityFile ~/.ssh/1-rsa Host 192.168.1.3 HostName xxx2.xx PreferredAuthentications publickey IdentityFile ~/.ssh/xxx-rsa
OK 蹬屹!