GitGitGit使用

Git 分支与整合策略

2016-08-01  本文已影响236人  michael_jia

持续部署的前提是模块化设计、自动化测试和持续集成。使用好 Git 的分支(branch)与整合(integrate)功能,有利于高效率的持续集成。Pro Git 关于 分支 的建议是不错的,值得采用。

整合

Rebasing(变基) 是一个很不错的功能,和 Merging(合并)一对兄弟,是用来整合来自不同分支的修改的两种方法。

Rebasing

对于多人协同开发来说,本地修改 commit(提交) 后,在 push(推送) 代码前,Rebasing 一下是一个很好的做法,log 看起来比较整齐,有助于阅读和理解(由于 merge 带来了蜘蛛网般的 log,想看清代码走向非常费劲;log 中常见的 "Merge branch 'v0.7d-rebuild' of http://git.example.com/wbs.wph into v0.7d-rebuild" ,本是不必要的 merge,也自然消失了)。
请不要对已经 push 出去的东西 再次 rebase 了。

怎么理解 Rebase 呢?
假定把当前分支作为新特性分支,在开发过程中,主分支(Rebase 分支)又发生变化了,我们就要基于 Rebase 分支最新末端重新应用一下当前分支的每个 commit。
同理,rebase 概念适用于有跟踪关系的远程和本地分支的整合。

在 config 中设置 rebase 选项,使得 git pull 使用 rebase 整合所做修改
git config --global pull.rebase true
git config --global branch.autoSetupRebase always
采用 merge 示意图

git pull 命令采用 merge 策略,即:pull(拉取)= fetch(抓取)+ merge;

采用 rebase 示意图

git pull 命令采用 rebase 策略,即:pull(拉取)= fetch(抓取)+ rebase;

由基于 C2,变为基于 C4;变基后,C3 和 C5 变为 C3' 和 C5'
变基时出现冲突,怎么办?
冲突产生的原因
冲突的解决之道
删除分支的理由和时机
了解 remote 的分支情况
A commit and its tree
选自 Branches in a Nutshell,理解 git 存储机制
分支管理策略(Branching Workflows
A “silo” view of progressive-stability branching
merge 示意图

Git 会使用两个分支的末端所指的快照(C4 和 C5)以及这两个分支的共同祖先 Common Ancestor(C2),做一个简单的三方合并。


一次典型合并中所用到的三个快照 A merge commit(C6), it has more than one parent.
高级主题
$ git push
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 257 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To http://git.example.com/wbs.wph.git
   0187b7f..9c68fb9  v0.8 -> v0.8
 ! [rejected]        dev -> dev (non-fast-forward)
error: failed to push some refs to 'http://git.xxtao.com/wbs.wph.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
- 请每次都要仔细查看返回的信息,git 做的非常好的一点就是会认认真真地告诉你下一步应该怎么做,你照着做就好了。
- 照着做尽管并不是 100% 都成功,无论如何值得认真尝试;
上一篇 下一篇

猜你喜欢

热点阅读