您好,登錄后才能下訂單哦!
git中包含多個分支和合并實現的實例分析,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
下面和大家講解一下git的使用。
一、包含多個分支和合并實現的實例
1、出現合并沖突的實例
[root@localhost ~]# mkdir -p /git/branche[root@localhost branche]# cp -rv ../wanyan/* . //偷懶一下 `../wanyan/README’ -> `./README’ `../wanyan/lib’ -> `./lib’ `../wanyan/lib/comment’ -> `./lib/comment’ `../wanyan/lib/include’ -> `./lib/include’ `../wanyan/lib/include/comment’ -> `./lib/include/comment’ `../wanyan/lib/include/main.c’ -> `./lib/include/main.c’ `../wanyan/lib/README’ -> `./lib/README’ [root@localhost branche]# git init Initialized empty Git repository in /git/branche/.git/ [root@localhost branche]# git add .[root@localhost branche]# git commit -m “1st commit”[master (root-commit) e9f37b6] 1st commit 5 files changed, 9 insertions(+), 0 deletions(-) create mode 100644 README create mode 100644 lib/README create mode 100644 lib/comment create mode 100644 lib/include/comment create mode 100644 lib/include/main.c [root@localhost branche]# cat .git/HEAD ref: refs/heads/master [root@localhost branche]# cat .git/refs/heads/master e9f37b62445a7c855108cb00455c9922ea356c29 [root@localhost branche]# git cat-file -t e9f3 Commit
第一次改變:
[root@localhost branche]# vi lib/comment include change the last change [root@localhost branche]# git commit -a -m “2rd commit”[master c2a876e] 2rd commit 1 files changed, 1 insertions(+), 0 deletions(-)
第二次改變:
[root@localhost branche]# vi README just test! another hang! last hang [root@localhost branche]# git commit -a -m “3rd commit”[master f5febf9] 3rd commit 1 files changed, 1 insertions(+), 0 deletions(-)
創建分支
[root@localhost branche]# git branch laji[root@localhost branche]# ll .git/refs/heads/ total 16 -rw-r–r– 1 root root 41 Nov 27 05:19 laji -rw-r–r– 1 root root 41 Nov 27 05:18 master [root@localhost branche]# cat .git/refs/heads/laji //以下可以看出指向同一個commit f5febf9e98c5dc2a1279a56c47642677fdea79ec [root@localhost branche]# cat .git/refs/heads/master f5febf9e98c5dc2a1279a56c47642677fdea79ec [root@localhost branche]# git branch //查看當前使用的分支 laji * master [root@localhost branche]# git checkout laji //分支的切換 Switched to branch ‘laji’ [root@localhost branche]# git branch * laji master [root@localhost branche]# cat .git/HEAD ref: refs/heads/laji
接下來演示的是分支之間的關系(互不影響),在分支laji下的修改對master分支沒任何影響。
首先是在laji分支的下的修改
[root@localhost branche]# vi README just test! another hang! last hang change for branch [root@localhost branche]# git commit -a -m “laji 4th commit”[laji b72a123] laji 4th commit 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost branche]# cat README just test! another hang! last hang change for branch [root@localhost branche]# cat .git/refs/heads/laji //commit不相同了,可見出現了分支 b72a1238f9962dd103c5839077026e7c342595ce [root@localhost branche]# cat .git/refs/heads/master f5febf9e98c5dc2a1279a56c47642677fdea79ec
然后切換到master分支觀察下
[root@localhost branche]# git checkout master Switched to branch ‘master’ [root@localhost branche]# git branch laji * master [root@localhost branche]# cat README just test! another hang! last hang 接著創造分叉(就是對mater下做出進一步的git) [root@localhost branche]# git branch laji * master [root@localhost branche]# vi README just test! another hang! last hang The master change [root@localhost branche]# git commit -a -m “master 4th commit”[master bf7bf97] master 4th commit 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost branche]# cat README //列出和laji分支做對比 just test! another hang! last hang The master change [root@localhost branche]# git checkout laji Switched to branch ‘laji’ [root@localhost branche]# cat README just test! another hang! last hang change for branch
最后就是分支的合并(把laji 合并到master),這個合并可以是不同的文件之間的合并(因為合作開發項目時,所做的工作基本是很難相同的)
[root@localhost branche]# git branch laji * master [root@localhost branche]# git merge laji Auto-merging README CONFLICT (content): Merge conflict in README Automatic merge failed; fix conflicts and then commit the result. [root@localhost branche]# cat README //因為是同一個文件的合并出現了沖突 just test! another hang! last hang The master change ======= change for branch >>>>>>> laji [root@localhost branche]# git branch laji *master [root@localhost branche]# vi README just test! another hang! last hang The master change change for branch ~ [root@localhost branche]# git add .[root@localhost branche]# git commit “last commit” 這樣就可以了,解決了沖突,提交成功。 [root@localhost branche]# git branch -D laji //刪除沒用的分支 Deleted branch laji (was b72a123).
2、不出現沖突的實例
[root@localhost other]# cd ..[root@localhost git]# mkdir another[root@localhost git]# cd another/[root@localhost another]# vi a1 wanyan ~ [root@localhost another]# vi a2 ethnicity [root@localhost another]# git init Initialized empty Git repository in /git/another/.git/ [root@localhost another]# git add .[root@localhost another]# git commit -m “1st commit”[master (root-commit) f723f47] 1st commit 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 a1 create mode 100644 a2 [root@localhost another]# git branch laji[root@localhost another]# git branch laji * master [root@localhost another]# git checkout laji Switched to branch ‘laji’ [root@localhost another]# vi a1 wanyan zhengjing [root@localhost another]# git commit -a -m “laji 2nd commit”[laji 05cda63] laji 2nd commit 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost another]# git checkout master[root@localhost another]# vi a2 ethnicity beta [root@localhost another]# git commit -a -m “mater 3rd commit”[master 1239b8e] mater 3rd commit 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost another]# cat a1 wanyan [root@localhost another]# cat a2 ethnicity beta [root@localhost another]# git checkout laji Switched to branch ‘laji’ [root@localhost another]# cat a1 wanyan zhengjing [root@localhost another]# cat a2 ethnicity [root@localhost another]# git checkout master Switched to branch ‘master’ [root@localhost another]# git merge laji Merge made by the ‘recursive’ strategy. a1 | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost another]# cat a1 wanyan zhengjing [root@localhost another]# cat a2 ethnicity beta [root@localhost another]# git branch -D laji //刪除分支 Deleted branch laji (was 05cda63). [root@localhost another]# git branch * master
二、僅有一個分支的合并實例
[root@localhost git]# mkdir other[root@localhost git]# cd other/[root@localhost other]# vim main hello ethnicitybeta [root@localhost other]# git init Initialized empty Git repository in /git/other/.git/ [root@localhost other]# git add .[root@localhost other]# git commit -m ‘1st commit’[master (root-commit) 9ef10c3] 1st commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 main [root@localhost other]# git branch wanyan[root@localhost other]# git checkout wanyan Switched to branch ‘wanyan’ [root@localhost other]# git branch master *wanyan [root@localhost other]# vi main hello ethnicitybeta wanyanzhenjiang ~ [root@localhost other]# git commit -a -m “wanyan 2nd commit”[wanyan 96aa677] wanyan 2nd commit 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost other]# cat main hello ethnicitybeta wanyanzhenjiang [root@localhost other]# git checkout master Switched to branch ‘master’ [root@localhost other]# git branch * master wanyan [root@localhost other]# cat main hello ethnicitybeta [root@localhost other]# git checkout master Switched to branch ‘master’ [root@localhost other]# git merge wanyan Updating 9ef10c3..96aa677 Fast-forward //表示被合并的分支并沒有出現分叉 main | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) [root@localhost other]# cat main hello ethnicitybeta wanyanzhenjiang
看完上述內容,你們掌握git中包含多個分支和合并實現的實例分析的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。