美文网首页
Git 分支相关操作

Git 分支相关操作

作者: AndyGF | 来源:发表于2020-07-14 00:27 被阅读0次

    本文介绍的所有分支操作都是在本地 Git 仓库。

    一、新建

    新建一个分支:

    git branch <branch-name>
    

    基于某个分支新建一个分支并切换到新分支上

    git checkout -b <branch-name>  <base-branch-name>
    // 基于主分支创建 newbranch 分支并切换到 newbranch 分支上。
    git checkout -b newbranch master
    // 相当于执行两个命令
    git branch newbranch master
    git checkout newbranch
    

    二、查看

    查看当前所有分支:

    git branch
    

    查看当前所有分支,并显示对应分支最后一次 commit id 和 commit message

    git branch -v
    

    三、切换分支

    切换到其他分支

    git checkout  <other-branch-name>
    

    四、合并分支

    例如把分支 hello 上的代码合并到主分支 master 上

    // 切回主分支
    git checkout master
    // 合并 hello 到主分支
    git merge hello
    

    合并成功后,不同的情况,提示不同。有可能同时会进入编辑界面,要求输入合并的 message 作为一个 commit 提交。

    如果提示合并失败,大部分是冲突,也叫分歧。此时需要手动合并冲突,完成后最好提交一个 commit。

    五、删除分支

    例如把 hello 这个分支合并到主分支以后,没有用了,需要删除

    git branch -d hello
    

    如果 hello 这个分支还没有合并, -d 会删除失败,此时如果确定放弃此分支,可以用 -D 这个参数

    // 强制删除,
    git branch -D hello
    

    相关文章

      网友评论

          本文标题:Git 分支相关操作

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