1. 程式人生 > >Git 分支合並沖突及合並分類

Git 分支合並沖突及合並分類

git git分支合並 git分支合並沖突 git分支合並種類 git快速合並與普通合並

Git 分支合並沖突及合並分類

  1. 分支合並沖突
    ##創建分支datagrand
    git checkout -b datagrand
    Switched to a new branch ‘datagrand‘
    git branch -a
    master
    \* datagrand
    ##在datagrand分支上創建文件
    echo "this is git file!" > read.txt
    git add read.txt
    git commit -m "add a new file"
    git push -u origin datagrand
    ##切換到master分支,建立read.txt文件
    echo "my name is wtf" > read.txt
    git add read.txt
    git commit -m "add a new file"
    git push 
    ##合並分支
    ##在master分支上操作
    git merge datagrand
    ##說明:這種情況下,Git無法執行“快速合並”,只能試圖把各自的修改合並起來,但這種合並就可能會有沖突,報錯如下:
    Auto-merging read.txt
    CONFLICT (add/add): Merge conflict in read.txt
    Automatic merge failed; fix conflicts and then commit the result.
    ##說明:Git告訴我們read.txt文件存在沖突,必須手動解決沖突後再提交。
  2. 解決合並沖突
    ##查看下當前分支狀態
    git status
    On branch master
    Your branch is up-to-date with ‘origin/master‘.
    You have unmerged paths.
    (fix conflicts and run "git commit")
    (use "git merge --abort" to abort the merge)
    Unmerged paths:
    (use "git add <file>..." to mark resolution)
    both added:      read.txt
    no changes added to commit (use "git add" and/or "git commit -a")
    ##查看一下readme.txt中的內容
    cat read.txt
    <<<<<<< HEAD
    my name is wtf!
    =======
    this is git file!
    \>>>>>>> datagrand
    ##說明:Git用<<<<<<<,=======,>>>>>>>標記出不同分支的內容,讓我們選擇要保留的內容,下面我們修改一下readme.txt,再次提交。
    ##操作步驟如下:
    vim read.txt
    git add read.txt
    git commit -m "fixed"
    [master 935e613] fixed
    git status
    On branch master
    Your branch is ahead of ‘origin/master‘ by 2 commits.
    (use "git push" to publish your local commits)
    nothing to commit, working tree clean
    cat read.txt
    this is git file!
    ##分支合並
    git merge datagrand
    ##查看合並後狀態
    git status
    On branch master
    Your branch is ahead of ‘origin/master‘ by 2 commits.
    (use "git push" to publish your local commits)
    nothing to commit, working tree clean
  3. 刪除分支
    git branch -d datagrand
    Deleted branch datagrand (was 3299654).
    ##查看一下分支合並信息
    git log --graph --pretty=oneline --abbrev-commit 
    \*   935e613 (HEAD -> master) fixed
    || * 3299654 (origin/datagrand) add a new file
    \* | d4f781c (origin/master) add a file read
    \* | ddb3e06 Delete read.txt
    \* | 13bfb1c add a file read
    |/
  4. 合並分支(普通合並)
    分支合並分為快速合並與普通合並兩種模式,普通合並,合並後的歷史有分支記錄,能看出來曾經做過合並,而快速合並就看不出來曾經做過合並。下面我們來演示一下普通合並:
    (未完待續)
    。。。。。。。。。。。。。。。。。。。。。

Git 分支合並沖突及合並分類