git 处理冲突

  • 处理冲突

    Jerry正在wchar_support分支上。他更改了功能的名称,并在测试后提交更改。
    
    [jerry@CentOS src]$ git branch
     master
    * wchar_support
    [jerry@CentOS src]$ git diff
    
    上面的命令产生以下结果-
    
    diff --git a/src/string_operations.c b/src/string_operations.c
    index 8fb4b00..01ff4e0 100644
    --- a/src/string_operations.c
    +++ b/src/string_operations.c
    @@ -1,7 +1,7 @@
    #include <stdio.h>
    #include <wchar.h>
    -size_t w_strlen(const wchar_t *s)
    +size_t my_wstrlen(const wchar_t *s)
    {
       const wchar_t *p = s;
    
    验证代码后,他提交更改。
    
    [jerry@CentOS src]$ git status -s
    M string_operations.c
    
    [jerry@CentOS src]$ git add string_operations.c
    
    [jerry@CentOS src]$ git commit -m 'Changed function name'
    [wchar_support 3789fe8] Changed function name
    1 files changed, 1 insertions(+), 1 deletions(-)
    
    [jerry@CentOS src]$ git push origin wchar_support
    
    上面的命令将产生以下结果-
    
    Counting objects: 7, done.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 409 bytes, done.
    Total 4 (delta 1), reused 0 (delta 0)
    To gituser@git.server.com:project.git
    64192f9..3789fe8 wchar_support -> wchar_support
    
  • 在master分支中执行更改

    同时,在master分支中,Tom还更改了相同功能的名称,并将其更改推送到master分支。
    
    [tom@CentOS src]$ git branch
    * master
    [tom@CentOS src]$ git diff
    
    上面的命令产生以下结果
    
    diff --git a/src/string_operations.c b/src/string_operations.c
    index 8fb4b00..52bec84 100644
    --- a/src/string_operations.c
    +++ b/src/string_operations.c
    @@ -1,7 +1,8 @@
    #include <stdio.h>
    #include <wchar.h>
    -size_t w_strlen(const wchar_t *s)
    +/* wide character strlen fucntion */
    +size_t my_wc_strlen(const wchar_t *s)
    {
       const wchar_t *p = s;
    
    验证差异后,他提交更改。
    
    [tom@CentOS src]$ git status -s
    M string_operations.c
    
    [tom@CentOS src]$ git add string_operations.c
    
    [tom@CentOS src]$ git commit -m 'Changed function name from w_strlen to my_wc_strlen'
    [master ad4b530] Changed function name from w_strlen to my_wc_strlen
    1 files changed, 2 insertions(+), 1 deletions(-)
    
    [tom@CentOS src]$ git push origin master
    
    上面的命令将产生以下结果-
    
    Counting objects: 7, done.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 470 bytes, done.
    Total 4 (delta 1), reused 0 (delta 0)
    To gituser@git.server.com:project.git
    64192f9..ad4b530 master -> master
    
    在wchar_support分支上,Jerry为宽字符串实现了strchr函数。测试之后,他提交并将更改推送到wchar_support分支。
    
    [jerry@CentOS src]$ git branch
    master
    * wchar_support
    [jerry@CentOS src]$ git diff
    
    上面的命令产生以下结果-
    
    diff --git a/src/string_operations.c b/src/string_operations.c
    index 01ff4e0..163a779 100644
    --- a/src/string_operations.c
    +++ b/src/string_operations.c
    @@ -1,6 +1,16 @@
    #include <stdio.h>
    #include <wchar.h>
    +wchar_t *my_wstrchr(wchar_t *ws, wchar_t wc)
    +
    {
       +
       while (*ws) 
       {
          +
          if (*ws == wc)
          +
          return ws;
          +
          ++ws;
          + 
       }
       + return NULL;
       +
    }
    +
    size_t my_wstrlen(const wchar_t *s)
    {
       const wchar_t *p = s;
    
    验证后,他提交更改。
    
    [jerry@CentOS src]$ git status -s
    M string_operations.c
    
    [jerry@CentOS src]$ git add string_operations.c
    
    [jerry@CentOS src]$ git commit -m 'Addded strchr function for wide character string'
    [wchar_support 9d201a9] Addded strchr function for wide character string
    1 files changed, 10 insertions(+), 0 deletions(-)
    
    [jerry@CentOS src]$ git push origin wchar_support
    
    上面的命令将产生以下结果-
    
    Counting objects: 7, done.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 516 bytes, done.
    Total 4 (delta 1), reused 0 (delta 0)
    To gituser@git.server.com:project.git
    3789fe8..9d201a9 wchar_support -> wchar_support
    
  • 解决冲突

    Tom想要了解Jerry在其私有分支上正在做什么,因此,他尝试从wchar_support分支中提取最新更改,但是Git中止操作并显示以下错误消息。
    
    [tom@CentOS src]$ git pull origin wchar_support
    
    上面的命令产生以下结果-
    
    remote: Counting objects: 11, done.
    63Git Tutorials
    remote: Compressing objects: 100% (8/8), done.
    remote: Total 8 (delta 2), reused 0 (delta 0)
    Unpacking objects: 100% (8/8), done.
    From git.server.com:project
    * branch
    wchar_support -> FETCH_HEAD
    Auto-merging src/string_operations.c
    CONFLICT (content): Merge conflict in src/string_operations.c
    Automatic merge failed; fix conflicts and then commit the result.
    
    解决冲突
    从错误消息中可以明显看出src/string_operations.c中存在冲突。他运行git diff命令以查看更多详细信息。
    
    [tom@CentOS src]$ git diff
    
    上面的命令产生以下结果-
    
    diff --cc src/string_operations.c
    index 52bec84,163a779..0000000
    --- a/src/string_operations.c
    +++ b/src/string_operations.c
    @@@ -1,8 -1,17 +1,22 @@@
    #include <stdio.h>
    #include <wchar.h>
    ++<<<<<<< HEAD
    +/* wide character strlen fucntion */
    +size_t my_wc_strlen(const wchar_t *s)
    ++=======
    + wchar_t *my_wstrchr(wchar_t *ws, wchar_t wc)
    +
    {
       +
       +
       while (*ws) 
       {
          if (*ws == wc)
          +
          return ws;
          +
          ++ws;
          + 
       }
       + return NULL;
       +
    }
    +
    + size_t my_wstrlen(const wchar_t *s)
    ++>>>>>>>9d201a9c61bc4713f4095175f8954b642dae8f86
    {
       const wchar_t *p = s;
    
    由于Tom和Jerry都更改了相同函数的名称,因此git处于混乱状态,它要求用户手动解决冲突。Tom决定保留Jerry建议的函数名称,但他保留他自己添加的注释。删除冲突标记后,git diff将如下所示。
    
    [tom@CentOS src]$ git diff
    
    上面的命令产生以下结果。
    
    diff --cc src/string_operations.c
    diff --cc src/string_operations.c
    index 52bec84,163a779..0000000
    --- a/src/string_operations.c
    +++ b/src/string_operations.c
    @@@ -1,8 -1,17 +1,18 @@@
    #include <stdio.h>
    #include <wchar.h>
    + wchar_t *my_wstrchr(wchar_t *ws, wchar_t wc)
    +
    {
       +
       while (*ws) 
       {
          +
          if (*ws == wc)
          +
          return ws;
          +
          ++ws;
          + 
       }
       + return NULL;
       +
    }
    +
    +/* wide character strlen fucntion */
    - size_t my_wc_strlen(const wchar_t *s)
    + size_t my_wstrlen(const wchar_t *s)
    {
       const wchar_t *p = s;
    
    Tom修改了文件后,他必须先提交这些更改,然后才能提交更改。
    
    [tom@CentOS src]$ git commit -a -m 'Resolved conflict'
    [master 6b1ac36] Resolved conflict
    
    [tom@CentOS src]$ git pull origin wchar_support.
    
    Tom解决了冲突,现在拉操作将成功。
  • 小知识

    GNU/Linux和Mac OS使用换行符(LF)或换行符作为换行符,而Windows使用换行符和回车符(LFCR)组合来表示换行符。为了避免由于这些行尾差异而导致不必要的提交,我们必须配置Git客户端以将同一行结尾写入Git存储库。对于Windows系统,我们可以配置Git客户端以在签出(checkout)时将行尾转换为CRLF格式,并在提交操作期间将它们转换回LF格式。以下设置将很有用。
    
    [tom@CentOS project]$ git config --global core.autocrlf true
    
    对于GNU/Linux或Mac OS,我们可以将Git客户端配置为在执行结帐操作时将行尾从CRLF转换为LF。
    
    [tom@CentOS project]$ git config --global core.autocrlf input