• <nav id="wkkge"><strong id="wkkge"></strong></nav>
  • <menu id="wkkge"></menu>
  • Git教程
    Git標簽管理
    Git分支
    Git操作
    Git應用
    GitHub應用
    IDEA對于Git&GitHub的支持
    Git與GitHub使用注意事項

    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 ..." to update what will be committed)
      (use "git checkout -- ..." 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)
     1 file changed, 4 insertions(+), 1 deletion(-)
    

     

     

    執行 master 分支變更

     

    同時在master分支中,另外一個開發人員(minsu)還會更改了內容,并將其更改推送到master分支。

     

    bjpowernode@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)
    bjpowernode@ubuntu:~/git/sample/src$
    
    

     

    驗證差異后,現在就提交更新內容。

     

    bjpowernode@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 ..." to update what will be committed)
      (use "git checkout -- ..." to discard changes in working directory)
    
            modified:   string.py
    
    no changes added to commit (use "git add" and/or "git commit -a")
    bjpowernode@ubuntu:~/git/sample/src$ git add string.py
    bjpowernode@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
     1 file changed, 3 insertions(+), 1 deletion(-)
    bjpowernode@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/bjpowernode/sample.git
       e7d1734..07cd5af  master -> master
    
    

     

    在wchar_support分支上,我們已經實現了一個count_len(obj)函數。假設經過測試后,提交并將其更改推送到wchar_support分支。

     

     

    出現沖突

     

    假設另外一個開發人員(minsu)想看看我們在wchar_branch分支上做了什么,他試圖從wchar_support分支中拉出最新的變化,但是Git會中斷這個操作,并顯示以下錯誤消息。

     

    bjpowernode@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/bjpowernode/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.
    bjpowernode@ubuntu:~/git/sample/src$
    
    

     

     

    解決沖突

     

    從錯誤消息中,很明顯文件:src/string.py 中存在沖突。運行git diff命令查看更多細節。

     

    bjpowernode@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
    bjpowernode@ubuntu:~/git/sample/src$
    
    

     

    由于兩個人同進修改了string.py中的代碼,所以Git處于混亂狀態,并且要求用戶手動解決沖突。

     

    假設maxsu決定保留修改的代碼,并刪除了自己定義的函數:obj_len(obj)。刪除沖突標記后(<<<<<<<<<<<<<<<< 和 >>>>>>>>>>>>>>>>>>>>的行),現在沖突的代碼如下所示:

     

     

    解決沖突需要修改代碼后,如下所示:

     

     

    git diff將如下所示:

     

    bjpowernode@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)
    ++
    bjpowernode@ubuntu:~/git/sample/src$

     

    由于minsu已經修改了這些文件,所以必須首先提交這些修改,然后就可以提出這些修改。如下所示:

     

    bjpowernode@ubuntu:~/git/sample/src$ git add .
    bjpowernode@ubuntu:~/git/sample/src$ git commit -a -m 'Resolved conflict'
    [master 33f0406] Resolved conflict
    bjpowernode@ubuntu:~/git/sample/src$ git pull origin wchar_support
    

     

    已經解決了沖突,現在執行git pull應該沒有問題了。

     

    全部教程
  • <nav id="wkkge"><strong id="wkkge"></strong></nav>
  • <menu id="wkkge"></menu>
  • 面对面棋牌游戏