SSH 与 git 的常用命令

1、生成 key

ssh-keygen -t rsa -C "邮箱" -f ~/.ssh/文件名
或者(默认路径)
ssh-keygen -t rsa -C "邮箱"

2、同机多个github账号 的 sshkey 配置

在 .ssh 目录中 创建 config 文件

# 进入.ssh 文件
cd ~/.ssh
# 创建 config 文件
touch config
# 编辑
vim config

将不同的 ssh key 配置上

#github-account1
Host account1.github.com
HostName github.com
User <gihub的用户名1>
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_account1

#github-account2
Host account1.github.com
HostName github.com
User <gihub的用户名2>
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_account2

#gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab

以上配置完成后,将 ssh 的对应公钥放到对应的 github 账号上

然后测试是否成功

ssh -T account1.github.com
ssh -T account2.github.com
# 输出成功如下
Hi account1! You've successfully authenticated, but GitHub does not provide shell access.

测试成功后,以后本机对于github 账号1 就使用 account1.github.com 这个域名来进行 pull 和 push;对于gihub 账号2 就使用 account2.github.com 这个域名进行 pull、push 等操作

git remote add origin git@<本地电脑中对 ssh 的配置 config 中的定的域名前缀,如前面的:account1>.github.com:<github上的仓库>.git
同机不同 git 服务账号的配置:如前面所示,一样在.ssh目录中的 config 中添加对应的配置即可

3、设置 git 的全局名称

# 设置全局用户名
git config --global user.name "用户名"
# 设置全局邮箱
git config --global user.email "邮箱"
# 查看
git config --list

4、针对不同项目设置名称和邮箱

进入项目根目录

# 设置当前目录的用户名
git config user.name "用户名"
# 设置当前目录的邮箱
git config user.emial "邮箱"
# 查看
git config --list

5、删除项目根目录中的.git 目录(隐藏目录)

假如现在已经在项目根目录

# 先清空目录
cd .git
# 清空目录中所有文件
rm -rf *
cd ../
rmdir .git

6、 git配置一个远程仓库到本地仓库

# 进入本地项目目录
cd <project dir>
# 初始 git
git init
# 添加远程仓库的 ssh 地址
git remote add origin <远程仓库的 ssh 地址>
# 拉取远程仓库
git pull
# 参考远程分支
git branch -a
# 检出master分支(或其他分支)
git checkout -b master remotes/origin/master
# 添加本地修改文件
git add .
# 提交修改
git commit
# 推送到远程仓库
git push origin master:master

7、更换远程仓库 url

# 进入项目根目录
cd <project dir>
# 删除远远程仓库url
git remote rm origin
# 重新添加远程仓库 url
git remote add origin <新远程仓库 url>

8、用命令行将 ssh 公钥关联远程 git 服务

ssh-copy-id -i ~/.ssh/id_rsa.pub git@<你服务器公网ip>


原文始发于微信公众号(三万之一):SSH 与 git 的常用命令

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/231243.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!