2013-02-24

Git Branch

Git Branch 是 Git 另外一項狂電其他版本控管系統的功能,因為它實在太輕量了,太簡單用了。
> git branch
* master  // 開發主軸事實上是一個叫做 master 的 branch

> git branch test  // 建立名為 test 的 branch

> git branch  // 有兩個 branch,目前在 master
* master
  test

> git checkout test  // 切換到 test 這個 branch
Switched to branch 'test'

> git branch  // 有兩個 branch,目前在 test
  master
* test

> git status  // 目前在 test 這個 branch
# On branch test
nothing to commit (working directory clean)

> git checkout master  // 切換回 master 這個 branch
Switched to branch 'master'

> git status // 目前在 master 這個 branch
# On branch master
nothing to commit (working directory clean)
Git Branch 就是強在可以在同一個專案裡快速切換 branch。

另外,git checkout 會修改專案檔案,所以在執行 git checkout 之前,必須確認專案環境已經 commit,否則會出錯,不然未 commit 的檔案就會一去不回。

HEAD
Git 使用 HEAD 這個 pointer 來紀錄目前在那一個 branch。

殊途同歸
當一個 branch 已經完成工作,一則留著不動,一則 merge 回 master。
> git branch
* master
  test

> git merge test
Already up-to-date.

> git branch
* master
  test

> git branch -d test
Deleted branch test (was c7e3939).

> git branch
* master
當有衝突發生時!
> git branch
* master

> git branch test  // 新增 branch

// 修改 a.txt

> git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

> git add a.txt

> git commit -m 'v1.0'
[master 1deb7e6] 'v1.0'
 1 file changed, 1 insertion(+)

> git checkout test // 切換到 test
Switched to branch 'test'

// 也修改 a.txt

> git status
# On branch test
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

> git add a.txt

> git commit -m 'v1.1'
[test 0bf60e8] 'v1.1'
 1 file changed, 1 insertion(+)

> git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.

> git merge test // 兩個 branch 都修改了 a.txt,所以無法自動 merge
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.

// 此時可以看到 a.txt 變成
<<<<<<< HEAD
v1.0
=======
v1.1
>>>>>>> test

> git add a.txt // 手動 merge a.txt 後,重新 git add

> git commit -m 'v1.2'
[master 78f144c] 'v1.2'

> git branch
* master
  test

> git branch -d test
Deleted branch test (was 0bf60e8).

更多指令

列出所有 branch
> git branch --list // 列出所有本機的 branch
* master
  v1.0
  v1.1
  v1.5
  v2.0
  v2.5

> git branch --list v* // 列出所有本機以 v 開頭的 branch
  v1.0
  v1.1
  v1.5
  v2.0
  v2.5

> git branch --list v1.* // 列出所有本機以 v1. 開頭的 branch
  v1.0
  v1.1
  v1.5

> git branch // 列出所有本機的 branch,可以省略 --list,但不能加上 pattern
* master

> git branch -r // 列出所有遠端的 branch,也可以使用 --remotes
  origin/HEAD -> origin/master
  origin/master

> git branch -a // 列出所有的 branch,包括本機與遠端,也可以使用 --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

找出已經 merge 且可以刪除的 branch
> git branch
* master

> git checkout -b bug12 // 等於 git branch bug12 加上 git checkout bug12
Switched to a new branch 'bug12'

// 新增檔案 bug12.txt

> git add bug12.txt // 加入追蹤

> git commit -m 'bug12.txt' // 交付
[bug12 b24baa9] 'bug12.txt'
 0 files changed
 create mode 100644 bug12.txt

> git checkout master // 切回 master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.

> git merge bug12 // 將 bug12 merge 回 master 之後,bug12 就可以刪除了
Updating a72c375..b24baa9
Fast-forward
 0 files changed
 create mode 100644 bug12.txt

> git checkout -b bug16 // 新增 bug16 branch 並切換過去
Switched to a new branch 'bug16'

// 新增檔案 bug16.txt

> git add bug16.txt // 加入追蹤

> git commit -m 'bug16.txt' // 交付
[bug16 92b07a5] 'bug16.txt'
 0 files changed
 create mode 100644 bug16.txt

> git checkout master // 切回 master,但沒有將 bug16 merge 回 master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 5 commits.

> git branch --merged // 列出已經 merge 的 branch,星號開頭以外的 branch 都可以刪除
  bug12
* master

> git branch --no-merged // 列出尚未 merge 的 branch,表示不可以刪除
  bug16


回到 Git 學習筆記
---
---
---

沒有留言:

張貼留言