一、git配置
1、全局用户名、邮箱
git 看用户名以及邮箱
git config user.name
git config user.email
git 设置用户名和邮箱
git config --global user.name zhanglei
git config --global user.email zhanglei@cngongbao.com
2、生成ssh key
生成ssh秘钥, 更详细参考这里
ssh-keygen -t rsa -C zhanglei@cngongbao.com
如果设置了密码添加ssh密码
ssh-add ~/.ssh/id_ras
3、git设置远程仓库地址
方法有三种:
- 修改命令 (如果没有远程地址,就不能用这条命令)
git remote set-url origin [url]
-
先删除后添加:
git remote rm origin git remote add origin [url]
-
修改.git/config文件
二、git使用
查看远程仓库:git remote -v
添加远程仓库:git remote add origin [url]
删除远程仓库:git remote rm origin
修改远程仓库:git remote origin set-url [url]
拉取远程仓库:git pull [remoteName] [localBranchName]
推送远程仓库:git push [remoteName] [localBranchName]
1、sourceTree远程分支更新
清除远程已经不存在的分支
git remote prune origin
更新远程分支
git remote update origin --prune
2、忽略已提交文件
有时候创建项目并push之后才发现不小心把应该要忽略的文件或目录给push上去了,如.idea/目录。
这时候再去编辑.gitignore文件并没有作用。
正确做法为:
git rm --cached -r .idea/
然后编辑.gitignore文件:
./idea
3、更新子模块
git submodule update --init
4、携带commit抽离git子模块
5、git 命令提交
持续话集成(jenkins、CI)场景就必须使用这个了
# 克隆远程仓库到本地
git clone <repository_url>
# 创建一个新分支
git branch <branch_name>
# 切换到一个分支
git checkout <branch_name>
# 提交更改
git add .
git commit -m "Commit message"
git push origin <branch_name>
# 合并分支
git checkout <target_branch>
git merge <source_branch>
git push origin <target_branch>
三、git规范
1、commit提交规范
feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style: 格式(不影响代码运行的变动)
refactor:重构(即不是新增功能,也不是修改bug的代码变动)
perf : 代码优化(提升代码性能)
test:增加测试
chore:构建过程或辅助工具的变动
2、分支规范
dev/develop:开发分支
test:测试分支
pre/preproduction:预发分支
master:生产分支
feature-:特性分支
bug-:bug修复分支
四、github配置ssh
github自August 13, 2021就不能使用http方式推送代码了,会报错如下
Pushing to https://github.com/liuxingyuzaixian/react-native-scrollable-view-pager.git
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
因此只能添加ssh
1、生成ssh公钥
cd ~/.ssh
这个只能用你的github邮箱,不带引号
ssh-keygen -t rsa -C "xxx@xxx.com"
查看公钥
cat id_rsa.pub
2、配置到github官网上
将上述公钥填到github官网,title随便填,点击新增即可
![image-20230518181509168](https://i-blog.csdnimg.cn/blog_migrate/26f9e4bfe869a57faa816cb32527cbca.png)
3、验证与github是否连接成功。
ssh -T git@github.com
4、修改端口号
github默认端口号是22,如果有开VPN等工具会占据该端口号,修改端口号如下:
新增文件~/.ssh/config,内容如下
Host github.com
Hostname ssh.github.com
Port 443
IdentityFile ~/.ssh/id_rsa