`
cakin24
  • 浏览: 1369311 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Git 分支管理

阅读更多

几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着可以从开发主线上分离开来,然后在不影响主线的同时继续工作。

有人把 Git 的分支模型称为"必杀技特性",而正是因为它,将 Git 从版本控制系统家族里区分出来。

创建分支命令:

git branch (branchname)

切换分支命令:

git checkout (branchname)

当你切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容, 所以多个分支不需要多个目录。

合并分支命令:

git merge 

你可以多次合并到统一分支, 也可以选择在合并之后直接删除被并入的分支。


列出分支

列出分支基本命令:

git branch

没有参数时,git branch 会列出你在本地的分支。

[root@localhost newrepo]# git branch
* master

此例的意思就是,我们有一个叫做"master"的分支,并且该分支是当前分支。

当你执行 git init 的时候,缺省情况下 Git 就会为你创建"master"分支。

如果我们手动创建一个分支。执行 git branch (branchname) 即可。

[root@localhost newrepo]# git branch testing
[root@localhost newrepo]# git branch
* master
  testing

现在我们可以看到,有了一个新分支 testing。

以此方式在上次提交更新之后创建了新分支,如果后来又有更新提交, 然后又切换到了"testing"分支,Git 将还原你的工作目录到你创建分支时候的样子

接下来演示如何切换分支,我们用 git checkout (branch) 切换到我们要修改的分支。

[root@localhost newrepo]# ls
test.c
[root@localhost newrepo]# echo 'runoob.com' > test.txt
[root@localhost newrepo]# git add .
[root@localhost newrepo]# git commit -m 'add test.txt'
[master 76f8be1] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
[root@localhost newrepo]# ls
test.c  test.txt
[root@localhost newrepo]# git checkout testing
Switched to branch 'testing'
[root@localhost newrepo]# ls
test.c[root@localhost newrepo]# git branch
  master
* testing

当切换到"testing"分支的时候,我们添加的新文件test.txt被移除了, 切换回"master"分支的时候,它有重新出现了。

[root@localhost newrepo]# git checkout master
Switched to branch 'master'
[root@localhost newrepo]# ls
test.c  test.txt

可以使用 git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下,从而在该分支中操作。

[root@localhost newrepo]# git checkout -b newtest
Switched to a new branch 'newtest'
[root@localhost newrepo]# ls
test.c  test.txt
[root@localhost newrepo]# git rm test.c
rm 'test.c'
[root@localhost newrepo]# ls
test.txt
[root@localhost newrepo]# git commit -am 'remove test.c'
[newtest 5ceb56c] remove test.c
 1 file changed, 4 deletions(-)
 delete mode 100644 test.c
[root@localhost newrepo]# git checkout master
Switched to branch 'master'
[root@localhost newrepo]# ls
test.c  test.txt

我们创建了一个分支,在该分支的上下文中移除了一个文件,然后切换回我们的主分支,那个文件又回来了。

使用分支将工作切分开来,从而让我们能够在不同上下文中做事,并来回切换。

删除分支

删除分支命令:

git branch -d (branchname)

要删除"testing"分支:

[root@localhost newrepo]# git branch
* master
  newtest
  testing
[root@localhost newrepo]# git branch -d testing
Deleted branch testing (was afe659f).
[root@localhost newrepo]# git branch
* master
  newtest

分支合并

一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。 你可以使用以下命令将任何分支合并到当前分支中去:

git merge
[root@localhost newrepo]# git branch
* master
  newtest
[root@localhost newrepo]# ls
test.c  test.txt
[root@localhost newrepo]# git merge newtest
Updating 76f8be1..5ceb56c
Fast-forward
 test.c | 4 ----
 1 file changed, 4 deletions(-)
 delete mode 100644 test.c
[root@localhost newrepo]# ls
test.txt

以上实例中我们将 newtest 分支合并到主分支去,test2.txt 文件被删除。

合并冲突

合并并不仅仅是简单的文件添加、移除的操作,Git 也会合并修改。

[root@localhost newrepo]# git branch
* master
  newtest
[root@localhost newrepo]# cat test.txt
runoob.com

首先,我们创建一个叫做"change_site"的分支,切换过去,我们将内容改为 www.runoob.com 。

[root@localhost newrepo]# git checkout -b change_site
Switched to a new branch 'change_site'
[root@localhost newrepo]# vim test.txt
[root@localhost newrepo]# head -1 test.txt
www.runoob.com
[root@localhost newrepo]# git commit -am 'changed the site'
[change_site f79f097] changed the site
 1 file changed, 1 insertion(+), 1 deletion(-)

上面的命令将修改的内容提交到 "change_site" 分支中。 

现在,切换回 "master" 分支,我们可以看到内容恢复到我们修改前的,我们再次修改test.txt文件。

[root@localhost newrepo]# git checkout master
Switched to branch 'master'
[root@localhost newrepo]# head -1 test.txt
runoob.com
[root@localhost newrepo]# vim test.txt
[root@localhost newrepo]# cat test.txt
runoob.com
new line
[root@localhost newrepo]# git diff
diff --git a/test.txt b/test.txt
index 3277d64..08a84ff 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 runoob.com
+new line
[root@localhost newrepo]# git commit -am 'new line'
[master 4f9493f] new line
 1 file changed, 1 insertion(+)

现在这些改变已经记录到 "master" 分支了。

此时change_site分支的test.txt内容为

www.runoob.com

master分支的test.txt的内容为

runoob.com
new line

接下来我们将 "change_site" 分支合并到主分支。

[root@localhost newrepo]# git merge change_site
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@localhost newrepo]# cat test.txt
<<<<<<< HEAD
runoob.com
new line
=======
www.runoob.com
>>>>>>> change_site

我们将change_site分支合并到 "master" 分支,一个合并冲突就出现了,接下来我们需要手动去修改它。

[root@localhost newrepo]# vim test.txt
[root@localhost newrepo]# cat test.txt
www.runoob.com
new line
[root@localhost newrepo]# git diff
diff --cc test.txt
index 08a84ff,43b979a..0000000
--- a/test.txt
+++ b/test.txt
@@@ -1,2 -1,1 +1,2 @@@
- runoob.com
+ www.runoob.com
 +new line

在 Git 中,我们可以用 git add 要告诉 Git 文件冲突已经解决

[root@localhost newrepo]# git status -s
UU test.txt
[root@localhost newrepo]# git add test.txt
[root@localhost newrepo]# git status -s
M  test.txt
[root@localhost newrepo]# git commit
[master bd7f492] Merge branch 'change_site'
[root@localhost newrepo]# git checkout change_site
Switched to branch 'change_site'
[root@localhost newrepo]# cat test.txtwww.runoob.com

现在我们成功解决了合并中的冲突,并提交了结果。

1
0
分享到:
评论

相关推荐

    git 分支管理

    ### Git分支管理详解 #### 一、分支的重要性与特点 几乎每种版本控制系统都支持分支功能,但在Git中,分支的使用方式与效率达到了前所未有的高度。传统的版本控制系统在创建分支时,通常需要复制整个项目的代码库...

    git分支管理策略

    ### git分支管理策略详解 #### 一、引言 在当今的软件开发环境中,版本控制系统是必不可少的一部分。其中,Git因其高效性和灵活性成为了最受欢迎的选择之一。对于任何希望提高团队协作效率、确保代码质量和版本可...

    GIT分支管理

    GIT分支管理 远程分支 本地分支 GIT分支管理 远程分支 本地分支

    Git分支管理的策略梳理

    Git分支管理的策略梳理 Git分支管理的策略梳理是非常重要的,因为 Git 的分支和合并功能非常强大,但如果不加注意,很可能会留下一个枝节蔓生、四处开放的版本库,到处都是分支,完全看不出主干发展的脉络。因此,...

    git分支管理

    详细讲解git分支管理,适合于代码管理、项目管理等工作。

    Git分支管理策略 - 阮一峰的网络日志

    在阮一峰的网络日志中,作者详细讨论了Git分支管理的策略,旨在保持版本库的演进简洁有序。以下是从文章中提取的关键知识点: 1. 版本管理系统的重要性:文章首先强调了版本管理系统在软件开发过程中的必要性,尤其...

    分支管理规范-GIT分支流程开发规范

    Git分支管理规范旨在为开发团队提供一个清晰、有序的工作流程,它将代码的不同阶段(如开发、测试、发布)与相应的分支关联起来,以促进协同工作和版本控制。通过规范化的分支策略,可以避免代码冲突,保证代码质量...

    git 分支管理命令及说明.md

    git 分支管理命令及说明

    Git分支管理详解

    在这一篇博客中我们来在大家讲解一下Git分支管理,这可以说是Git的又一大特点。下面我们就来学习一下Git分支管理吧。我们先来说一个简单的案例吧,你们团队中有多个人再开发一下项目,一同事再开发一个新的功能,...

    git 代码管理与版本管理.docx

    Git 代码管理与版本管理 Git 代码管理与版本管理是...Git 代码管理与版本管理是软件开发过程中不可或缺的一部分,了解 Git 的基本概念、工作流程和分支管理可以帮助开发团队更好地管理代码,提高协作效率和代码质量。

    GIT分支代码统计

    本主题聚焦于"Git分支代码统计",这是一项对于项目管理和团队协作至关重要的任务。通过统计每个分支的代码量,我们可以了解开发进度,分析贡献度,以及识别可能的问题。 在描述中提到,这个过程分为两步。第一步...

    Git分支管理实践

    Git分支管理实践是Git在软件开发中的核心应用之一,它源于Linux哲学,强调用户对自己的操作有清晰的认识并能承担责任。Git作为一个分布式版本控制系统,它的设计理念是让用户能够灵活地管理代码的不同版本,确保...

    浅谈Git分支管理策略

    Git分支管理策略是软件开发中不可或缺的一个环节,特别是在使用Git作为版本控制系统时。Git因其高效、灵活的分支和合并机制而备受青睐。本文主要探讨了如何有效地管理Git分支,以保持代码库的整洁和团队协作的高效性...

    git分支管理_动力节点Java学院整理

    标题和描述中提到的“git分支管理”是Git工作流程的关键部分。在实际开发中,分支的主要作用是隔离开发环境,使得团队成员可以在各自的分支上同时进行工作,而不会相互干扰。例如,当你在开发新功能时,你可以创建一...

    git分支版本管理.pdf

    以下是对"git分支版本管理.pdf"中提到的知识点的详细说明: 1. **主分支(master)**: 主分支代表了项目的正式发布版本,通常是最稳定的代码库,只包含已验证并上线的代码。 2. **开发分支(dev)**: 开发分支...

    git版本管理软件

    #### 六、git分支管理 - **6.2 git分支** - **创建分支**:`git branch &lt;branch-name&gt;` - **切换分支**:`git checkout &lt;branch-name&gt;` - **重命名分支**:`git branch -m &lt;new-branch-name&gt;` - **删除分支**:`...

Global site tag (gtag.js) - Google Analytics