外链论坛

 找回密码
 立即注册
搜索
查看: 1|回复: 0

Git 分支管理全攻略:一篇博客带你玩转代码分支!

[复制链接]

2605

主题

4430

回帖

9911万

积分

论坛元老

Rank: 8Rank: 8

积分
99110735
发表于 12 小时前 | 显示全部楼层 |阅读模式

什么是分支?在 Git 里,分支其实就有点像一个树的枝杈,每一个分支上能够区别的文件的版本,并且不会互相干扰。

分支功能有什么用?在工作中,咱们经常是需要和别人一块研发一个项目的,此时可能你研发 A 功能,别人研发 B 功能;倘若仅有一个分支的话,那样所有人都得在这个分支上干活;倘若研发完了功能,但是别人研发完,那样还得等其他人研发完(否则研发到一半的功能,怎么给别人运用,是吧)。

此刻有了分支,完全能够创建多个分支,想提交就提交,研发完后再合并到原来的分支上,这般就不会影响(被影响)别人工作。

其他版本掌控系统如 SVN 等都有分支管理,然则用过之后你会发掘,这些版本掌控系统创建和切换分支比蜗牛还慢,简直让人没法忍受,结果分支功能成为了安排大众都不去用。

但 Git 的分支是与众区别的,无论创建、切换和删除分支,Git 在 1 秒钟之内就能完成!无论你的版本库是 1 个文件还是 1 万个文件。

图示分支的概念

咱们之前说过,每提交一个新版本,Git 就会把它们自动串成一条时间线,这条时间线便是一个分支。截止到日前仅有一条时间线,在 Git 里,这个分支叫主分支,即 master 分支。

起始的时候,master 分支是一条线,Git 用 master 指向最新的提交,再用 HEAD 指向 master,就能确定当前分支,以及当前分支的提交点:

HEAD

master

┌───┐ ┌───┐ ┌───┐

│ │───→│ │───→│ │

└───┘ └───┘ └───┘

咱们创建新的分支,例如 dev 时,Git 新建了一个指针叫 dev,指向 master 相同的提交,再把 HEAD 指向 dev,就暗示当前分支在 dev 上:

master

┌───┐ ┌───┐ ┌───┐

│ │───→│ │───→│ │

└───┘ └───┘ └───┘

dev

HEAD

Git 创建一个分支火速由于除了增多一个 dev 指针,改改 HEAD 的指向,工作区的文件都任何变化!

此刻起始,对工作区的修改和提交便是针对 dev 分支了,例如新提交一次后,dev 指针往前移动一步,而 master 指针不变:

master

┌───┐ ┌───┐ ┌───┐ ┌───┐

│ │───→│ │───→│ │───→│ │

└───┘ └───┘ └───┘ └───┘

dev

HEAD

假如咱们在 dev 上的工作完成为了,就能够把 dev 合并到 master 上。Git 怎么合并呢?最简单的办法便是直接把 master 指向 dev 的当前提交,就完成为了合并:

HEAD

master

┌───┐ ┌───┐ ┌───┐ ┌───┐

│ │───→│ │───→│ │───→│ │

└───┘ └───┘ └───┘ └───┘

dev

因此 Git 合并分支火速!就改改指针,工作区内容不变!

合并完分支后,乃至能够删除 dev 分支。删除 dev 分支便是把 dev 指针给删掉,删掉后,咱们就剩下了一条 master 分支:

HEAD

master

┌───┐ ┌───┐ ┌───┐ ┌───┐

│ │───→│ │───→│ │───→│ │

└───┘ └───┘ └───┘ └───┘

实质研发中,咱们应该根据几个基本原则进行分支管理:

master 分支应该是非常稳定的,用作主分支(就像一棵树总得有个树干,不可光有树枝),便是仅用来发布新版本,平时不可在上面干活;

干活都在 dev 分支上,便是说,dev 分支是不稳定的,到某个时候,例如 1.0 版本研发完并测试完了,准备发布时,再把 dev 分支合并到 master 上

你和你的小伙伴们每一个人都有自己的分支,时不时地往 dev 分支上合并就能够了。

因此,团队合作的分支看起来就像这般

git-br-policy

创建分支

请读者务必动手实践!

咱们运用 git branch 分支名 来创建分支:

$ git

branch dev

咱们能够运用 git branch 来查看当前分支的创建状况

$ git

branch

dev

* master

git branch 命令会列出所有分支,当前分支前面会标一个 * 号,能够看到此刻有两个分支,一个是 dev,一个是 master。

而后咱们能够用 git switch 分支名 切换分支了:

$ git

switch dev

Switched to branch dev$ git

branch

* dev

master

能够一条命令创建并切换分支:

$ git

switch -c dev

倘若需要频繁切换分支,能够简写:

$ git

switch -

管理分支

删除分支

倘若咱们要删除分支,运用 git branch -d 分支名 就可重视不可删除当前分支。例如咱们当前在 dev 分支,倘若删除就报答错:

$ git

branch -d dev

error: Cannot delete branch devchecked out atD:/Projects/LearnGit

得切换到其他分支后,才可删除

$ git

switch master

Switched to branch masterYour branch is ahead of gitee/master

by 8 commits.

(use "git push"to publish yourlocal commits)$ git

branch -d dev

Deleted branch dev (was 0066f6d)

.

查看分支创建时间

git reflog show --date=iso <branch name> 命令能够查看到指定分支的历次更改记录,最下面一条的时间即是分支创建时间。

$ git reflog show --date=

iso dev

0066f6d (HEAD -> dev, master) dev@{2023-01-14 15:40:45 +0800}

: branch: Created from HEAD

重命名分支

有时候发掘创建的分支名字搞错了,要改名,怎么办?运用如下命令:

$ git branch -m <old_branch_name> <new_branch_name>

当你要重命名的分支恰好是当前分支时,就不需要指定旧的分支名叫作

$ git branch -m <new_branch_name>

查找分支

之前咱们说了查看本地分支能够

$ git

branch

bug

feature

* master

倘若要列出所有分支(本地和远程),假设 -a 参数:

$ git

branch -a

bug

feature

* master

remotes/gitee/feature

remotes/gitee/master

remotes/github/feature

remotes/github/master

合并分支

接下来咱们演示下,在其他分支上编写代码,而后合并到 master 分支。

首要还是得创建分支

$ git

switch -c dev

Switched to a new branch dev

咱们创建一个新的文件夹,用来存放咱们演示的文件。

$ mkdir

3-branch

$ echo "Creating a new branch is quick" >

3-branch/branch.txt

$ git add .$ git commit -m "branch test"

此刻,dev 分支的工作完成,咱们能够切换回 master 分支:

$ git

checkout master

切换回 master 分支后,咱们能够看到刚才创建的文件夹不见了:

$ ll

total 5

drwxr-xr-x 1 peterjxl 197121 0 1月 11 07:37 1-diffAndPath/

drwxr-xr-x 1 peterjxl 197121 0 1月 14 07:19 2-versionControl/

-rw-r--r-- 1 peterjxl 197121 33 1月 13 22:53 readme.md

由于那个提交是在 dev 分支上,而 master 分支此刻的提交点并变:

此刻咱们来合并分支:

$ git

merge dev

Updating 0066f6d..

5a512b7

Fast-forward

3-branch/branch.txt |

1 +

1 file changed, 1 insertion(+)

create mode 100644 3-branch/branch.txt

重视 Git 的提示:Fast-forward,指的是这次合并是“快进模式”,便是直接把 master 指向 dev 的当前提交,因此合并速度非常快。当然,不是每次合并都能 Fast-forward,咱们后面会讲其他方式的合并。

能够看到此刻 master 分支上,已然 dev 分支研发的内容了:

$ ll

total 5

drwxr-xr-x 1 peterjxl 197121 0 1月 11 07:37 1-diffAndPath/

drwxr-xr-x 1 peterjxl 197121 0 1月 14 07:19 2-versionControl/

drwxr-xr-x 1 peterjxl 197121 0 1月 14 15:54 3-branch/

-rw-r--r-- 1 peterjxl 197121 33 1月 13 22:53 readme.md

$ cat

3-branch/branch.txt

Creating a new branch is quick

倘若要丢弃一个被合并过的分支,能够经过 git branch -D <name> 强行删除,否则会报错:

$ git

branch -d feature-vulcan

error: The branch feature-vulcan

is not fully merged.

If you are sure you want to delete it, run git branch -D feature-vulcan.

处理冲突

合并分支不是那样一帆风顺,经常会遇到冲突:当多个人在区别分支上修改同一个文件,这般合并的时候大概率会出现冲突,咱们来实践下。

首要创建一个新的分支:

$ git

switch -c feature1

Switched to a new branch feature1

修改下 branch.txt 内容如下,并提交:

$ vim

3-branch/branch.txt

$ cat

3-branch/branch.txt

Creating a new branch is quick And simple

$ git add

3-branch/branch.txt

$ git commit -m "And simple"

切换为 master 分支,并且一样修改 branch.txt:

$ git

switch master

$ vim

3-branch/branch.txt

$ cat

3-branch/branch.txt

Creating a new branch is quick &

simple

$ git add

3-branch/branch.txt

$ gitcommit -m"&simple"

此刻,master 分支和 feature1 分支各自都分别有新的提交,变成为了这般

HEAD

master

┌───┐

┌─→│ │

┌───┐ ┌───┐ ┌───┐ │ └───┘

│ │───→│ │───→│ │──┤

└───┘ └───┘ └───┘ │ ┌───┐

└─→│ │

└───┘

feature1

这种状况下,Git 没法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,咱们试试看:

$ git

merge feature1

Auto-merging 3-branch/branch.txt

CONFLICT (content): Merge conflictin

3-branch/branch.txt

Automatic merge failed; fix conflicts and then

commit the result.

Git 告诉咱们,branch.txt 文件存在冲突,必须手动处理冲突后再提交。

git status 可以告诉咱们冲突的文件:

$ git

status

On branch master

Your branch is ahead of gitee/master

by 10 commits.

(use "git push" to publish your local commits)

You have unmerged paths.

(fix conflicts and run "git commit") (use "git merge --abort" to abort the merge)

Unmerged paths:

(use "git add <file>..." to mark resolution)

both modified: 3-branch/branch.txt

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

咱们此刻瞧瞧 branch.txt 的内容:

$ cat

3-branch/branch.txt

<<<<<<<

HEAD

Creating a new branch is quick&

simple

=======

Creating a new branch is quick And simple

>>>>>>>

feature1

Git 用 <<<<<<<,=======,>>>>>>> 标记出区别分支的内容。咱们修改如下后保留

$ cat

3-branch/branch.txt

Creating a new branch is quick and simple

再提交:

$ git add

3-branch/branch.txt

$ git commit -m "conflict fixed"[master dd140df]

conflict fixed

此刻,master 分支和 feature1 分支变成为了下图所示:

HEAD

master

┌───┐ ┌───┐

┌─→│ │───→│ │

┌───┐ ┌───┐ ┌───┐ │ └───┘ └───┘

│ │───→│ │───→│ │──┤ ▲

└───┘ └───┘ └───┘ │ ┌───┐ │

└─→│ │──────┘

└───┘

feature1

用带参数的 git log 能够看到分支的合并状况

$ git log --graph --pretty=

oneline --abbrev-commit

* dd140df (HEAD -> master)

conflict fixed

|\|* bbf0307(feature1)

And simple

* | 668f226 &

simple

|

/

* 5a512b7 (dev) branch test* 0066f6d delete test.txt by git rm

--chched

* 3ce9df0 add

test.txt

* 8a6d94d delete test.txt

* 42c477d add

test.txt

* b391595 delete test.txt

* 8889d50 add

test.txt

* 643c5ef .gitignore文件不生效,重新添加

* 3d04684 add gitignore file* 39d7f12 (github/master, gitee/master) add

readme.md

* aeb06f4 git

tracks changes

* 8f5bb58 understand how stage works

* efc9138 append GPL word

* 750360e add

distributed word

* 0282c44 wrote a readmefile* 0b3cfef add

world.txt and diff.txt

* abf2051 add diff

and patch hello.txt

倘若你用可视化图形界面,看到的结果是类似的:

其他合并方式

合并分支时,Git 会默认用 Fast forward 模式,但这种模式下,删除分支后,会丢掉分支信息。

倘若禁用了 Fast forward 模式,Git 就会在 merge 时生成一个新的 commit,这般,从分支历史上就能够看出分支信息。

下面咱们实战一下 --no-ff 方式的 git merge。

咱们创建一个新的分支 feature02,并修改内容后提交:

$ gitswitch -

c feature02

Switched to a new branch feature02$ vim 3-branch/branch.

txt

$ cat 3-branch/branch.

txt

Creating a new branch

is quick and simple

test no fast forward

$ git add 3-branch/branch.

txt

$ git commit -m "test"

此刻咱们切换回 master,并合并分支:

$ git

switch master

$ git merge --no-ff -m "merge with no-ff"

feature02

Merge made by therecursive

strategy.

3-branch/branch.txt |

1 +

1 file changed, 1 insertion(+)

由于这次合并要创建一个新的 commit,因此加上 -m 参数,把 commit 描述写进去。

合并后,咱们用 git log 瞧瞧分支历史:

$ git log --graph --pretty=oneline --abbrev-

commit

* f564208 (HEAD -> master) merge with no-

ff

|

\

| * 8f87605 (feature02)

test

|/*

dd140df conflict fixed

|

\

| *

bbf0307 And simple

* | 668f226 &

simple

|/*

5a512b7 branch test

* 0066f6d delete test.txt by git rm --

chched

* 3ce9df0 add test.

txt

* 8a6d94d delete test.

txt

* 42c477d add test.

txt

* b391595 delete test.

txt

* 8889d50 add test.

txt

* 643c5ef .

gitignore文件不生效,重新添加

*

3d04684 add gitignore file

* 39d7f12 (github/master, gitee/master) add readme.

md

*

aeb06f4 git tracks changes

*

8f5bb58 understand how stage works

*

efc9138 append GPL word

*

750360e add distributed word

*

0282c44 wrote a readme file

*0b3cfef add world.txt and diff.

txt

* abf2051 add diff and patch hello.

tx

能够看到,不运用 Fast forward 模式,merge 后就像这般

git-no-ff-mode

用 GitExtensions 查看,结果类似:

合并分支时,加上 --no-ff 参数就能够用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而 fast forward 合并就看不出来曾经做过合并。

比较分支的差异

有时候咱们需要比较两个分支的差异,能够运用如下命令:

$ git diff

branch1 branch2 --stat   //表示出所有有差异的文件列表

$ git diff branch1 branch2 文件名(带路径)

  //表示指定文件的仔细差异

$ git diff

branch1 branch2                   //表示出所有有差异的文件的仔细差异

(未完待续...)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

站点统计|Archiver|手机版|小黑屋|外链论坛 ( 非经营性网站 )|网站地图

GMT+8, 2024-10-3 18:17 , Processed in 0.070624 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.