There are generally two ways to integrate branches in git - git rebase and git merge. But in your actual team development, which method is used the most, or which one do you think is better? ?
天蓬老师2017-05-02 09:46:25
I guess the reality is like this:
At least half of the people who use git do not know how to use rebase (even for basic functions) (this estimate is probably very conservative)
I’m afraid only half of the remaining half really understand when to rebase correctly...
Merge or rebase is not a question of choosing one or the other. It should be chosen according to the specific situation. In reality, this "specific situation" is really difficult to list one by one. For most developers, It is said that the functions in the team are relatively single, and it is not often encountered, so here I will only summarize two principles that everyone should understand and abide by:
When you go pull
from remote, always use rebase (with one exception, discussed later)
When you complete a feature (assuming you opened a local branch separately) and plan to merge it into the trunk branch, always use merge
Developers should understand that different merge strategies may not affect the final code result, but they will affect how git records the history of submissions (although many times this understanding only benefits individuals and has little impact on themselves). When other people use these histories (such as during code review, or during bisect, etc.), they will feel extremely happy or miserable because of what you have done in the past.
The second point is easier to understand. Hide the specific details of developing a function in the local branch, and merge the final result into the trunk branch as a complete record.
The key is the first point. In most cases, everyone will choose the default pull
,也就是 fetch + merge
, which is fetch + merge
, so we will see interference messages such as Merge commit xxx into xxx appearing constantly on the trunk. If everyone does this for convenience, then we will never have a clean and beautiful history tree. Instead, we will have something like this:
If you clone a project that uses git correctly, you will never see a messy master like the picture above. why? The answer is: git pull --rebase
. This command will use rebase
instead of the default git pull --rebase
,这条命令会使用 rebase
来代替默认的 pull
to rebase the merged history tree, which can avoid the inability to quickly Additional merge records generated by fast forward.
Now most git clients allow setting git pull
的默认行为是 --rebase
, so mastering this trick can produce clean and beautiful history records for most daily pull operations, but this is not a perfect ending. There is an exception like this:
When you use git merge
完成了一个功能分支向主干分支的合并之后(当然是 --no-ff
)
When you run git fetch
, you find that the remote trunk is several commits ahead of you
If you git pull --rebase
at this time, you will find that the record of the feature branch you just merged is gone...
This is because rebase will ignore merged records, so the rebase command has a special parameter called: --preserve-merges
which is used to reconstruct ignored merged records. So a better solution than --preserve-merges
用于重建被忽略的合并记录。所以比 git pull --rebase
更好的方案就是用 git fetch
+ git rebase -p
is to use
instead.
git pull --rebase
git fetch
git rebase -p
I can avoid the accident mentioned above by doing this. It seems a bit complicated, but in fact it is not difficult once you are familiar with it. The more important reason is that
git pull
cannot be used together with
git pull
配合使用,所以你必须指明目标分支给 rebase,这一点比较烦人,特别是很多 GUI 客户端压根不支持 git rebase -p
Since it cannot be used with
ORIG_HEAD
ORIG_HEAD
在很多情形下非常有用,比如说你可以用 git log -p -reverse ORIG_HEAD
来回看最近一次合并所产生的所有变化等等。--preserve-merges
Very useful in many situations. For example, you can use git log -p -reverse ORIG_HEAD
to review all the changes caused by the latest merge, etc. --preserve-merges
will cause it to lose the location it is supposed to point to, and you will have to find the correct hash to replace it first, which can be a bit annoying.
Additional point: If your team doesn't care about a lot of unimportant interfering commit records on the trunk branch, then you can always use rebase, so that at least there won't be many forks, and if you handle it properly, you can get a clean (but verbose) trunk. Branch history.
淡淡烟草味2017-05-02 09:46:25
Not mandatory, but all tests are required to be green after push
(We don’t have particularly long branches, most are merged within 2-3 days, rarely more than a week)
Usually everyone chooses according to the merge difficulty. If there is no difference, rebase is given priority... because it looks better