Mac中Git的简单实用(6) --- 分支管理策略

今天我来介绍下Git,Git是一款免费、开源的分布式版本控制系统。
我们在上一个学习了Git分支冲突管理。
这一章,我们要学习Git的Fast-forward模式、保存恢复现场  、测试的feature分支。

Mac中Git的简单实用(1) — Git基本命令(1)
Mac中Git的简单实用(2) — Git基本命令(2)
Mac中Git的简单实用(3) — Github远程仓库
Mac中Git的简单实用(4) — 分支branch管理
Mac中Git的简单实用(5) — Git分支冲突管理


1、Fast forward模式

对于我们上一章学习merge命令,其实是使用Fast-forward模式,合并方式是“快进模式”。
我们要学习禁用Fast-forward模式:
    如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。

下面我们实战一下–no-ff方式的git merge:

首先我们创建一个名称为dev的分支:
    MBP:git qiuyu$ git checkout -b dev
    切换到一个新分支 'dev' 

修改git.txt并提交一个新的commit:
    git is ok.
    git is nice.
    change branch.
    using master.
    using branch1.
    the latest.
    test no fast-forward.

    MBP:git qiuyu$ git add git.txt
    MBP:git qiuyu$ git commit -m "no fast-forward"
    [dev d60705f] no fast-forward
     1 file changed, 1 insertion(+), 1 deletion(-)

现在,我们切换回master:
    MBP:git qiuyu$ git checkout master
    切换到分支 'master'
    您的分支与上游分支 'origin/master' 一致。

准备合并dev分支,请注意--no-ff参数,表示禁用Fast forward:
    MBP:git qiuyu$ git merge --no-ff -m "merge with no-ff" dev
    Merge made by the 'recursive' strategy.
    git.txt | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-) 

合并后,我们用git log看看分支历史:
    MBP:git qiuyu$ git log --graph --pretty=oneline --abbrev-commit
    *   03d3111 merge with no-ff
    |\  
    | * d60705f no fast-forward
    |/  
    *   33605db the lateset
    |\  
    | * b00c593 branch1
    * | 89519de master
    |/  
    * d2ba8f0 branch test
    * e5798f6 the third release
    * f49806e the second release
    * 4644630 the first release

首先,master分支一般只进行提交,而不进行工作。

那在哪进行工作呢?工作都在dev分支上,工作结束,再把dev分支合并到master上,在master分支发布。

你和你的同事每个人都在dev分支上工作,每个人都有自己的分支,时不时地往dev分支上合并就可以了。


2、保存恢复现场

当出现bug我们需要修改,,你可以创建一个分支来修复它
但是,当前正在dev上进行的工作还没有提交
Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作

先修改git.txt
    git is ok.
    git is nice.
    change branch.
    using master.
    using branch1.
    the latest.
    test no fast-forward.
    test git stash.

MBP:git qiuyu$ git status
位于分支 master
您的分支领先 'origin/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)
    修改:     git.txt

通过git stash保存现场:
MBP:git qiuyu$ git stash
Saved working directory and index state WIP on master: 03d3111 merge with no-ff
HEAD 现在位于 03d3111 merge with no-ff
MBP:git qiuyu$ git status
位于分支 master
您的分支领先 'origin/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)

我们下面创建一个分支来修改并提交:
    MBP:git qiuyu$ git checkout -b debug01
    切换到一个新分支 'debug01'
    MBP:git qiuyu$ git add git.txt
    MBP:git qiuyu$ git commit -m "debug01"
    [debug01 cfe4639] debug01
     1 file changed, 1 insertion(+)

我们再切换到master分支并删除debug01:
    MBP:git qiuyu$ git checkout master
    切换到分支 'master'
    您的分支领先 'origin/master' 共 2 个提交。
      (使用 "git push" 来发布您的本地提交)
    MBP:git qiuyu$ git merge --no-ff -m "debug01" debug01
    Merge made by the 'recursive' strategy.
     git.txt | 1 +
     1 file changed, 1 insertion(+)
    MBP:git qiuyu$ git branch -d debug01
    已删除分支 debug01(曾为 cfe4639)。

工作现场通过git stash list命令看看:
    MBP:git qiuyu$ git stash list
    stash@{0}: WIP on master: 03d3111 merge with no-ff

工作现场的恢复:
一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了:
MBP:git qiuyu$ git stash list
stash@{0}: WIP on master: 03d3111 merge with no-ff
MBP:git qiuyu$ git stash pop
自动合并 git.txt
冲突(内容):合并冲突于 git.txt

通过之前消除冲突的方法即可解决。

3、测试的feature分支

如果你希望在当前分支中添加一个实验代码,不想把master弄乱,
每添加一个新功能,最好新建一个分支,在上面开发,完成后,合并,最后,删除该分支。

创建一个分支branch1:
MBP:git qiuyu$ git checkout -b branch1
切换到一个新分支 'branch1'

创建一个test.c的文件,进行提交:
MBP:git qiuyu$ vi test.c
MBP:git qiuyu$ git add test.c
MBP:git qiuyu$ git status
位于分支 branch1
要提交的变更:
  (使用 "git reset HEAD <文件>..." 撤出暂存区)
    新文件:   test.c
MBP:git qiuyu$ git commit -m "add test"
[branch1 004cc16] add test
 1 file changed, 1 insertion(+)
 create mode 100644 test.c

这时候测试分支需要丢弃,则需要使用git branch -D 命令:
首先切回master:
MBP:git qiuyu$ git checkout master
切换到分支 'master'
您的分支与上游分支 'origin/master' 一致。

对branch1进行删除:
MBP:git qiuyu$ git branch -d branch1
error: 分支 'branch1' 没有完全合并。
如果您确认要删除它,执行 'git branch -D branch1'。
MBP:git qiuyu$ git branch -D branch1
已删除分支 branch1(曾为 004cc16)。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值