简体   繁体   English

git rebase(如果没有冲突),否则git merge

[英]git rebase (if no conflicts) otherwise git merge

When my branch has no conflicts with master, I prefer to do a git rebase , because it keeps a plain history. 当我的分支与master没有冲突时,我更喜欢做一个git rebase ,因为它保留了一个简单的历史记录。

But when there are conflicts, sometimes rebase makes me solve the conflicts for each remote commit, which is more work. 但是当存在冲突时,有时rebase会让我解决每个远程提交的冲突,这是更多的工作。 Also, I consider that if there are conflicts then the probability that the conflict resolution introduces a new bug is higher, so in that case I prefer to do a merge instead, that keeps clear record of where I started coding, what I did, and how I resolved the conflict. 此外,我认为如果存在冲突,则冲突解决引入新错误的概率更高,因此在这种情况下我更愿意进行merge ,这清楚地记录了我开始编码的位置,我做了什么,以及我是如何解决冲突的。

So I keep doing: 所以我一直这样做:

git fetch -> git rebase 

And, if there is any conflict: 而且,如果有任何冲突:

git rebase --abort
git merge

I find myself doing this several times per day. 我发现自己每天都会这样做几次。 Is there any built-in way to achieve this? 有没有内置的方法来实现这一目标? Or should I create a git alias? 或者我应该创建一个git别名?

I prefer to inspect the incoming commits manually, then decide on rebase-vs-merge, but you can in fact automate this, using the fact that git rebase will exit with a zero (success) status if it completes the rebase, and a nonzero (failure) status if it does not. 我更喜欢手动检查传入的提交, 然后决定rebase-vs-merge,但实际上你可以使用git rebase在完成rebase时以零(成功)状态退出的事实自动执行此操作,并且非零(失败)状态,如果没有。 You can therefore write a tiny shell script, or one-line command suitable for use as a shell alias: 因此,您可以编写一个小的shell脚本或适合用作shell别名的单行命令:

git fetch && { git rebase || { git rebase --abort && git merge; } }

which says to run git fetch , and if it succeeds, run the braced series of commands: 它表示运行git fetch ,如果成功,运行一系列支持的命令:

  • git rebase , and if that succeeds, nothing else; git rebase ,如果成功,别的什么都没有; or 要么

    • git rebase --abort , and if that succeeds, git merge git rebase --abort ,如果成功, git merge

(The inner braces are not actually required since && binds more tightly than || , at least in sh and bash. They are here more for clarity of intent.) (实际上并不需要内括号,因为&&||更紧密地绑定,至少在sh和bash中。它们更多是为了清晰意图。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM