美文网首页
Pro Git 学习笔记 (七, 分支创建合并)

Pro Git 学习笔记 (七, 分支创建合并)

作者: 冯斯特罗 | 来源:发表于2017-03-09 09:56 被阅读49次

    新建分支

    basic-branching-1.png
    git checkout -b iss53 # Switched to a new branch "iss53"
    

    它是下面两条命令的简写:

    git branch iss53
    git checkout iss53
    
    basic-branching-2.png
    $ vim index.html
    $ git commit -a -m 'added a new footer [issue 53]'
    
    basic-branching-3.png
    $ git checkout master
    Switched to branch 'master'
    
    $ git checkout -b hotfix
    Switched to a new branch 'hotfix'
    $ vim index.html
    $ git commit -a -m 'fixed the broken email address'
    [hotfix 1fb7853] fixed the broken email address
     1 file changed, 2 insertions(+)
    
    basic-branching-4.png
    $ git checkout master
    $ git merge hotfix
    Updating f42c576..3a0874c
    Fast-forward
     index.html | 2 ++
     1 file changed, 2 insertions(+)
    

    在合并的时候,你应该注意到了"快进(fast-forward)"这个词。 由于当前 master 分支所指向的提交是你当前提交(有关 hotfix 的提交)的直接上游,所以 Git 只是简单的将指针向前移动。 换句话说,当你试图合并两个分支时,如果顺着一个分支走下去能够到达另一个分支,那么 Git 在合并两者的时候,只会简单的将指针向前推进(指针右移),因为这种情况下的合并操作没有需要解决的分歧——这就叫做 “快进(fast-forward)”


    basic-branching-5.png
    $ git branch -d hotfix
    Deleted branch hotfix (3a0874c).
    
    $ git checkout iss53
    Switched to branch "iss53"
    $ vim index.html
    $ git commit -a -m 'finished the new footer [issue 53]'
    [iss53 ad82d7a] finished the new footer [issue 53]
    1 file changed, 1 insertion(+)
    
    basic-branching-6.png

    你在hotfix 分支上所做的工作并没有包含到 iss53 分支中。
    如果你需要拉取 hotfix 所做的修改,你可以使用 git merge master 命令将 master 分支合并入 iss53 分支
    或者你也可以等到 iss53 分支完成其使命,再将其合并回 master 分支

    分支的合并

    $ git checkout master
    Switched to branch 'master'
    $ git merge iss53
    Merge made by the 'recursive' strategy.
    index.html |    1 +
    1 file changed, 1 insertion(+)
    
    basic-merging-2.png

    遇到冲突时的分支合并

    遇到冲突时的分支合并

    遇到冲突时的分支合并

    $ git merge iss53
    Auto-merging index.html
    CONFLICT (content): Merge conflict in index.html
    Automatic merge failed; fix conflicts and then commit the result.
    

    没有冲突的,会提交,有冲突的,保留在工作区,手动解决冲突.

    $ git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:      index.html
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    如果你想使用图形化工具来解决冲突,你可以运行 git mergetool,该命令会为你启动一个合适的可视化合并工具,并带领你一步一步解决这些冲突: (都有哪些工具辅助冲突解决?)
    冲突解决之后:

    git commit # 或 git commit -a
    
    Merge branch 'iss53'
    
    Conflicts:
        index.html
    #
    # It looks like you may be committing a merge.
    # If this is not correct, please remove the file
    #   .git/MERGE_HEAD
    # and try again.
    
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch master
    # All conflicts fixed but you are still merging.
    #
    # Changes to be committed:
    #   modified:   index.html
    #
    

    也就是说,如果没有冲突,git会帮助你生成一个git commit.如果有冲突,需要手动解决冲突,并提交.

    相关文章

      网友评论

          本文标题:Pro Git 学习笔记 (七, 分支创建合并)

          本文链接:https://www.haomeiwen.com/subject/kcwbgttx.html