開發者user1 負責用getopt 進行命令解析的功能,由於這個功能用到getopt 函數,因而將這個分支命名爲user1/getopt.
(1)確保是在開發者user1的工做區中
cd /home/jackluo/workspace/user1/workspace/hello-world
(2)開發者user1 基於當前HEAD建立分支user1/getopt.
git branch user1/getopt
(3)使用 git branch建立分支,並不會自動切換.查看當前分支能夠看到仍然工做在master分支(用星號"*"標識)中.
[root@localhost hello-world]# git branch
* master
user1/getopt
(4)執行git checkout 命令切換到新分支上
[root@localhost hello-world]# git checkout user1/getopt
已經位於 'user1/getopt'
(5)再次查看分支列表,當前工做分支的標記符(星號)已經落在user1/getopt分支上.
[root@localhost hello-world]# git branch
master
* user1/getopt
分支其實是建立在目錄.git/refs/heads 下的引用 ,版本庫初始時建立的master分支就是在該目錄下.
查看一下.git/refs/heads 目錄下的引用 .能夠在該目錄 下看到master文件,和一個user1目錄.而在user1目錄下是文件getopt。git
[root@localhost hello-world]# ls -F .git/refs/heads/
master user1/函數
[root@localhost hello-world]# ls -F .git/refs/heads/user1/
getoptspa
引用文件 .git/refs/heads/user1/getopt記錄的是一個提交ID.命令行
[root@localhost hello-world]# cat .git/refs/heads/user1/getopt
d901dd8170f67fec607828905d5fbd91e32724003d
由於分支user1/getopt是基於頭指針HEAD建立的,所以當前該分支和master分支的指向是一致的.指針
[root@localhost hello-world]# cat .git/refs/heads/master
d901dd8170f67fec607828905d5fbd91e3272400code
===============================blog
建立分支user2/i18n開發
建立分支:執行git branch <branchname>命令建立新分支get
切換分支:執行git checkout <branchname>命令切換到新分支
git checkout -b <new_branch> [<start_point>]
檢出命令git checkout經過參數-b <new_branch> 實現了建立分支和切換分支兩個動做的合二爲一,下面是
開發者user2就使用git checkout 命令來建立分支,
(1)進入到開發者user2的工做目錄 ,並和上游同步一次
[root@localhost workspace]# cd user2/workspace/hello-world/
[root@localhost hello-world]# git pull
(2).執行git checkout -b 命令,建立並切換到新分支user2/i18n上.
[root@localhost hello-world]# git checkout -b user2/i18n 切換到一個新分支 'user2/i18n'
(3)查看本地分支列表,會看到已經建立 並切換到user2/i18n分支上了.
[root@localhost hello-world]# git branch
master
* user2/i18n
開發者user1完成功能開發
開發者user1開始在user1/getopt 分支中工做,重構hello-world 中的命令行參 數解析的代碼,重構時採用getopt_long 函數.
也能夠試着更改,不過在hello-world中已保存了一份改好的代碼,能夠直接檢出.
(1)確保是在user1的工做區中
cd ../../../user1/workspace/hello-world/
(2)執行下面的命令,用里程B jx/v2.0標記的內容(已實現用getopt 進行命令行解析的功能)替換暫存區和工做區.
下面的git checkout 命令的最後是一個點"."所以檢出只更改了暫存區和工做區,
而沒有修改頭指針.
git checkout jx/v2.0 -- .
(3)查看狀態,會看到分支仍保持爲user1/getopt,但文件src/main.c 被修改了.
[root@localhost hello-world]# git status # 位於分支 user1/getopt # 要提交的變動: # (使用 "git reset HEAD <file>..." 撤出暫存區) # # 修改: src/Makefile # 修改: src/main.c #
(4)比較暫存區和HEAD的文件差別,能夠看到爲實現用getopt進行命令行解析功能而對代碼 的改動
[root@localhost hello-world]# git diff --cached
(5)開發者user1提交代碼,完成任務 .
[root@localhost hello-world]# git commit -m "Refactor: use getopt_long for arguments parsing."
(6).提交完成以後,能夠看到這時 user1/getopt分支和master分支的指向不一樣了。
[root@localhost hello-world]# git rev-parse user1/getopt master 733dcf67eba976a61d0dc6396c9d23cb23568591 d901dd8170f67fec607828905d5fbd91e3272400
(7)編譯運行hello-world.
注意輸出中的版本號顯示.
[root@localhost src]# make clean rm -f hello main.o version.h [root@localhost src]# make version.h.in => version.h cc -c -o main.o main.c cc -o hello main.o [root@localhost src]# ./hello Hello world. (version: v1.0-1-g733dcf6)
將user1/getopt分支合併到主線
(1),爲將分支合併到主線,首先user1將工做區切換到主線,master分支.
[root@localhost src]# git checkout master 切換到分支 'master'
(2)而後執行git merge命令以合併user1/getopt 分支.
[root@localhost src]# git merge user1/getopt 更新 d901dd8..733dcf6
(3)本次合併不是常順利,實際上合併後master分支和user1/getopt指向同一個提交 ,這是由於合併前的master的提交就是user/getopt分支的父提交,因此這次合併至關於將分支master重置到user1/getopt分支
[root@localhost src]# git rev-parse user1/getopt master 733dcf67eba976a61d0dc6396c9d23cb23568591 733dcf67eba976a61d0dc6396c9d23cb23568591
(4)查看狀態信息能夠看到本地和遠程分支的跟蹤關係 .
[root@localhost src]# git status # 位於分支 master # 您的分支領先 'origin/master' 共 1 個提交。 # (使用 "git push" 來發布您的本地提交) # 無文件要提交,乾淨的工做區
(5)上面的狀態輸出中顯示本地master分支比遠程共享版本庫的master分支領先.能夠運行git cherry命令查看喜愛些提交領先(未被推送到上游跟蹤分支中).
[root@localhost src]# git cherry
+ 733dcf67eba976a61d0dc6396c9d23cb23568591
(6)執行推送操做,完成本地分支向遠程分支的同步
[root@localhost src]# git push warning: push.default 未設置,它的默認值將會在 Git 2.0 由 'matching' 修改成 'simple'。若要再也不顯示本信息並在其默認值改變後維持當前使用習慣, 進行以下設置: git config --global push.default matching 若要再也不顯示本信息並從如今開始採用新的使用習慣,設置: git config --global push.default simple 參見 'git help config' 並查找 'push.default' 以獲取更多信息。 ('simple' 模式由 Git 1.7.11 版本引入。若是您有時要使用老版本的 Git, 爲保持兼容,請用 'current' 代替 'simple' 模式) Counting objects: 21, done. Delta compression using up to 4 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (5/5), 588 bytes | 0 bytes/s, done. Total 5 (delta 3), reused 1 (delta 1) To /home/jackluo/workspace/repos/hello-world.git d901dd8..733dcf6 master -> master
(7)刪除 user1/getopt分支.
隱然特性分支user1/getopt 已經合併到主線上了,那麼分支完成了歷史命,能夠放心地將其刪除.
[root@localhost src]# git branch -d user1/getopt 已刪除分支 user1/getopt(曾爲 733dcf6)。