FE Workflow
standard-commit>git

账户配置

配置全局账户

git config --global user.name "xxx"
git config --global user.email ""

配置本地账户

git config user.name "xxx"
git config user.email ""

SSH 本地配置多个Git账户

# 步骤一:为每个Git账号生成一个SSH密钥对
ssh-keygen -t rsa -C "youremail_github@example.com" -f ~/.ssh/id_rsa_github_user1
ssh-keygen -t rsa -C "youremail_github@example.com" -f ~/.ssh/id_rsa_github_user2

# 步骤二:配置SSH客户端
# 编辑~/.ssh/config文件:
touch ~/.ssh/config
open ~/.ssh/config

# 在~/.ssh/config文件中,为每个Git服务添加配置:
# GitHub account
Host github_user1
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github_user1
Host github_user2
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github_user2

# 步骤三:将公钥添加到对应的Git账号中
# 复制到对应 GitHub Gitee GitLab 的SSH key设置页面中
cat ~/.ssh/id_rsa_github_user1.pub
cat ~/.ssh/id_rsa_github_user2.pub

# 测试SSH连接: ssh -T [config文件配置的User]@[config文件配置的Host]
ssh -T git@github_user1
ssh -T git@github_user2

# 多账号使用问题:指向SSH仓库地址
git remote set-url origin git@gitee.com:[用户名]/[仓库地址].git

命名&操作

分支命名

feature/xxx # 功能分支
bugfix/xxx # bug修复分支
hotfix/xxx # 紧急修复分支
release/xxx # 发布分支

查看日志

git log # 查看提交历史,包括所有分支和标签的提交记录
git log --oneline # 查看简约版历史记录
git log --graph # 查看分支合并图
git reflog # 查看 git 引用日志,查看 HEAD 移动和操作

vim 编辑器

# 按 i 进入编辑模式,按 ESC 退出编辑模式
# :wq 保存并退出
# :q! 不保存退出
# 修改commit的记录

commit

暂存区存储、恢复

git stash # 把当前工作现场“储藏”起来
git stash pop # 恢复储藏内容
git stash list # 查看储存列表
git stash apply stash@{0} # 多次stash后,恢复指定的stash

合并 commit

# 1、使用命令打开文本编辑器,会出现最近的commit历史(顶部为旧记录)
git rebase -i HEAD~2 # 合并最近两次commit
# 2、将需要合并的commit前面的pick改为s
# 3、保存退出 :wq
# 4、会出现一个新的文本编辑器,将需要合并的commit信息合并
# 5、保存退出 :wq
# 6、会出现冲突,可以强制push
$ git push -f

分支

分支操作

git branch <branch_name> # 创建分支
git checkout <branch_name>  ||  git switch <branch_name> # 切换分支
git checkout -b <branch_name>  ||  git switch -c <branch_name> # 创建并切换分支
git branch -d <branch_name> # 删除分支
git branch -D <branch_name> # 强行删除分支
git push origin --delete <branch_name> # 删除远程分支

合并分支

git cherry-pick <commit_id> # 复制一个特定的提交到当前分支

# 合并方式一:merge
# merge 会把合并前的分支的提交历史原封不动的拷贝过来
git merge <branch_name> # 合并分支
git merge --no-ff -m <message> <branch_name> # 在合并分支时添加合并描述

# 合并方式二:rebase merge
# 保留提交的作者信息,同时可以合并commit历史
# 完美的解决了 squash merge 定位不到原开发者的问题
git checkout dev
git rebase -i master # 通过 vi 手动调整commit历史
git checkout master
git merge dev

撤销最近的一次合并

git reflog # 查看 Git 操作历史
git reset --hard HEAD~1 # 方式一:完全丢弃你的更改
git reset --soft HEAD~1 # 方式二:保留你的更改,并将它们移回工作区
git push -f origin <your-branch-name> # 强制推送你的更改到远程仓库

发布

打 tag 标签

git tag # 列出所有 tag
git tag -a stable-v1.0.0 -m 'xx功能需求稳定版本1.0.0' # 添加 tag
git tag -d <tag_name> # 删除某个标签
git show <tag_name> # 显示标签对应提交记录的具体信息
git push <remote> <tag_name> # 推送某个标签到远程仓库
git push <remote> --delete <tag_name> # 删除远程仓库中的某个标签
账户配置
配置全局账户
配置本地账户
SSH 本地配置多个Git账户
命名&操作
分支命名
查看日志
vim 编辑器
commit
暂存区存储、恢复
合并 commit
分支
分支操作
合并分支
撤销最近的一次合并
发布
打 tag 标签