git 创建branch分支
開(kāi)發(fā)者user1 負(fù)責(zé)用getopt 進(jìn)行命令解析的功能,因?yàn)檫@個(gè)功能用到getopt 函數(shù),于是將這個(gè)分支命名為user1/getopt.
(1)確保是在開(kāi)發(fā)者user1的工作區(qū)中
cd /home/jackluo/workspace/user1/workspace/hello-world
(2)開(kāi)發(fā)者user1 基于當(dāng)前HEAD創(chuàng)建分支user1/getopt.
git branch user1/getopt
(3)使用 git branch創(chuàng)建分支,并不會(huì)自動(dòng)切換.查看當(dāng)前分支可以看到仍然工作在master分支(用星號(hào)"*"標(biāo)識(shí))中.
[root@localhost hello-world]# git branch
* master
user1/getopt
(4)執(zhí)行g(shù)it checkout 命令切換到新分支上
[root@localhost hello-world]# git checkout user1/getopt
已經(jīng)位于 'user1/getopt'
(5)再次查看分支列表,當(dāng)前工作分支的標(biāo)記符(星號(hào))已經(jīng)落在user1/getopt分支上.
[root@localhost hello-world]# git branch
master
* user1/getopt
分支實(shí)際上是創(chuàng)建在目錄.git/refs/heads 下的引用 ,版本庫(kù)初始時(shí)創(chuàng)建的master分支就是在該目錄下.
查看一下.git/refs/heads 目錄下的引用 .可以在該目錄 下看到master文件,和一個(gè)user1目錄.而在user1目錄下是文件getopt。
[root@localhost hello-world]# ls -F .git/refs/heads/
master user1/
[root@localhost hello-world]# ls -F .git/refs/heads/user1/
getopt
引用文件 .git/refs/heads/user1/getopt記錄的是一個(gè)提交ID.
[root@localhost hello-world]# cat .git/refs/heads/user1/getopt
d901dd8170f67fec607828905d5fbd91e3272400
因?yàn)榉种ser1/getopt是基于頭指針HEAD創(chuàng)建的,因此當(dāng)前該分支和master分支的指向是一致的.
[root@localhost hello-world]# cat .git/refs/heads/master
d901dd8170f67fec607828905d5fbd91e3272400
===============================
創(chuàng)建分支user2/i18n
創(chuàng)建分支:執(zhí)行g(shù)it branch <branchname>命令創(chuàng)建新分支
切換分支:執(zhí)行g(shù)it checkout <branchname>命令切換到新分支
git checkout -b <new_branch> [<start_point>]
檢出命令git checkout通過(guò)參數(shù)-b <new_branch> 實(shí)現(xiàn)了創(chuàng)建分支和切換分支兩個(gè)動(dòng)作的合二為一,下面是
開(kāi)發(fā)者user2就使用git checkout 命令來(lái)創(chuàng)建分支,
(1)進(jìn)入到開(kāi)發(fā)者user2的工作目錄 ,并和上游同步一次
[root@localhost workspace]# cd user2/workspace/hello-world/ [root@localhost hello-world]# git pull
(2).執(zhí)行g(shù)it checkout -b 命令,創(chuàng)建并切換到新分支user2/i18n上.
[root@localhost hello-world]# git checkout -b user2/i18n 切換到一個(gè)新分支 'user2/i18n'
(3)查看本地分支列表,會(huì)看到已經(jīng)創(chuàng)建 并切換到user2/i18n分支上了.
[root@localhost hello-world]# git branch master * user2/i18n
開(kāi)發(fā)者user1完成功能開(kāi)發(fā)
開(kāi)發(fā)者user1開(kāi)始在user1/getopt 分支中工作,重構(gòu)hello-world 中的命令行參 數(shù)解析的代碼,重構(gòu)時(shí)采用getopt_long 函數(shù).
也可以試著更改,不過(guò)在hello-world中已保存了一份改好的代碼,可以直接檢出.
(1)確保是在user1的工作區(qū)中
cd ../../../user1/workspace/hello-world/
(2)執(zhí)行下面的命令,用里程B jx/v2.0標(biāo)記的內(nèi)容(已實(shí)現(xiàn)用getopt 進(jìn)行命令行解析的功能)替換暫存區(qū)和工作區(qū).
下面的git checkout 命令的最后是一個(gè)點(diǎn)"."因此檢出只更改了暫存區(qū)和工作區(qū),
而沒(méi)有修改頭指針.
git checkout jx/v2.0 -- .
(3)查看狀態(tài),會(huì)看到分支仍保持為user1/getopt,但文件src/main.c 被修改了.
[root@localhost hello-world]# git status # 位于分支 user1/getopt # 要提交的變更: # (使用 "git reset HEAD <file>..." 撤出暫存區(qū)) # # 修改: src/Makefile # 修改: src/main.c #
(4)比較暫存區(qū)和HEAD的文件差異,可以看到為實(shí)現(xiàn)用getopt進(jìn)行命令行解析功能而對(duì)代碼 的改動(dòng)
[root@localhost hello-world]# git diff --cached
(5)開(kāi)發(fā)者user1提交代碼,完成任務(wù) .
[root@localhost hello-world]# git commit -m "Refactor: use getopt_long for arguments parsing."
(6).提交完成之后,可以看到這時(shí) user1/getopt分支和master分支的指向不同了。
[root@localhost hello-world]# git rev-parse user1/getopt master 733dcf67eba976a61d0dc6396c9d23cb23568591 d901dd8170f67fec607828905d5fbd91e3272400
(7)編譯運(yùn)行hello-world.
注意輸出中的版本號(hào)顯示.
[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將工作區(qū)切換到主線,master分支.
[root@localhost src]# git checkout master 切換到分支 'master'
(2)然后執(zhí)行g(shù)it merge命令以合并user1/getopt 分支.
[root@localhost src]# git merge user1/getopt 更新 d901dd8..733dcf6
(3)本次合并非常順利,實(shí)際上合并后master分支和user1/getopt指向同一個(gè)提交 ,這是因?yàn)楹喜⑶暗膍aster的提交就是user/getopt分支的父提交,所以此次合并相當(dāng)于將分支master重置到user1/getopt分支
[root@localhost src]# git rev-parse user1/getopt master 733dcf67eba976a61d0dc6396c9d23cb23568591 733dcf67eba976a61d0dc6396c9d23cb23568591
(4)查看狀態(tài)信息可以看到本地和遠(yuǎn)程分支的跟蹤關(guān)系 .
[root@localhost src]# git status # 位于分支 master # 您的分支領(lǐng)先 'origin/master' 共 1 個(gè)提交。 # (使用 "git push" 來(lái)發(fā)布您的本地提交) # 無(wú)文件要提交,干凈的工作區(qū)
(5)上面的狀態(tài)輸出中顯示本地master分支比遠(yuǎn)程共享版本庫(kù)的master分支領(lǐng)先.可以運(yùn)行g(shù)it cherry命令查看喜好些提交領(lǐng)先(未被推送到上游跟蹤分支中).
[root@localhost src]# git cherry + 733dcf67eba976a61d0dc6396c9d23cb23568591
(6)執(zhí)行推送操作,完成本地分支向遠(yuǎn)程分支的同步
[root@localhost src]# git push warning: push.default 未設(shè)置,它的默認(rèn)值將會(huì)在 Git 2.0 由 'matching' 修改為 'simple'。若要不再顯示本信息并在其默認(rèn)值改變后維持當(dāng)前使用習(xí)慣, 進(jìn)行如下設(shè)置: git config --global push.default matching 若要不再顯示本信息并從現(xiàn)在開(kāi)始采用新的使用習(xí)慣,設(shè)置: git config --global push.default simple 參見(jiàn) 'git help config' 并查找 'push.default' 以獲取更多信息。 ('simple' 模式由 Git 1.7.11 版本引入。如果您有時(shí)要使用老版本的 Git, 為保持兼容,請(qǐng)用 '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 已經(jīng)合并到主線上了,那么分支完成了歷史命,可以放心地將其刪除.
[root@localhost src]# git branch -d user1/getopt 已刪除分支 user1/getopt(曾為 733dcf6)。
總結(jié)
以上是生活随笔為你收集整理的git 创建branch分支的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 武警特战预备队员淘汰后怎么办
- 下一篇: Vue中实现菜单下拉、收起的动画效果