https://learngitbranching.js.org/?locale=zh_CN
Basics
1. commit
git commit2. checkout
git checkoutgit checkout -b xxx3. merge and rebase
git mergegit rebase- 在
bugFix分支执行git rebase main,会把 main 分支作为基础。
- 在
test分支执行git rebasebugFixmain,会把 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 revertgit 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