一 配置 SSH 密鑰
1 加密算法類型如下所示:
算法類型 | 公鑰 | 私鑰 |
---|---|---|
ED25519 | id_ed25519.pub | id_ed25519 |
RSA | id_rsa.pub | id_rsa |
步驟一: 查看已存在的 SSH 公鑰
- ED25519 算法
cat ~/.ssh/id_ed25519.pub
- RSA 算法
cat ~/.ssh/id_rsa.pub
步驟二: 生成 SSH 密鑰
如果沒有公鑰
- 基于ED25519算法,生成密鑰對命令如下:
ssh-keygen -t ed25519 -C “<注釋內(nèi)容>”
- 基于RSA算法,生成密鑰對命令如下:
ssh-keygen -t rsa -C “<注釋內(nèi)容>”
- 此處需要設(shè)置密碼,密碼支持為空(如果你不想在每次使用 SSH 協(xié)議訪問倉庫時,都要輸入用于保護(hù)私鑰文件的口令,可以在創(chuàng)建密鑰時,輸入空口令。)
步驟三: 在 codeUp 或 github 上設(shè)置公鑰
- 執(zhí)行步驟一 復(fù)制公鑰內(nèi)容
- 粘貼到對應(yīng)網(wǎng)站的設(shè)置地址內(nèi)
二 如果本地需要存儲多個公鑰
如下場景:
- 需要配置兩個CodeUp 和 一個GitHub
解決方案一:
-
打開本地終端,按如下格式編輯~/.ssh/config文件
-
hostName 為域名
-
下載示例用戶2時,需要把下載鏈接中的域名更換為別名
# Codeup 示例用戶1
HostName codeup.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
# Codeup 示例用戶2,設(shè)置別名 codeup-user-2
Host codeup-user-2
HostName codeup.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/codeup_user_2_ed25519
# GitLab 平臺
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_ed25519
- 下載示例:
# 訪問 Codeup,將使用 ~/.ssh/id_ed25519.pub 密鑰
git clone git@codeup.aliyun.com:example/repo.com
# 以 codeup-user-2 別名訪問 Codeup 時,將使用 ~/.ssh/codeup_user_2_ed25519 密鑰
git clone git@codeup-user-2:example/repo.com
# 訪問 GitLab 平臺,將使用 ~/.ssh/gitlab_ed25519 密鑰
git clone git@gitlab.com:example/repo.com
解決方案二:
- 修改啟動項(xiàng) zshrc 文件
alias gcl='git clone --recurse-submodules' # 配置簡化命令
alias gcln='git clone --recurse-submodules --config core.sshCommand="ssh -i ~/.ssh/id_rsa_new"' # 配置簡化命令 私鑰鏈接id_rsa_new
本地項(xiàng)目推送到github
在 GitHub 上創(chuàng)建倉庫
在本地初始化 Git 倉庫
- 如果您尚未在本地項(xiàng)目目錄中初始化 Git 倉庫,請使用以下命令:
cd path/to/your/project
git init # 添加文件到 Git 倉庫
將您的項(xiàng)目文件添加到 Git 倉庫中:
git add .
提交更改
- 提交您添加到倉庫中的更改:
git commit -m “Initial commit” #將 “Initial commit” 替換為描述您所做更改的適當(dāng)消息。
在 GitHub 上添加 SSH 密鑰(如果尚未添加 請看上面的文檔)
使用 git remote 命令將您的本地倉庫與 GitHub 上的遠(yuǎn)程倉庫關(guān)聯(lián)起來。
- 如果您使用 SSH:
git remote add origin git@github.com:your-username/your-repository.git
#將 your-username 和 your-repository 替換為您的 GitHub 用戶名和倉庫名。
推送項(xiàng)目到 GitHub
- 使用 git push 命令將您的本地倉庫推送到遠(yuǎn)程倉庫:
git push -u origin master
-u 或 --set-upstream 選項(xiàng)會設(shè)置本地分支 master 跟蹤遠(yuǎn)程分支 master。這樣,您在未來的推送操作中只需運(yùn)行 git push 即可。
如果您的本地主分支不是 master 而是 main(這是 GitHub 新建倉庫時的默認(rèn)分支名),則您應(yīng)該運(yùn)行:文章來源:http://www.zghlxwxcb.cn/news/detail-856658.html
推送完成后,您可以訪問您的 GitHub 倉庫頁面,確認(rèn)代碼已經(jīng)成功上傳。
SHH配置文檔參考地址文章來源地址http://www.zghlxwxcb.cn/news/detail-856658.html
到了這里,關(guān)于git ssh密鑰配置 & 本地項(xiàng)目推送到github的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!