- UID
- 667348
- 积分
- 0
- 金币
- 0
- 精华
- 0
- 威望
- 0
- 贡献
- 0
- 阅读权限
- 220
- 注册时间
- 2009-5-29
- 最后登录
- 2026-5-6
- 在线时间
- 0 小时
热心网友
- 金币
- 0
- 阅读权限
- 220
- 精华
- 0
- 威望
- 0
- 贡献
- 0
- 在线时间
- 0 小时
- 注册时间
- 2009-5-29
|
配置 GitHub 和 Gitee 共存环境
前言
准备
-
git工具: https://git-scm.com/downloads
-
GitHub账号: https://github.com
-
Gitee账号: https://gitee.com
配置
-
清除 git 的全局设置 (没配置全局则跳过)
-
生成并添加 SSH Keys
-
多环境配置config文件
-
在~/.ssh/目录下创建config文件
touch ~/.ssh/config
-
配置config文件内容
-
最简配置
# GitHub
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_ed25519
-
完整配置
# Default gitHub user Self
Host github.com
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
# Add gitee user
Host gitee.com
HostName gitee.com
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519
AddKeysToAgent yes
-
参数解释
-
Host
它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。
这里可以使用任意字段或通配符。
当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。
-
Port
自定义的端口。默认为22,可不配置
-
User
自定义的用户名,默认为git,也可不配置
-
HostName
真正连接的服务器地址
-
PreferredAuthentications
指定优先使用哪种方式验证,支持密码和秘钥验证方式
-
IdentityFile
指定本次连接使用的密钥文件
-
AddKeysToAgent yes
将私钥加载到 ssh-agent,
等同于 ssh-add ~/.ssh/id_ed25519
检验
-
clone 测试
-
GitHub 项目
$ git clone git@github.com:librarookie/spring-boot.git
Cloning into 'spring-boot'...
remote: Enumerating objects: 15, done.
remote: Total 15 (delta 0), reused 0 (delta 0), pack-reused 15
Receiving objects: 100% (15/15), done.
-
Gitee 项目
$ git clone git@gitee.com:librarookie/test.git
Cloning into 'test'...
remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (19/19), done.
-
push 测试
-
commit
$ git commit -am "test"
Author identity unknown
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'noname@G3.(none)')
-
原因是没有配置 user.name和 user.email
- 方案一: 设置全局变量的
user.name和 user.email
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
note: 此方案适合只使用GitHub 或Gitee(可以试试GitHub和Gitee使用同一个账号)
- 方案二: 设置项目库局部
user.name和 user.email
- 进入项目本地仓库
- 设置
user.name和 user.email
git config --local user.email "you@example.com"
git config --local user.name "Your Name"
-
commit 2
$ git commit -am "5555"
[test 52bcd83] 5555
1 file changed, 1 insertion(+)
-
push
$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 242 bytes | 242.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.1]
To gitee.com:librarookie/test.git
a14d3de..52bcd83 test -> test
拓展
- 如何将 GitHub 项目导入码云?一步搞定!
- git-config 配置多用户环境以及 includeIf用法
Ref
- https://duter2016.github.io/2021/01/22/Git同时使用Gitee和Github并设置代理/
- https://www.jianshu.com/p/68578d52470c
来源:https://www.cnblogs.com/librarookie/p/15390709.html |
|