【Git】4. Git分支操作

4. Git分支操作

4.1 什么是分支

  1. 在版本控制过程中,同时推进多个任务,为每个任务,我们就可以创建每个任务的单独分支。使用分支意味着程序员可以把自己的工作从开发主线上分离开来, 开发自己分支的时候,不会影响主线分支的运行。对于初学者而言,分支可以简单理解为副本,一个分支就是一个单独的副本。(分支底层其实也是指针的引用)

  2. 图解:

在这里插入图片描述

4.2 分支的好处

  1. 同时并行推进多个功能开发,提高开发效率
  2. 各个分支在开发过程中,如果某一个分支开发失败,不会对其他分支有任何影响。失败的分支删除重新开始即可

4.3 分支的操作

命令名称作用
git branch 分支名创建分支
git branch -v查看分支
git checkout 分支名切换分支
git merge 分支名把指定的分支合并到当前分支上
  1. 查看分支

    1. 基本语法:git branch -v

    2. 实例操作:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git branch -v
      * master f5dc083 first commit
      
  2. 创建分支

    1. 基本语法:git branch 分支名

    2. 实例操作:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git branch hot-fix
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git branch -v
        hot-fix f5dc083 first commit //新分支
      * master  f5dc083 first commit
      
  3. 切换分支

    1. 基本语法:git checkout 分支名

    2. 实例操作:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git checkout hot-fix
      Switched to branch 'hot-fix'
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
      
      
    3. 图解:

      1. master、 hot-fix 其实都是指向具体版本记录的指针,所以创建分支的本质就是多创建一个指针

      2. 当前所在的分支,其实是由HEAD决定的

        1. HEAD 如果指向 master,那么我们现在就在 master 分支上
        2. HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上
        3. 所以切换分支的本质就是移动 HEAD 指针

        在这里插入图片描述

  4. 修改 hot-fix 分支

    //在hot-fix分支上做修改,并添加到暂存区,并提交到本地库
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ vim hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git add hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git commit -m "hot-fix first commit" hello.txt
    [hot-fix 8753437] hot-fix first commit
     1 file changed, 2 insertions(+), 2 deletions(-)
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git status
    On branch hot-fix
    nothing to commit, working tree clean
        
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ cat hello.txt
    hello git 1111111
    hello git 2222222
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    
  5. 查看当前日志

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git reflog
       //当前指针指向了hot-fix
    8753437 (HEAD -> hot-fix) HEAD@{0}: commit: hot-fix first commit
    f5dc083 (master) HEAD@{1}: checkout: moving from master to hot-fix
    f5dc083 (master) HEAD@{2}: reset: moving to f5dc083
    8c7d395 HEAD@{3}: reset: moving to HEAD
    8c7d395 HEAD@{4}: commit: first commit
    f5dc083 (master) HEAD@{5}: commit (initial): first commit
    
    
  6. 回到master分支,查看文件

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git checkout master
    Switched to branch 'master'
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ cat hello.txt
    hello git	//未合并前,文件内容仍是之前的
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    
  7. 合并分支

    1. 基本语法:git merge 分支名

    2. 实例操作:

      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ git merge hot-fix
      Updating f5dc083..8753437
      Fast-forward
       hello.txt | 4 ++--
       1 file changed, 2 insertions(+), 2 deletions(-)
      
      lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
      $ cat hello.txt
      hello git 1111111		//	master分支上的文件内容已经修改
      hello git 2222222
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      

4.4 分支冲突问题

  1. 冲突产生的原因:合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。 Git 无法替我们决定使用哪一个。必须人为决定新代码内容

  2. 在master分支上修改文件, 添加到暂存区,并提交到本地库

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ vim hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
            modified:   hello.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git add hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git commit -m "master test" hello.txt
    [master f8b3939] master test
     1 file changed, 1 insertion(+), 1 deletion(-)
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git status
    On branch master
    nothing to commit, working tree clean
    
  3. 切换到hot-fix 分支上,对文件修改

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git checkout hot-fix
    Switched to branch 'hot-fix'
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ vim hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ vim hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git add hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git commit -m "hot-fix test" hello.txt
    [hot-fix 184ab7a] hot-fix test
     1 file changed, 2 insertions(+), 1 deletion(-)
    
  4. 回到master分支进行合并

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (hot-fix)
    $ git checkout master
    Switched to branch 'master'
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ git merge hot-fix
    Auto-merging hello.txt
    CONFLICT (content): Merge conflict in hello.txt
    Automatic merge failed; fix conflicts and then commit the result.
    //Automatic merge failed自动合并失败
    //两个分支在同一个文件的同一个位置有两套完全不同的修改,不知道选哪个
    
  5. 手动合并:

    1. 此时文件内容

      hello git 1111111
      hello git 2222222
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      <<<<<<< HEAD
      hello git master
      hello git
      =======
      hello git
      hello git hot-fix
      
      >>>>>>> hot-fix
      
    2. 手动合并

      hello git 1111111
      hello git 2222222
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git
      hello git master
      hello git hot-fix
      
  6. 查看此时的状态

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING)
    $ git status
    On branch 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 modified:   hello.txt 	//有文件未被检测
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
  7. 将手动合并的文件添加、提交

    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING)
    $ git add hello.txt
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING)
    $ git commit -m "merge test" hello.txt
    fatal: cannot do a partial commit during a merge.
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master|MERGING)
    $ git commit -m "merge test"
    [master 694ba18] merge test
    
    lenovo@DESKTOP-55E400K MINGW64 /e/Git-space/project_1 (master)
    $ cat hello.txt
    hello git 1111111
    hello git 2222222
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git
    hello git master  //此时文件已经解决冲突,完成合并
    hello git hot-fix
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值