• Git 分支管理


    Git 分支:

    (1) 分支( branches ) 是指在开发主线中分离出来的一条支线,在支线上做进一步开发而不影响到原来主线,就好比游戏中的主线任务、分支任务
    (2) Git 中的 master 分支,和其他分支一样,通常我们会把这个分支作为主分支,这个分支是在 git init 命令运行时默认创建的一个分支,是默认的一个分支
    (3) 平常我们开发的时候不要在 master 分支上进行开发,创建别的分支进行开发,等到要发布到线上的时候再全部合并到 master 分支

    git branch                    # 查看分支
    git branch <branch_name>      # 创建分支
    git checkout <branch_name>    # 切换分支
    git merge <branch_name>       # 合并分支
    git branch -d <branch_name>   # 删除分支


    如何把其他分支的代码合并到主分支:

    [root@localhost ~]$ cd /data/git
    [root@localhost git]$ git branch dev      # 我们先创建一个分支
    [root@localhost git]$ git checkout dev    # 然后切换到这个分支
    [root@localhost git]$ touch 1.txt         # 提交文件到这个分支
    [root@localhost git]$ git add 1.txt
    [root@localhost git]$ git commit -m 'add new file' 1.txt
    [root@localhost git]$ git checkout master    # 切换到主分支
    [root@localhost git]$ git merge dev          # 把dev分支的代码合并到主分支


    分支合并时冲突:

    如下,当 master 和 dev 两个分支都对 1.txt 进行了修改,合并分支时就会造成冲突

    [root@localhost git]$ git checkout dev      
    [root@localhost git]$ echo "aaa" >> 1.txt
    [root@localhost git]$ git commit -m 'add some character to file' 1.txt
    [root@localhost git]$ git checkout master   
    [root@localhost git]$ echo "bbb" >> 1.txt
    [root@localhost git]$ git commit -m 'add some character to file' 1.txt
    [root@localhost git]$ git merge dev  
    Auto-merging 1.txt
    CONFLICT (content): Merge conflict in 1.txt
    Automatic merge failed; fix conflicts and then commit the result.

    解决分支冲突的方法:

    在 master 分支下,把文件修改成 dev 分支里的内容,然后再合并 dev 分支。但是有一个问题,如果 master 分支下的修改才是我们想要的呢?我们可以先编辑 1.txt ,改成想要的,然后提交,再切换到 dev 分支,把 master 分支合并到 dev 分支 ( 倒着合并 ),合并分支有一个原则,那就是要把最新的分支合并到旧的分支,也就是说 git merge <branch_name> 中 <branch_name> 必须是内容最新的分支。

    [root@localhost git]$ cat 1.txt    # 查看文件内容,会自动显示冲突的内容
    <<<<<<< HEAD                       # 如下,表示bbb是master分支产生的变更,aaa是dev分支产生的变更
    bbb                                
    =======
    aaa
    >>>>>>> dev    
    [root@localhost git]$ cat 1.txt    # 如果dev分支下是我想要的,那么我们可以先改成同dev分支里的内容一样,然后提交文件,最后再合并分支
    aaa
    [root@localhost git]$ git add 1.txt
    [root@localhost git]$ git commit -m 'resolve confict' 1.txt
    [root@localhost git]$ git merge dev
    [root@localhost git]$ cat 1.txt    # 如果master分支下是我想要的,那么我们可以先改成我想要的内容,然后提交
    aaa                                # 然后切换到dev分支,把master分支合并到dev分支即可
    bbb
    [root@localhost git]$ git add 1.txt
    [root@localhost git]$ git commit -m 'resolve confict'   # 注意这里需要提交全部,不能只提交一个文件
    [root@localhost git]$ git checkout dev
    [root@localhost git]$ git merge master


    远程分支管理:

    git branch                          # 查看本地分支
    git ls-remote origin                # 查看远程分支
    git push origin <new_branch_name>   # 把本地新建的分支推送到远程

    当我们使用 git push 的时候,有两种情况:

    (1) 当本地分支和远程分支一致时,git push 会把所有本地分支的变更一同推送到远程
    (2) 当本地分支比远程多时,默认只推送本地和远程一致的分支

    当我们使用 git clone 的时候,默认只克隆 master 分支下的代码到本地,如果我们想克隆其他分支,操作如下:

    [root@localhost git]$ git clone https://github.com/pzk7788/studygit.git    # 先克隆远程的master分支下来
    [root@localhost git]$ cd studygit/                                         # 进入克隆的分支代码目录
    [root@localhost studygit]$ git checkout -b dev2 origin/dev2                # 克隆远程的新分支,第一个dev2是本地新分支,第二个dev2是远程的分支
    # 如果本地存在dev2分支,会自动关联远程分支,如果不存在则会自动在本地创建该分支

         

       

  • 相关阅读:
    从构建分布式秒杀系统聊聊限流特技
    轻快的VIM(三):删除
    shell中各种括号的作用()、(())、[]、[[]]、{}
    java的重写规则
    UNIX命令,统计当前目录(含子目录)下所有后缀为.log的文件中ERROR出现的行数
    linux下使用 du查看某个文件或目录占用磁盘空间的大小
    linux如何查看系统占用磁盘空间最大的文件及让文件按大小排序
    管道命令和xargs的区别(经典解释)
    JAVA 一个或多个空格分割字符串
    shell替换一个或多个空格为逗号
  • 原文地址:https://www.cnblogs.com/pzk7788/p/10291906.html
Copyright © 2020-2023  润新知