之前其实很少用到git merge
and git rebase
,一般都是通过Github UI Pull Request merges feature branch’s updates into master.
也可在CLI 中merge feature branch into master,但这样master中的git history不是线性的,并且会制造一个merge commit:
1 | # merge feature to master, first go to master |
Highlight:
- fast-forward merge: less commits, no merge commit will be generated.
- recusive merge: has merge commit, can do revert, clear what was done on a branch.
关于rebase的使用,可以参考这篇文章,它构造了一个线性的git history,方便以后查看: https://www.jianshu.com/p/6960811ac89c 注意,这篇文章的例子是将dev branch 本地合并到master 再提交,并不是merge request的方式。
- 先将本地master 更新, git pull origin master
- 进入需要rebase的分支, git checkout dev
- 执行rebase, git rebase master. 这样就把master的commits 线性的合并到dev 分支了
- rebase可能遇到conflicts, 参考这里去修复Resolving merge conflicts after a Git rebase
- dev 中rebase 完成后,切换到master执行merge, git checkout master, git merge dev (github/gitlab UI就是做了这一步)
这样就把dev合并到master中了,实际上还是先rebase 再 merge.
这个文章讲了更多的rebase 特性: https://baijiahao.baidu.com/s?id=1633418495146592435
- git pull --rebase (will not use merge)
- 修改commit 历史
- 合并commit
- 分解commit
- 重新排序commit
Git Interactive Rebase, Squash, Amend and Other Ways of Rewriting History
实际遇到一个要求,存在多次的commits在dev branch中,需要squash multiple commits into one, 解决办法:
在本地rebase 然后再提交, for example I use dev
branch:
1 | # 如果dev branch不干净 |
Other GitHub tutorials:
GitHub.com Help Documentation: https://docs.github.com/en/github