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. 合并分支(普通合并)
    分支合并分为快速合并与普通合并两种模式,普通合并,合并后的历史有分支记录,能看出来曾经做过合并,而快速合并就看不出来曾经做过合并。下面我们来演示一下普通合并:
    ##新创建分支grand
    git checkout -b grand
    ##在grand分支上创建文件
    echo "this is a test file" > readme.txt
    ##git操作
    git add readme.txt
    git commit -m "add merge a line"
    ##切换到master分支
    git checkout master
    ##普通合并
    git merge --no-ff -m "merge with no-ff" grand
    说明:--no-ff参数表示禁用快速合并
    ##查看合并历史
    git log --graph --pretty=oneline --abbrev-commit
    \*   96a332a (HEAD -> master) merge with no-ff
    |\
    | * 347b403 (grand) add merge a line
    |/
    说明:合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支记录,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。
  5. 快速合并与普通合并的比较
    (1)快速合并
    Git 分支合并冲突及合并分类
    (2)普通合并
    Git 分支合并冲突及合并分类