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

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

猜你喜欢

转载自cakin24.iteye.com/blog/2345362