简体   繁体   中英

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.

But when there are conflicts, sometimes rebase makes me solve the conflicts for each remote commit, which is more work. 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.

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?

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. You can therefore write a tiny shell script, or one-line command suitable for use as a shell alias:

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 rebase , and if that succeeds, nothing else; or

    • git rebase --abort , and if that succeeds, 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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