- Git基础概念
- Git历史(简史)
- Git基础和原理
- Git安装设置
- Git使用前配置
- Git快速入门
- Git工作流程
- Git创建存储库
- Git克隆操作
- Git执行变更操作
- Git查看更改
- Git提交更改
- Git推送(push)操作
- Git更新操作
- Git隐藏(Stash)操作
- Git移动操作
- Git重命名操作
- Git删除操作
- Git修正错误
- Git标签操作
- Git补丁操作
- Git管理分支
- Git处理冲突
- Git不同平台换行符问题
- Git远程操作详解
-
Git常用命令
- git config命令
- git help命令
- git init命令
- git add命令
- git clone命令
- git status命令
- git diff命令
- git commit命令
- git reset命令
- git rm命令
- git mv命令
- git branch命令
- git checkout命令
- git merge命令
- git mergetool命令
- git log命令
- git stash命令
- git tag命令
- git fetch命令
- git pull命令
- git push命令
- git remote命令
- git submodule命令
- git show命令
- git shortlog命令
- git describe命令
- git rebase命令
Git处理冲突
假设要在wchar_support
分支中执行更改,修改wchar_support
分支中的代码。添加一个计算长度的函数:count_len(obj)
,代码变化如下 -
$ git branch master * wchar_support Administrator@MY-PC /D/worksp/sample/src (wchar_support) $ git diff diff --git a/src/string.py b/src/string.py index ba6d584..4307fe2 100644 --- a/src/string.py +++ b/src/string.py @@ -13,4 +13,7 @@ a = '我' # b = 'ab' ab = '我ab' -print(len(a), len(b), len(ab), len('=')) \ No newline at end of file +print(len(a), len(b), len(ab), len('=')) + +def count_len(obj): + return len(obj) \ No newline at end of file
假设验证代码后,没有问题就提交这些更改。
$ git status On branch wchar_support 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: string.py no changes added to commit (use "git add" and/or "git commit -a") Administrator@MY-PC /D/worksp/sample/src (wchar_support) $ git add string.py Administrator@MY-PC /D/worksp/sample/src (wchar_support) $ git commit -m "add new function: count_len(obj)" [wchar_support 1cc9ddb] add new function: count_len(obj) file changed, 4 insertions(+), 1 deletion(-)
执行 master 分支变更
同时在master
分支中,另外一个开发人员(minsu
)还会更改了内容,并将其更改推送到master
分支。
zyiz@ubuntu:~/git/sample/src$ git diff diff --git a/src/string.py b/src/string.py index ba6d584..5eb2a5d 100644 --- a/src/string.py +++ b/src/string.py @@ -13,4 +13,6 @@ a = '我' # b = 'ab' ab = '我ab' -print(len(a), len(b), len(ab), len('=')) \ No newline at end of file +print(len(a), len(b), len(ab), len('=')) +def obj_len(obj): + return len(obj) zyiz@ubuntu:~/git/sample/src$
验证差异后,现在就提交更新内容。
zyiz@ubuntu:~/git/sample/src$ git status On branch master Your branch is up-to-date with 'origin/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: string.py no changes added to commit (use "git add" and/or "git commit -a") zyiz@ubuntu:~/git/sample/src$ git add string.py zyiz@ubuntu:~/git/sample/src$ git commit -m 'Changed function name from w_strlen to my_wc_strlen' [master 07cd5af] Changed function name from w_strlen to my_wc_strlen file changed, 3 insertions(+), 1 deletion(-) zyiz@ubuntu:~/git/sample/src$ git push origin master Username for 'http://git.oschina.net': 769728683@qq.com Password for 'http://769728683@qq.com@git.oschina.net': Counting objects: 4, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 398 bytes | 0 bytes/s, done. Total 4 (delta 1), reused 0 (delta 0) To http://git.oschina.net/zyiz/sample.git e7d1734..07cd5af master -> master
在wchar_support
分支上,我们已经实现了一个count_len(obj)
函数。假设经过测试后,提交并将其更改推送到wchar_support
分支。
出现冲突
假设另外一个开发人员(minsu
)想看看我们在wchar_branch
分支上做了什么,他试图从wchar_support
分支中拉出最新的变化,但是Git会中断这个操作,并显示以下错误消息。
zyiz@ubuntu:~/git/sample/src$ git pull origin wchar_support remote: Counting objects: 4, done. remote: Compressing objects: 100% (3/3), done. remote: Total 4 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (4/4), done. From http://git.oschina.net/zyiz/sample * branch wchar_support -> FETCH_HEAD e7d1734..1cc9ddb wchar_support -> origin/wchar_support Auto-merging src/string.py CONFLICT (content): Merge conflict in src/string.py Automatic merge failed; fix conflicts and then commit the result. zyiz@ubuntu:~/git/sample/src$
解决冲突
从错误消息中,很明显文件:src/string.py 中存在冲突。运行git diff
命令查看更多细节。
zyiz@ubuntu:~/git/sample/src$ git diff diff --cc src/string.py index 5eb2a5d,4307fe2..0000000 --- a/src/string.py +++ b/src/string.py @@@ -14,5 -14,6 +14,11 @@@ b = 'ab ab = 'æ?‘ab' print(len(a), len(b), len(ab), len('=')) ++<<<<<<< HEAD +def obj_len(obj): + return len(obj) ++======= + + def count_len(obj): - return len(obj) ++ return len(obj) ++>>>>>>> 1cc9ddb410561976b006106590481cc01b79080e zyiz@ubuntu:~/git/sample/src$
由于两个人同进修改了string.py
中的代码,所以Git处于混乱状态,并且要求用户手动解决冲突。
假设maxsu
决定保留修改的代码,并删除了自己定义的函数:obj_len(obj)
。删除冲突标记后(<<<<<<<<<<<<<<<<
和 >>>>>>>>>>>>>>>>>>>>
的行),现在冲突的代码如下所示 -
解决冲突需要修改代码后,如下所示 -
git diff
将如下所示 -
zyiz@ubuntu:~/git/sample/src$ git diff diff --cc src/string.py index 5eb2a5d,4307fe2..0000000 --- a/src/string.py +++ b/src/string.py @@@ -14,5 -14,6 +14,7 @@@ b = 'ab ab = 'æ?‘ab' print(len(a), len(b), len(ab), len('=')) + -def count_len(obj): - return len(obj) +def obj_len(obj): + return len(obj) ++ zyiz@ubuntu:~/git/sample/src$
由于minsu
已经修改了这些文件,所以必须首先提交这些修改,然后就可以提出这些修改。如下所示 -
zyiz@ubuntu:~/git/sample/src$ git add . zyiz@ubuntu:~/git/sample/src$ git commit -a -m 'Resolved conflict' [master 33f0406] Resolved conflict zyiz@ubuntu:~/git/sample/src$ git pull origin wchar_support
已经解决了冲突,现在执行git pull
应该没有问题了。
上一篇:Git管理分支
下一篇:Git不同平台换行符问题