Git 分支管理

幾乎每一種版本控制系統都以某種形式支援分支。使用分支意味著你可以從開發主線上分離開來,然後在不影響主線的同時繼續工作。

有人把 Git 的分支模型稱為必殺技特性,而正是因為它,將 Git 從版本控制系統家族裡區分出來。

建立分支命令:

git branch (branchname)

切換分支命令:

git checkout (branchname)

當你切換分支的時候,Git 會用該分支的最後提交的快照替換你的工作目錄的內容, 所以多個分支不需要多個目錄。

合併分支命令:

git merge 

你可以多次合併到統一分支, 也可以選擇在合併之後直接刪除被併入的分支。

開始前我們先建立一個測試目錄:

$ mkdir gitdemo
$ cd gitdemo/
$ git init
Initialized empty Git repository...
$ touch README
$ git add README
$ git commit -m '第一次版本提交'
[master (root-commit) 3b58100] 第一次版本提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

Git 分支管理

列出分支

列出分支基本命令:

git branch

沒有引數時,git branch 會列出你在本地的分支。

$ git branch
* master

此例的意思就是,我們有一個叫做 master 的分支,並且該分支是當前分支。

當你執行 git init 的時候,預設情況下 Git 就會為你建立 master 分支。

如果我們要手動建立一個分支。執行 git branch (branchname) 即可。

$ git branch testing
$ git branch
* master
  testing

現在我們可以看到,有了一個新分支 testing

當你以此方式在上次提交更新之後建立了新分支,如果後來又有更新提交, 然後又切換到了 testing 分支,Git 將還原你的工作目錄到你建立分支時候的樣子。

接下來我們將演示如何切換分支,我們用 git checkout (branch) 切換到我們要修改的分支。

$ ls
README
$ echo 'itread01.com' > test.txt
$ git add .
$ git commit -m 'add test.txt'
[master 3e92c19] add test.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
$ ls
README        test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README

當我們切換到 testing 分支的時候,我們新增的新檔案 test.txt 被移除了。切換回 master 分支的時候,它們有重新出現了。

$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

我們也可以使用 git checkout -b (branchname) 命令來建立新分支並立即切換到該分支下,從而在該分支中操作。

$ git checkout -b newtest
Switched to a new branch 'newtest'
$ git rm test.txt 
rm 'test.txt'
$ ls
README
$ touch itread01.php
$ git add .
$ git commit -am 'removed test.txt、add itread01.php'
[newtest c1501a2] removed test.txt、add itread01.php
 2 files changed, 1 deletion(-)
 create mode 100644 itread01.php
 delete mode 100644 test.txt
$ ls
README        itread01.php
$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

如你所見,我們建立了一個分支,在該分支的上移除了一些檔案 test.txt,並添加了 itread01.php 檔案,然後切換回我們的主分支,刪除的 test.txt 檔案又回來了,且新增加的 itread01.php 不存在主分支中。

使用分支將工作切分開來,從而讓我們能夠在不同開發環境中做事,並來回切換。

刪除分支

刪除分支命令:

git branch -d (branchname)

例如我們要刪除 testing 分支:

$ git branch
* master
  testing
$ git branch -d testing
Deleted branch testing (was 85fc7e7).
$ git branch
* master

分支合併

一旦某分支有了獨立內容,你終究會希望將它合併回到你的主分支。 你可以使用以下命令將任何分支合併到當前分支中去:

git merge
$ git branch
* master
  newtest
$ ls
README        test.txt
$ git merge newtest
Updating 3e92c19..c1501a2
Fast-forward
 itread01.php | 0
 test.txt   | 1 -
 2 files changed, 1 deletion(-)
 create mode 100644 itread01.php
 delete mode 100644 test.txt
$ ls
README        itread01.php

以上例項中我們將 newtest 分支合併到主分支去,test.txt 檔案被刪除。

合併完後就可以刪除分支:

$ git branch -d newtest
Deleted branch newtest (was c1501a2).

刪除後, 就只剩下 master 分支了:

$ git branch
* master

合併衝突

合併並不僅僅是簡單的檔案新增、移除的操作,Git 也會合並修改。

$ git branch
* master
$ cat itread01.php

首先,我們建立一個叫做 change_site 的分支,切換過去,我們將 itread01.php 內容改為:

<?php
echo 'itread01';
?>

建立 change_site 分支:

$ git checkout -b change_site
Switched to a new branch 'change_site'
$ vim itread01.php
$ head -3 itread01.php
<?php
echo 'itread01';
?>
$ git commit -am 'changed the itread01.php'
[change_site 7774248] changed the itread01.php
 1 file changed, 3 insertions(+)
 

將修改的內容提交到 change_site 分支中。 現在,假如切換回 master 分支我們可以看內容恢復到我們修改前的(空檔案,沒有程式碼),我們再次修改 itread01.php 檔案。

$ git checkout master
Switched to branch 'master'
$ cat itread01.php
$ vim itread01.php    # 修改內容如下
$ cat itread01.php
<?php
echo 1;
?>
$ git diff
diff --git a/itread01.php b/itread01.php
index e69de29..ac60739 100644
--- a/itread01.php
+++ b/itread01.php
@@ -0,0 +1,3 @@
+<?php
+echo 1;
+?>
$ git commit -am '修改程式碼'
[master c68142b] 修改程式碼
 1 file changed, 3 insertions(+)

現在這些改變已經記錄到我的 "master" 分支了。接下來我們將 "change_site" 分支合併過來。

$ git merge change_site
Auto-merging itread01.php
CONFLICT (content): Merge conflict in itread01.php
Automatic merge failed; fix conflicts and then commit the result.

$ cat itread01.php     # 代開檔案,看到衝突內容
<?php
<<<<<<< HEAD
echo 1;
=======
echo 'itread01';
>>>>>>> change_site
?>

我們將前一個分支合併到 master 分支,一個合併衝突就出現了,接下來我們需要手動去修改它。

$ vim itread01.php 
$ cat itread01.php
<?php
echo 1;
echo 'itread01';
?>
$ git diff
diff --cc itread01.php
index ac60739,b63d7d7..0000000
--- a/itread01.php
+++ b/itread01.php
@@@ -1,3 -1,3 +1,4 @@@
  <?php
 +echo 1;
+ echo 'itread01';
  ?>

在 Git 中,我們可以用 git add 要告訴 Git 檔案衝突已經解決

$ git status -s
UU itread01.php
$ git add itread01.php
$ git status -s
M  itread01.php
$ git commit
[master 88afe0e] Merge branch 'change_site'

現在我們成功解決了合併中的衝突,並提交了結果。