[Git] Git 自學筆記 : 合併分支(merge)

  • 1831
  • 0
  • Git
  • 2016-09-23

git的主要核心就在於分支及合併
官方有一個簡單的 建立分支 及合併的練習
https://guides.github.com/activities/hello-world/


1.自動合併
2.衝突合併

主要git指令: git checkout -b
                     git pull
                     git push
                     git merge

 

  • 自動合併
    建立分支
  1. git checkout -b branchfeature
    ....switched to a new branch "branchfeature"
  2. branch異動檔案;單純新增檔案,或是於既有檔案後面增加內
    git add newfile.txt
    git commit -m "add new file"
  3. 選擇pull request (master<--->branchname)
    git checkout master
  4. 系統提示:
    Able to merge. These branches can be automatically merged.
    This branch has no conflicts with the base branch
  5. Create a Merged Commit 
     git merge branchfeature
     git pull (如果需要)
     git push (push回remote respo) 
     
  • 衝突合併

    git merge 時 "自動合併"很聰明地解決了大部分的問題
    甚至連同一份檔案 被修改都可能自動合併 (。:.゚ヽ(*´∀`)ノ゚.:。 灑花!!!~~~)
    但仍有可能發生修改的 部分 重疊 造成git無法自動合併

    1.發生衝突 時,會出現以下警告
    e.g
    git merge branchfeature 
    Auto-merging /source/file.txt
    CONFLICT (content): Merge conflict in /source/file.txt
    Automatic merge failed; fix conflicts and then commit the result.


    2.衝突的檔案會被修改如下
    new file content header
    <<<<<<< HEAD
    new file master
    ..
    =======
    new file feature 
    ..
    >>>>>>> feature
    new file footer

    3.手動解決衝突
    修改成你要的版本後,並去除 標記(<<<<<<< HEAD, =======,>>>>>>> feature)
    e.g 
    new file content header
    new file feature merge to master
    new file footer

    4.再度提交commit
    git add .
    git commit -m "merge master + feature"

    5.再merge看看 ,直到沒有衝突
     
  • 若有衝突,但確認要以本地repo為主,把遠端直接覆蓋      
    git push  --force