6. git的分支管理
6.1 理解git的分支
在版本控制过程中,在同时推进多个任务时,为每个任务都创建单独的分支,程序员们在开发自己的分支,不会影响主线分支的运行。
使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。
6.2 分支管理
- 创建分支
git branch 分支 - 切换分支
git checkout 分支名
git checkout -b change_site 创建change_site同时切换到change_site分支上。 - 把指定的分支合并到当前分支上
git merge 分支名 - 列出分支
git branch
没有参数的时候列出本地的分支 - 删除分支
git branch -d (branchname)
创建一个新的分支testing
$ git branch
* master
$ git branch testing
$ git branch
* master
testing
假如你在上次提交更新之后创建新的分支,如果后来又有了更新提交,接下来你又切换到了testing分支,
Git将还原你的工作目录到你创建分支时候的数据状态。
6.2.1 分支合并
更新之前有master和testing
$ git branch testing # 创建testing分支
$ git branch
* master
testing
$ mkdir gitdemo
$ cd gitdemo
$ echo 'first update master' > test.txt
$ cd ..
$ git add .
$ git commit -m 'add text.txt' # 更新提交到本地仓库
$ git checkout testing # 切换到testing分支,git还原到了testing分支创建时的数据状态。创建的gitdemo目录和test.txt文件被移出了。
$ git checkout master # 切换回 master 分支的时候,它们又重新出现了
使用分支将工作切分开来,从而让我们能够在不同开发环境中做事,并来回切换。
6.2.1 分支冲突
合并不仅是简单的文件添加,移出的操作,Git也会合并修改。
git merge
$ touch runoob.php # 首先在master
$ git add runoob.php
$ git commit -m 'touch runoob.php';
$ git checkout -b feature #创建一个分支并切换到feature
$ vim runoob.php
$ git add runoob.php
$ git commit -m 'feature simple';
$ git checkout master # 切换到master
$ cat runoob.php
$ vim runoob.php
$ git diff
warning: LF will be replaced by CRLF in runoob.php.
The file will have its original line endings in your working directory
diff --git a/runoob.php b/runoob.php
index e69de29..ac60739 100644
--- a/runoob.php
+++ b/runoob.php
@@ -0,0 +1,3 @@
+<?php
+echo 1;
+?>
$ git add runoob.php
warning: LF will be replaced by CRLF in runoob.php.
The file will have its original line endings in your working directory
$ git commit -m 'master php';
[master 42544b9] master php
1 file changed, 3 insertions(+)
$ git merge feature # 合并feature到master上
Auto-merging runoob.php
CONFLICT (content): Merge conflict in runoob.php
Automatic merge failed; fix conflicts and then commit the result.
$ cat runoob.php # 查看合并后的runoob.php, 这里要手动修改冲突
<<<<<<< HEAD
echo 1;
=======
echo 'runoob';
>>>>>>> feature
?>
$ vim runoob.php
$ git add runoob.php
$ git commit -m 'confilt fixed';
$ cat runoob.php # 这是冲突已经解决了
<?php
echo 'runoob';
$ git status -s
?? runoob.txt
?>
冲突解决后,删除feature分支
$ git branch -d feature
Deleted branch feature (was 1d5bd91).