git的操作
1、下载git并配置⽤户
git config --global user.name "你的⽤户名"
git config --global user.email "你的邮箱"
2、上传项目到github/gitee
- 2.1 在github/gitee上创建git仓库
- 2.2 在项目中创建git仓库
git init
- 2.3 关联本地项目到远程仓库
git remote add 连接仓库名称 仓库地址
- 2.4 缓存到暂存区
git add .
- 2.5 提交内容
git commit -m "提交说明"
- 2.6 提交到远程仓库
git push origin 分⽀名
若是有时候提交不上去需要强制提交
git push -u origin master -f
3、协同合作
- 3.1 创建并切换分⽀
git checkout -b 分⽀名
- 3.2 切换分⽀
git checkout 分⽀名
- 3.3 放弃本地代码当前分⽀下的修改
git checkout .
- 3.4 提交代码
- 3.5 切换到master分支合并代码
git checkout master
git merge 你创建的分⽀名
git push origin master
4、拉取远程代码
git pull
5、拉取某个分支最新代码
git pull 分⽀名
6、查看当前分支状态
git status
7、查看提交历史
git log
8、查看git所有分支
git branch -a
9、回滚代码
- 9.1 查看回滚的commitID
git log
- 9.2 reset
git reset –hard 要回滚的commitID / git reset --hard HEAD^
- 9.2 revert
git revert 要回滚的commitID
两者的区别在于:
1. revert:是放弃指定提交的修改,但是会生成一次新的记录,需要填写提交注释并且以前的提交记录会存在
2. reset:是指姜HEAD指针指到指定提交, 历史记录中不会存在放弃提交的记录。
10、迁移老代码到新代码仓库
11、如何使用ssh管理多个账户
11.1 清空默认账户以及邮箱
git config --global --unset user.name
git config --global --unset user.email
- 11.1 ⽣成多个ssh key(这⾥就不讲解了,⽹上有很多教程 )
- 11.2 在github以及码云上绑定对应的ssh key
- 11.3 给他们分别添加到ssh-agent信任列表
# id_rsa_github_public 换成自己创建的ssh名字
ssh-add ~/.ssh/id_rsa_github_public
ssh-add ~/.ssh/id_rsa_github_private
ssh-add ~/.ssh/id_rsa_gao
ssh-add ~/.ssh/id_rsa_gitlab
- 11.4 在 ~/.ssh/config⽂件夹中进⾏配置,若没有就创建⼀个config
touch config
# Host 链接账户名(一会儿测试是否成功用)
# Hostname 链接地址
# IdentityFile ssh地址
# User 仓库用户名
# 公司的github账号
Host gongyibai.github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa_github_public
User gongyibai
# 自己的github账号
Host gcindy.github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa_github_private
User gcindy
# 自己的gitee账号
Host gongyibai.gitee.com
Hostname gitee.com
IdentityFile ~/.ssh/id_rsa_github_private
User gongyibai
# 高明伟的github账号
Host gaowei1012.github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa_gao
User gaowei1012
# 自建的gitlab账号
Host suxiaoyan.gitlab.com
# Hostname https://dgdream.online
Hostname 42.192.149.196
IdentityFile ~/.ssh/id_rsa_gitlab
User suxiaoyan
11.2 测试链接是否成功
# 测试 GitHub
ssh -T git@gcindy.github.com
# 测试 Gitee
ssh -T git@gongyibai.gitee.com
这样就是配置成功了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8SipAxUt-1676599621007)(media/16765358251969/16765992572573.jpg)]
12、macos ssh配置重启就失效
- 在zshrc中配置
vim ~/.zshrc
ssh-add -K ~/.ssh/id_rsa_github_public