https://learngitbranching.js.org/?locale=zh_CN
Basics
1. commit
git commit -m "feat: xxx."
- Normal flow
git add . git commit -m "feat: local recording" git push
- Others
git commit
加上-m
参数是为了直接提供提交信息,而不需要打开文本编辑器。
情况 | 示例 | 是否推荐 |
不加引号 | git commit -m Fix | 不推荐 |
加双引号 | git commit -m "Fix login button styling" | 推荐 |
加单引号 | git commit -m 'Fix login button styling' | 可选 |
多行提交信息 | git commit -m "Title" -m "Description" | 推荐 |
2. checkout
git checkout
git checkout -b xxx
3. merge and rebase
git merge
git rebase
- 在
bugFix
分支执行git rebase main
,会把 main 分支作为基础。
- 在
test
分支执行git rebase
bugFix
main
,会把 bugFix 变成 main 的基础(也就是先 bugFix 然后 main)
4. 分离 head (rampup)
绝对:
git checkout xxx
相对:
^
- 切换到 main 的父节点(相当于让 head 指向 main 的父节点):
git checkout main^
- 切换到 head 的父节点:
git checkout HEAD^
~
- 后退 4 步:
git checkout HEAD~4
- 将分支强制移动到某位置:
git branch -f main HEAD~3
5. reset
git reset xxx
- 只对本地分支有效
- 可以对 HEAD 或者分支名操作,可以加入绝对引用和相对引用(注意!在 A 分支进行 git reset B, 会导致 A 重置到 B 的状态)
git reset bugFix^
git reset HEAD~3
git revert
git revert HEAD
: 提交记录后面会多出一个 commit
6. cherrypick
需要 hash 值:
git cherry-pick <提交号>...
git cherry-pick c3 c6 c7
7. 交互式 rebase
- 交互式 rebase 指的是使用带参数
-interactive
的 rebase 命令, 简写为i
- 会产生一个副本,然后把 HEAD 指过去
git rebase -i c1
: 能够重新整理 c1一直到 HEAD 的所有提交记录
Others
1. 本地栈式提交(只取一个提交记录)
结合以下两个即可
git rebase -i
git cherry-pick
2. 技巧1
情景:之前在
newImage
分支上进行了一次提交,然后又基于它创建了 caption
分支,然后又提交了一次。此时想对某个以前的提交记录进行一些小小的调整。比如设计师想修改一下 newImage
中图片的分辨率,尽管那个提交记录并不是最新的了。- 先用
git rebase -i
将提交重新排序,然后把我们想要修改的提交记录挪到最近
- 然后用
git commit --amend
来进行一些小修改
- 接着再用
git rebase -i
来将他们调回原来的顺序
- 最后把 main 移到修改的最前端(比如使用
git rebase caption main
)
查看上游分支
git branch -vv
更新上游分支
git branch --set-upstream-to=origin/newupstreambranch mylocalbranch
更新本地的远程分支列表
git fetch --prune
Comments