git学习二——多分支的git管理

工作空间准备

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2
$  git init lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ ls
ConcurrentArtTest/lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ touch myfile.txtlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git add .lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git commit -m "first commit"[master 8e5d948] first commit1 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 myfile.txt

目前
这里写图片描述

创建分支

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git branch issue1lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git branchissue1
* master

目前:
这里写图片描述

切换分支

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git checkout issue1Switched to branch 'issue1'

或者可以一步完成 建立分支+切换分支
git checkout -b issue1

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue1)
$ vim myfile.txt在myfile.txt中写入:i write in branch issue1;lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue1)
$ git statusOn branch issue1
Changes not staged for commit:(use "git add ..." to update what will be committed)(use "git checkout -- ..." to discard changes in working directory)modified:   myfile.txtno changes added to commit (use "git add" and/or "git commit -a")lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue1)
$ git commit -am "second commit"warning: LF will be replaced by CRLF in myfile.txt.
The file will have its original line endings in your working directory.
[issue1 warning: LF will be replaced by CRLF in myfile.txt.
The file will have its original line endings in your working directory.
b5cd4bc] second commit
warning: LF will be replaced by CRLF in myfile.txt.
The file will have its original line endings in your working directory.1 file changed, 1 insertion(+)lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue1)
$ git statusOn branch issue1
nothing to commit, working directory clean

好了,我们在分支issue1中修改并提交了 myfile.txt。
这里写图片描述

合并修改的分支到主干

建议使用 git log –pretty=oneline 查看日志

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue1)
$ git logcommit b5cd4bcdd0e14a6f5b58d91b4f661d2503c9089c
Author: lemon 
Date:   Wed Jul 6 00:03:43 2016 +0800second commitcommit 8e5d9487ee8e04f0c5b9f574aecf272691d1c537
Author: lemon 
Date:   Tue Jul 5 23:45:27 2016 +0800first commitcommit 060375dd97da0797bf9067f0e3faecefb68c4244
Author: lemon 
Date:   Sun Jul 3 02:51:32 2016 +08000lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git logcommit 8e5d9487ee8e04f0c5b9f574aecf272691d1c537
Author: lemon 
Date:   Tue Jul 5 23:45:27 2016 +0800first commitcommit 060375dd97da0797bf9067f0e3faecefb68c4244
Author: lemon 
Date:   Sun Jul 3 02:51:32 2016 +08000

可以看到在issue1分支提交的记录只会存在于issue1分支中,目前并不被master等其他分支共享.

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue1)
$ git checkout masterSwitched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.(use "git push" to publish your local commits)lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git merge issue1Updating 8e5d948..b5cd4bc
Fast-forwardmyfile.txt | 1 +1 file changed, 1 insertion(+)lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git logcommit b5cd4bcdd0e14a6f5b58d91b4f661d2503c9089c
Author: lemon <g50231160@gmail.com>
Date:   Wed Jul 6 00:03:43 2016 +0800second commitcommit 8e5d9487ee8e04f0c5b9f574aecf272691d1c537
Author: lemon <g50231160@gmail.com>
Date:   Tue Jul 5 23:45:27 2016 +0800first commitcommit 060375dd97da0797bf9067f0e3faecefb68c4244
Author: lemon <g50231160@gmail.com>
Date:   Sun Jul 3 02:51:32 2016 +08000lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$

这里写图片描述

            ——————>

这里写图片描述

                         ——————>

这里写图片描述

后两张图就是一个 fast-forward 类型的merge过程,之间把master指向issue1,则这时master共享了issue1的commit log,但是你会发现没有单独的merge相关log。

另外,可以看到my.txt合并了issue1的内容!

删除分支

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git branch -d issue1Deleted branch issue1 (was b5cd4bc).lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git statusOn branch master
Your branch is ahead of 'origin/master' by 2 commits.(use "git push" to publish your local commits)
nothing to commit, working directory clean

这里写图片描述

多分支开发情况

假如多个人同时开发:

$ git branch
* issue2issue3master

这里写图片描述

一个人在issue2分支中开发并提交!

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue2)
$ vim myfile.txtlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue2)
$ git statusOn branch issue2
Changes not staged for commit:(use "git add ..." to update what will be committed)(use "git checkout -- ..." to discard changes in working directory)modified:   myfile.txtno changes added to commit (use "git add" and/or "git commit -a")lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue2)
$ git commit -am "modify myfile issue2"[issue2 cfc4c39] modify myfile issue21 file changed, 1 insertion(+)lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue2)
$

这里写图片描述

    ——————>

这里写图片描述

另一个人在issue3中开发并提交

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3)
$ vim myfile.txtlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3)
$ git commit -am "modify myfile issue3"
[issue3 3cd42d0] modify myfile issue31 file changed, 1 insertion(+)

此时issue2内容是:
i write in branch issue1;
i write in branch issue2;
~
~
此时issue3内容是:
i write in branch issue1;
i write in branch issue3;
~
~

此时就好比另个人在base on 一个master ,分别独自开发:

这里写图片描述

合并冲突

其中一个人完成开发,开始合代码,可以看到又是一个fast-forward merge:

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue2)
$ git checkout masterSwitched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.(use "git push" to publish your local commits)lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git merge issue2Updating b5cd4bc..cfc4c39
Fast-forwardmyfile.txt | 1 +1 file changed, 1 insertion(+)

这里写图片描述

接着这个人合到主干后,另外一个人完成开发了,要合到主干:

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3)
$ git checkout masterAlready on 'master'
Your branch is ahead of 'origin/master' by 3 commits.(use "git push" to publish your local commits)
g
lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git merge issue3Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master|MERGING)

可以看到,提示说自动合并,产生了冲突,打开文件看到:

i write in branch issue1;
<<<<<<< HEAD
someone write in branch issue2;
=======
i write in branch issue3;
>>>>>>> issue3
~
~
~~

分割线下边是本次合入的代码,分割线上边是master分支的代码,这两块冲突了(不同分支修改了统一代码,不知道该要谁的代码??)。

手动修改冲突,然后再次提交!:

i write in branch issue1;
someone write in branch issue2;
i write in branch issue3;

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master|MERGING)
$ git statusOn branch master
Your branch is ahead of 'origin/master' by 3 commits.(use "git push" to publish your local commits)
You have unmerged paths.(fix conflicts and run "git commit")Unmerged paths:(use "git add ..." to mark resolution)both modified:   myfile.txtno changes added to commit (use "git add" and/or "git commit -a")lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master|MERGING)
$ git commit -am "reslove confilct and merge on master"[master c79a14e] reslove confilct and merge on masterlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git logcommit c79a14e2f9b7dd14fb67e0e27343fe8def30a61a
Merge: cfc4c39 3cd42d0
Author: lemon 0@gmail.com>
Date:   Wed Jul 6 00:39:12 2016 +0800reslove confilct and merge on mastercommit 3cd42d0aa83fdcf4c7a56676270a42c19176b84b
Author: lemon 0@gmail.com>
Date:   Wed Jul 6 00:27:14 2016 +0800modify myfile issue3commit cfc4c39c4f61e11a4df456a49d8c997fb16ed1f0
Author: lemon 0@gmail.com>
Date:   Wed Jul 6 00:22:50 2016 +0800modify myfile issue2commit b5cd4bcdd0e14a6f5b58d91b4f661d2503c9089c
Author: lemon 0@gmail.com>
Date:   Wed Jul 6 00:03:43 2016 +0800second commitcommit 8e5d9487ee8e04f0c5b9f574aecf272691d1c537
Author: lemon 0@gmail.com>
Date:   Tue Jul 5 23:45:27 2016 +0800first commitcommit 060375dd97da0797bf9067f0e3faecefb68c4244
Author: lemon 0@gmail.com>
Date:   Sun Jul 3 02:51:32 2016 +08000

这里写图片描述

如果我们用rebase做合并呢??试试看

先取消上次合并

lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)
$ git reset --hard HEAD~
HEAD is now at cfc4c39 modify myfile issue2lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3)
$ git rebase masterFirst, rewinding head to replay your work on top of it...
Applying: modify myfile issue3
Using index info to reconstruct a base tree...
M       myfile.txt
Falling back to patching base and 3-way merge...
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
error: Failed to merge in the changes.
Patch failed at 0001 modify myfile issue3
The copy of the patch that failed is found in: .git/rebase-apply/patchWhen you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

用冲突提示,打开myfile.txt查看冲突:

i write in branch issue1;
<<<<<<< cfc4c39c4f61e11a4df456a49d8c997fb16ed1f0
someone write in branch issue2;
=======
i write in branch issue3;
>>>>>>>  modify myfile issue3

cfc4c39c4f61e11a4df456a49d8c997fb16ed1f0 这个是master 最后一次的commitId
手动修改冲突然后提交:


lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3|REBASE 1/1)
$ git rebase --continuemyfile.txt: needs merge
You must edit all merge conflicts and then
mark them as resolved using git addlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3|REBASE 1/1)
$ git statusrebase in progress; onto cfc4c39
You are currently rebasing branch 'issue3' on 'cfc4c39'.(fix conflicts and then run "git rebase --continue")(use "git rebase --skip" to skip this patch)(use "git rebase --abort" to check out the original branch)Unmerged paths:(use "git reset HEAD ..." to unstage)(use "git add ..." to mark resolution)both modified:   myfile.txtno changes added to commit (use "git add" and/or "git commit -a")lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3|REBASE 1/1)
$ vim myfile.txtlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3|REBASE 1/1)
$ git add myfile.txtlenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3|REBASE 1/1)
$ git rebase --continue
Applying: modify myfile issue3lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (issue3)

这里写图片描述
——————————>
此时这样:
这里写图片描述

这样,在master分支的issue3分支就可以fast-forward合并了。切换到master分支后执行合并。

$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.(use "git push" to publish your local commits)lenovo@lenovo-PC MINGW64 /g/Git_learn/tt3/tt2 (master)$ git merge issue3
Updating cfc4c39..7842888
Fast-forwardmyfile.txt | 2 ++1 file changed, 2 insertions(+)

这里写图片描述

待续…….


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部