2016年3月9日 星期三

使用git command來解決pull request造成code conflict的問題

  1. 有衝突的pull request
在github上試著合入billiam327所提交的pull request
這個是一個衝突的pull request

  1. github建議的方向


Checkout via command line

If you cannot merge a pull request automatically here, you have the option of checking it out via command line to resolve conflicts and perform a manual merge.

Step 1: From your project repository, check out a new branch and test the changes.

  git checkout -b billiam327-master master  git pull https://github.com/billiam327/sync_utility.git master    

Step 2: Merge the changes and update on GitHub.

git checkout master
git merge --no-ff billiam327-master
git push origin master

首先git branch
會顯示你正在處於master的branch

nelsoncgtekiMBP:sync_utility nelsonchung$ git branch
* master

git checkout -b billiam327-master master
就是以下兩個指令的簡化版
產生一個branch
git branch billiam327-master
將source code切換到billiam327-master下
git checkout billiam327-master
nelsoncgtekiMBP:sync_utility nelsonchung$ git checkout -b billiam327-master master
Switched to a new branch 'billiam327-master'

執行
就產生conflict囉!

nelsoncgtekiMBP:sync_utility nelsonchung$ git pull https://github.com/billiam327/sync_utility.git master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/billiam327/sync_utility
 * branch            master     -> FETCH_HEAD
Auto-merging compress.sh
CONFLICT (content): Merge conflict in compress.sh
Automatic merge failed; fix conflicts and then commit the result.

衝突的內容
可以用git diff的方式先觀察
nelsoncgtekiMBP:sync_utility nelsonchung$ git diff
diff --cc compress.sh
index f1bc6a8,4df3660..0000000
--- a/compress.sh
+++ b/compress.sh
@@@ -1,44 -1,32 +1,72 @@@
  echo "0. Compress sample cmd"
++<<<<<<< HEAD
 +echo "11. .tar.Z"
 +echo "12. .tgz"
++=======
+ echo "5. Compress .tar.gz"
+ echo "6. Decompress .tar.gz"
+ echo "7. Decompress .bz"
++>>>>>>> 25ed2d8b8c89b6a15f6c56403b4d17abae16f008
  read option
 
  case "$option" in
      "0")
      echo "Compress sample"
      ;;
++<<<<<<< HEAD
 +    "11")
 +        echo "Select action."
 +        echo "1. Compress file"
 +        echo "2. Uncompress file"
 +        read action
 +        case "$action" in
 +        "1")
 +            read -p "Please input filename : " filename
 +            read -p "Please input dirname : " dirname
 +            echo `tar zcvf $filename.tar.Z $dirname`
 +            ;;
 +        "2")
 +            read -p "Please input filename : " filename
 +            echo `tar zxvf $filename.tar.Z`
 +            ;;
 +        esac
 +    ;;
 +    "12")
 +        echo "Select action."
 +        echo "1. Compress file"
 +        echo "2. Uncompress file"
 +        read action
 +        case "$action" in
 +        "1")
 +            read -p "Please input filename : " filename
 +            echo `tar zcvf $filename.tgz $filename`
 +            ;;
 +        "2")
 +            read -p "Please input filename : " filename
 +            echo `tar zxvf $filename.tgz`
 +            ;;
 +        esac
++=======
+     "5")
+     echo "Compress .tar.gz"
+     echo "Please input filename:"
+     read filename
+     echo "Please input directory:"
+     read dirname
+     tar zcvf $filename\.tar.gz $dirname
+     ;;
+     "6")
+     echo "Decompress .tar.gz"
+     echo "Please input filename:"
+     read filename
+     tar zxvf $filename\.tar.gz
+     ;;
+     "7")
+     echo "Decompress .bz"
+     echo "Please input filename:"
+     read filename
+     bunzip2 -d $filename.bz
++>>>>>>> 25ed2d8b8c89b6a15f6c56403b4d17abae16f008
      ;;
      "*")
      echo "Not supported"

因為知道衝突是發生在compress.sh
所以就要用
vim compress.sh的方式去解決衝突問題

nelsoncgtekiMBP:sync_utility nelsonchung$ vim compress.sh
nelsoncgtekiMBP:sync_utility nelsonchung$ git diff
diff --cc compress.sh
index f1bc6a8,4df3660..0000000
--- a/compress.sh
+++ b/compress.sh
@@@ -1,44 -1,32 +1,66 @@@
  echo "0. Compress sample cmd"
+ echo "5. Compress .tar.gz"
+ echo "6. Decompress .tar.gz"
+ echo "7. Decompress .bz"
 +echo "11. .tar.Z"
 +echo "12. .tgz"
  read option
 
  case "$option" in
      "0")
      echo "Compress sample"
      ;;
+     "5")
+     echo "Compress .tar.gz"
+     echo "Please input filename:"
+     read filename
+     echo "Please input directory:"
+     read dirname
+     tar zcvf $filename\.tar.gz $dirname
+     ;;
+     "6")
+     echo "Decompress .tar.gz"
+     echo "Please input filename:"
+     read filename
+     tar zxvf $filename\.tar.gz
+     ;;
+     "7")
+     echo "Decompress .bz"
+     echo "Please input filename:"
+     read filename
+     bunzip2 -d $filename.bz
 +    "11")
 +        echo "Select action."
 +        echo "1. Compress file"
 +        echo "2. Uncompress file"
 +        read action
 +        case "$action" in
 +        "1")
 +            read -p "Please input filename : " filename
 +            read -p "Please input dirname : " dirname
 +            echo `tar zcvf $filename.tar.Z $dirname`
 +            ;;
 +        "2

解決完畢之後
就可以commit囉!
此時,你是在billiam327底下進行commit
nelsoncgtekiMBP:sync_utility nelsonchung$ git status
On branch billiam327-master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

both modified:   compress.sh

no changes added to commit (use "git add" and/or "git commit -a")
nelsoncgtekiMBP:sync_utility nelsonchung$ git commit -am "Merge billiam327's patch."
[billiam327-master 1eeb4e3] Merge billiam327's patch.
nelsoncgtekiMBP:sync_utility nelsonchung$ git status
On branch billiam327-master
nothing to commit, working directory clean

git log看一下commit是否ok

nelsoncgtekiMBP:sync_utility nelsonchung$ git log
commit 1eeb4e3839235541e2d49266bd785ebd8aa7bb0a
Merge: 7caa911 25ed2d8
Author: nelsonchung <chihchun.chung@gmail.com>
Date:   Tue Mar 8 20:06:03 2016 +0800

    Merge billiam327's patch.

commit 7caa911614c0a756b2c540cc625afe7079a40abe
Author: nelsonchung <chihchun.chung@gmail.com>
Date:   Tue Mar 1 14:48:22 2016 +0800

    Add the git command - git diff

commit 176c69b148f7152150404ec88c13732d52e2fa6e
Merge: 2a3a088 3a0fd69
Author: Nelson Chung <chihchun.chung@gmail.com>
Date:   Tue Mar 1 10:29:34 2016 +0800

    Merge pull request #2 from yayanglu/master
   
    compress.sh

commit 2a3a088888cd4dae6946d2b197cd99899c881b33
Author: nelsonchung <chihchun.chung@gmail.com>
Date:   Fri Feb 26 16:39:29 2016 +0800

    Modify gitcmd for testing.

commit 0a226fbca060c471d90ad36a8f4a426d7af19a20
Merge: 0b8a91b 8fedb7b
Author: Nelson Chung <chihchun.chung@gmail.com>
Date:   Fri Feb 26 15:54:15 2016 +0800

    Merge pull request #7 from nelsonchunggithub/master
   
    Add the command - git remote add upstream

commit 8fedb7b9241eedf3f8e10ea02f06a662bb3b1a93
Author: nelsonchung <chihchun.chung@gmail.com>
Date:   Fri Feb 26 15:48:02 2016 +0800

    Add the command - git remote add upstream

commit 0b8a91bb5034379e2b5d2d7dac8f70b5da90805d
Author: nelsonchung <chihchun.chung@gmail.com>
Date:   Fri Feb 26 15:38:16 2016 +0800

    Modify gitcmd to test.

接下來,切回到master去
並將billiam327-master的內容給合過來

git checkout master
git branch
git merge --no-ff billiam327-master

nelsoncgtekiMBP:sync_utility nelsonchung$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
nelsoncgtekiMBP:sync_utility nelsonchung$ git branch
  billiam327-master
*
master
nelsoncgtekiMBP:sync_utility nelsonchung$ git merge --no-ff billiam327-master
Merge made by the 'recursive' strategy.
 compress.sh | 22
++++++++++++++++++++++
 1 file changed, 22 insertions(+)

最後可以利用git push將合入的內容提交到github囉!
這封郵件來自 Evernote。Evernote 是您專屬的工作空間,免費下載 Evernote

透過Facebook分享

沒有留言: