git学习 分支管理(3):分支管理策略——禁用Fast forward模式(分支图)(第七天)

文章目录

  • 分支管理策略
    • 禁用Fast forward模式
    • Fast forward模式
    • 开发原则

分支管理策略

通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息

如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息

禁用Fast forward模式

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

首先,仍然创建并切换dev分支:

$ git switch -c dev
Switched to a new branch 'dev'

修改readme.txt文件,并提交一个新的commit:

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git status
On branch dev
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git add readme.txt

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   readme.txt


asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git commit -m "add merge"
[dev fa1517e] add merge
 1 file changed, 1 insertion(+)

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git status
On branch dev
nothing to commit, working tree clean

之所以列出菜鸟的,是为了提醒大家千万要 git status

现在,我们切换回master:

$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

注意:这里出现后面的

Your branch is ahead of ‘origin/master’ by 3 commits.
(use “git push” to publish your local commits)

是因为菜鸟和GitHub仓库有3次版本的跨度,而且这里后面还会用到,读者(没一起跟菜鸟学习的)可能不会出现,也不必纠结这个!

准备合并dev分支,请注意–no-ff参数,表示禁用Fast forward:

$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

注意:正如最上面说的,如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,所以才要加上-m,把commit描述写进去。

合并后,我们用 git log --graph 看看分支历史:

$ git log --graph
*   commit 8002fa2e223018c9858685f96966bfc1f8fc3250 (HEAD -> master)
|\  Merge: adc26a9 fa1517e
| | Author: ****
| | Date:   Sun Jul 12 17:34:02 2020 +0800
| |
| |     merge with no-ff
| |
| * commit fa1517e0bd07a65542bf7736c64ba3585a4cff0d (dev)
|/  Author: ****
|   Date:   Sun Jul 12 17:32:45 2020 +0800
|
|       add merge
|
*   commit adc26a9c79012f0def89aeb3362e0155de69655a
|\  Merge: 6edf4c3 2809b02
| | Author: ****
| | Date:   Sat Jul 11 16:10:20 2020 +0800
| |
| |     conflict fixed

可以看到,不使用Fast forward模式,merge后就像这样:
git学习 分支管理(3):分支管理策略——禁用Fast forward模式(分支图)(第七天)_第1张图片
那么删除分支后会有什么改变吗?

$ git branch -d dev
Deleted branch dev (was fa1517e).

注意:was fa1517e是缩写的版本号,读者的肯定和我不一样!

再次,用 git log --graph 看看分支历史:

$ git log --graph
*   commit 8002fa2e223018c9858685f96966bfc1f8fc3250 (HEAD -> master)
|\  Merge: adc26a9 fa1517e
| | Author: ****
| | Date:   Sun Jul 12 17:34:02 2020 +0800
| |
| |     merge with no-ff
| |
| * commit fa1517e0bd07a65542bf7736c64ba3585a4cff0d
|/  Author: ****
|   Date:   Sun Jul 12 17:32:45 2020 +0800
|
|       add merge
|
*   commit adc26a9c79012f0def89aeb3362e0155de69655a
|\  Merge: 6edf4c3 2809b02
| | Author: ****
| | Date:   Sat Jul 11 16:10:20 2020 +0800
| |
| |     conflict fixed

发现没有变化!

想想也是,删除的只是分支的指针,自然不会对log产生什么影响!

然后 git status ,结果如下:

On branch master
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

发现版本差距从3变成了5,git log 就可以发现:

$ git log
commit 8002fa2e223018c9858685f96966bfc1f8fc3250 (HEAD -> master)
Merge: adc26a9 fa1517e
Author: ****
Date:   Sun Jul 12 17:34:02 2020 +0800

    merge with no-ff

commit fa1517e0bd07a65542bf7736c64ba3585a4cff0d
Author: ****
Date:   Sun Jul 12 17:32:45 2020 +0800

    add merge

commit adc26a9c79012f0def89aeb3362e0155de69655a
Merge: 6edf4c3 2809b02
Author: ****
Date:   Sat Jul 11 16:10:20 2020 +0800

    conflict fixed

多的两个版本正好一个是自己commit的,一个就是禁用Fast forward模式而产生的!

Fast forward模式

例如:

$ git switch -c dev
Switched to a new branch 'dev'

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git branch
* dev
  master

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git status
On branch dev
$                                                                         e "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git add readme.txt

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git status
On branch dev
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   readme.txt


asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git commit -m "add merge2"
[dev 4165a59] add merge2
 1 file changed, 1 insertion(+), 1 deletion(-)

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git status
On branch dev
nothing to commit, working tree clean

asus@XXX MINGW64 /e/Git_warehouse (dev)
$ git switch master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.
  (use "git push" to publish your local commits)

asus@XXX MINGW64 /e/Git_warehouse (master)
$ git merge dev
Updating 8002fa2..4165a59
Fast-forward
 readme.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

asus@XXX MINGW64 /e/Git_warehouse (master)
$ git log --graph
* commit 4165a59cf8e4d6434f81231481fbb099141d0741 (HEAD -> master, dev)
| Author: ****
| Date:   Sun Jul 12 17:38:45 2020 +0800
|
|     add merge2
|
*   commit 8002fa2e223018c9858685f96966bfc1f8fc3250
|\  Merge: adc26a9 fa1517e
| | Author: ****
| | Date:   Sun Jul 12 17:34:02 2020 +0800
| |
| |     merge with no-ff
| |
| * commit fa1517e0bd07a65542bf7736c64ba3585a4cff0d
|/  Author: ****
|   Date:   Sun Jul 12 17:32:45 2020 +0800
|
|       add merge

asus@XXX MINGW64 /e/Git_warehouse (master)
$ git branch -d dev
Deleted branch dev (was 4165a59).

asus@XXX MINGW64 /e/Git_warehouse (master)
$ git log --graph
* commit 4165a59cf8e4d6434f81231481fbb099141d0741 (HEAD -> master)
| Author: ****
| Date:   Sun Jul 12 17:38:45 2020 +0800
|
|     add merge2
|
*   commit 8002fa2e223018c9858685f96966bfc1f8fc3250
|\  Merge: adc26a9 fa1517e
| | Author: ****
| | Date:   Sun Jul 12 17:34:02 2020 +0800
| |
| |     merge with no-ff
| |
| * commit fa1517e0bd07a65542bf7736c64ba3585a4cff0d
|/  Author: ****
|   Date:   Sun Jul 12 17:32:45 2020 +0800
|
|       add merge
|

asus@XXX MINGW64 /e/Git_warehouse (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

可以很轻易地发现,Fast forward模式下,分支不会被展示!而且commit也只进行了一个,所以版本只由5变成了6!

所以还是建议使用上面的一种(禁用Fast forward模式),不然没有记录是一件很令人头疼的事,而且也不直观!

开发原则

首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;

那在哪干活呢?干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,在master分支发布1.0版本;

你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并就可以了。

所以,团队合作的分支看起来就像这样:
git学习 分支管理(3):分支管理策略——禁用Fast forward模式(分支图)(第七天)_第2张图片
这里最下面两条相当于你工作的分支,dev是开发分支(你和你的同事都提交到这个分支上),master是主分支!

你可能感兴趣的:(git,git,git分支管理,分支管理策略,经验分享,其他)