Git开发代码合并

1.创建新的本地临时分支:
[user@localhost olt_pm]$ git branch BMCA
[user@localhost olt_pm]$ git branch
BMCA
* git29
[user@localhost olt_pm]$ git checkout BMCA
M script/cli/PM_PON_OAM_RETURNCODE.xml
M tdm/rp/cli/source/tdm_sa.c
Switched to branch 'BMCA'
[user@localhost olt_pm]$ git branch
* BMCA
git29
2.在新分支内commit修改:
[user@localhost olt_pm]$ git add script/cli/PM_PON_OAM_RETURNCODE.xml tdm/rp/cli/source/tdm_sa.c
[user@localhost olt_pm]$ git commit

3.切换回原分支、更新后再merge本地临时分支改动:
[user@localhost olt_pm]$ git checkout git29
Switched to branch 'git29'
Your branch is ahead of 'gerrit/git29' by 6 commits.
[user@localhost olt_pm]$ git branch
BMCA
* git29
[user@localhost olt_pm]$ git pull
......
[user@localhost olt_pm]$ git merge BMCA
Updating 10bc10b..0ca175d
Fast-forward
script/cli/PM_PON_OAM_RETURNCODE.xml | 1 +
tdm/rp/cli/source/tdm_sa.c | 4 ++--
2 files changed, 3 insertions(+), 2 deletions(-)
[user@localhost olt_pm]$
此时git log会发现修改已经合入原分支。本地临时分支可以在提交成功后再删除。
[user@localhost olt_pm]$ git branch -d BMCA
Deleted branch BMCA (was 0ca175d).
[user@localhost olt_pm]$ git branch
* git29
4.cherry-pick代替merge:
merge会把一批修改合并,如果只想合并某次修改,(典型情况:将库里的主干分支本人的修改合入库里新分支),就得用cherry-pick了:
git checkout到源branch、然后git log --committer=zhou.qimin列出自己的入库记录并复制保存,
找出需要合入的多次提交的hash值,然后git branch切换到新分支、从旧到新依次在新分支下执行git cherry-pick即可:
git cherry-pick xxx
git cherry-pick yyy
git cherry-pick zzz
此过程中如果有冲突会有类似提示:
......
git commit -c 72693be3811bb1197499ed8e7b725a51b90fb694
......
那么手动解决冲突后再执行这行即可。解决冲突方法:
git st ./
......
# both modified: olt_common/sec/time/include/time_moudle_id.h
......
直接编辑提示“both modified”的文件,找到所有类似如下的地方:
<<<<<<< HEAD
b789
=======
b45678910
>>>>>>> 6853e5ff961e684d3a6c
修改并干掉注释后,再git add olt_common/sec/time/include/time_moudle_id.h、然后git commit -c 72693be3811bb1197499ed8e7b725a51b90fb694,冲突解决,继续后续 git cherry-pick。全部完成后,新分支下git log会发现多次修改合入了,但仍然是多次commit的形式,此时合并多次commit为1次即可,有两种方法可选:

(1). git rebase:

git rebase -i mmm /* 注:这里mmm是指新分支里新合入之前最后的那次commit的hash值 */
此时会出现vim模式界面:
pick oooo .....
pick pppp .....
pick qqqq .....
其中:
pick 的意思是执行这个 commit
squash 的意思是这个 commit 会被合并到前一个commit
所以只需保留第一行pick,后面多行都将pick改为squash:
pick oooo .....
squash pppp .....
squash qqqq .....
然后:wq以保存并退出,然后就来到熟悉的commit message 的编辑界面,修改后:wq保存退出。
此时git log会发现多次未提交的commit已经合并为一个,之后只要提交即可。提交时会提示类似如下错误:
remote: ERROR: [9c577b7] missing Change-Id in commit message footer
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 10109621@gerrit.zte.com.cn:hooks/commit-msg ${gitdir}/hooks/
remote: And then amend the commit:
remote: git commit --amend
git log 查看会发现确实新加的合并多次操作产生的提交没有Change-Id,按照提示运行两个命令并保存,再次提交即可。

[网上参考:https://www.jianshu.com/p/964de879904a ]

(2). git reset :
git reset mmm /* 注:这里mmm是指新分支里新合入之前最后的那次commit的hash值 */
这种方法需要需要把所有文件修改全部再git add一遍、然后git commit,需要git reset前清楚本地有哪些额外修改不入库。

(最后,cherry-pick也可以用公司gerrit系统进行cherry-pick操作,但可能无法将多次commit合并为1次提交)

5.拉取库中新建立的分支:
库根目录的git配置文件复制原有分支配置修改为新库配置即可:
[zhouqimin@localhost olt_pub]$vi .git/config
......
[branch "git29"]
remote = gerrit
merge = refs/heads/git29
[branch "C6XXV1.1.1_release20190218"]
remote = gerrit
merge = refs/heads/C6XXV1.1.1_release20190218
然后直接git checkout C6XXV1.1.1_release20190218切换新库了。

其它合并修改的方法:
(a). git stash方法保存本地修改。
网上参考:http://www.cppblog.com/deercoder/archive/2011/11/13/160007.html
这种方法对付简单的情况、少数的修改还是可以的。但是对于复杂的情况会更麻烦。
(b). git format-patch、git am即产生补丁文件、应用补丁文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值