• git分支概念与项目中的应用


    文档:https://git-scm.com/book/zh/v2/Git-分支-分支简介

    分支理解

    • master分支是项目在创建时候的默认分支,除此之外,它并没有更多的含义。

    • 剩下的 “开发分支”,“灰度分支”, “预发布分支”, “需求分支”,“测试分支” 都是根据项目和需求约定的。它们本质上只是一个分支而已。

    分支在项目中的应用

    1、首先,我们创建了一个项目:

    http://10.2.16.183/zhiheng/myproject

    这是我局域网搭建的gitlab,我们就以这个项目为例。

    2、项目的基本流程:

    • 克隆项目到本地
    > git clone http://10.2.16.183/zhiheng/myproject
    
    Cloning into 'myproject'...
    warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    
    切换到项目:
    > cd  myproject
    
    • 查看当前状态
    > git status
    
    On branch master
    Your branch is up to date with 'origin/master'.
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
    	.idea/
    	a.py
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    • 提交代码
    > git add a.py
    > git commit -m "first file"
    > git push origin master
    

    3、分支的使用

    为什么要使用分支?

    1、你在开发项目里面一个很大的模块,这个模块需要连续开发一个月,你可以选择一个月提交一次,但一个月的开发代码都存在你的本地电脑是很危险的。万一电脑丢失,代码被误删,损失很大!

    2、你们团队的项目有十几个人在维护,每天会有N多次的提交,一旦你拉取和提交的间隙,被别人提交了代码,当你在提交的时候别人就需要解决冲突。每次解决和提交冲突是很浪费时间的。

    分支的使用

    • 查看所有分支(远程分支和本地分支)
    > git branch -a
    
    * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/master
    
    • 查看本地分支
    > git branch -a
    * master
    
    • 创建分支
    > git branch dev
    
    • 切换分支
    > git checkout dev
    

    当你当前分支有未提交的文件时,不允许你提交分支。

    • 在 dev 分支上面操作

    创建 dev_a.py 文件

    dev> git status
    
    On branch dev
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
    	.idea/
    	dev_a.py
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    dev> git add dev_a.py
    
    dev> git commit -m "dev a file"
    [dev ace2539] dev a file
     1 file changed, 1 insertion(+)
     create mode 100644 dev_a.py
    
    • 目前虽然本地多了一个 dev 分支, 但远程是没有的。
    dev> git branch -a
    
    * dev
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/master
    
    • 提交到远程分支。
    git push origin dev
    warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
    Counting objects: 3, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    remote:
    remote: To create a merge request for dev, visit:
    remote:   http://10.2.16.183/zhiheng/myproject/merge_requests/new?merge_request%5Bsource_branch%5D=dev
    remote:
    To http://10.2.16.183/zhiheng/myproject
     * [new branch]      dev -> dev
    
    • 再次查看所有分支, 远程分支也多了一个dev
    dev> git branch -a
    * dev
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/dev
      remotes/origin/master
    
    • 不同分支下面,文件数量不一样。
    ## dev 分支
    dev> ls
    README.md    a.py      dev_a.py
    
    ## master分支
    master> ls
    README.md    a.py
    

    4、代码的冲突与解决。

    假设A 和 B 在一个分支上开发

    1、A 拉取 common.py 文件,修改。
    2、B 拉取 common.py 文件,修改。
    3、B 提交了 common.py 文件的修改。
    4、A 在提交 common.py 文件时就会遇到冲突, A 应该怎么做?

    • 拉取远程代码
    git pull origin master
    warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
    remote: Enumerating objects: 5, done.
    remote: Counting objects: 100% (5/5), done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 3 (delta 1), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From http://10.2.16.183/zhiheng/myproject
     * branch            master     -> FETCH_HEAD
       ed59b2e..c166f93  master     -> origin/master
    Updating ed59b2e..c166f93
    error: Your local changes to the following files would be overwritten by merge:
    	common.py
    Please commit your changes or stash them before you merge.
    Aborting
    

    这个时候发现代码被 B 修改了,因为我本地也做了更新,所以不允许拉取。

    • 先提交提交代码,再拉取。
    > git add common.py
    > git commit -m "A 也修改了文件"
    > git pull origin master
    warning: redirecting to http://10.2.16.183/zhiheng/myproject.git/
    From http://10.2.16.183/zhiheng/myproject
     * branch            master     -> FETCH_HEAD
    Auto-merging common.py
    CONFLICT (content): Merge conflict in common.py
    Automatic merge failed; fix conflicts and then commit the result.
    
    • 解决冲突
    vim common.py
    
    # 这是一个公共文件
    
    def common(a, b):
    <<<<<<< HEAD
        c = a + b
        return c
    =======
        return a + b
    >>>>>>> c166f934de72779168cd00ef40bb96ff65e09ab5
    

    开发的过程尽量避免多人改一个方法,像这样的冲突就比较解决了。 A和B需要坐到一起,这个冲突解决。

    • 重新提交冲突
    > git add common.py
    > git commit -m "合并冲突"
    [master 485cfaa] 合并冲突
    > git push origin master
    

    5、分支的合并。

    如果多个开发同时在一个分支上开发,上面的冲突每天要发生很多次。这将严重影响开发效率。 每个开发都在自己的分支上面开发。

    • A开发在 dev 分支。
    git branch
      master
    * dev
    
    test> ls
    README.md  a.py  dev1.py   dev2.py   dev_a.py
    
    • B开发在 test 分支。
    git branch
      master
    * test
    
    test> ls
    a.py  common.py  README.md  test1.py  test2.py
    

    此时,两个分支的上的代码出现了较大的不同。

    testdev合并到master

    1、 在A电脑上有本地只有 master 和 dev ,可以直接合并。

    > git merge dev
    Merge made by the 'recursive' strategy.
     dev_a.py | 1 +
     1 file changed, 1 insertion(+)
     create mode 100644 dev_a.py
    

    2、B电脑本地只有 master 和 test 分支。

    • B电脑:先把 test 分支推送
    git add .
    > git commit -m "test分支代码"
    [test 14032ea] test分支代码
     2 files changed, 2 insertions(+)
     create mode 100644 test1.py
     create mode 100644 test2.py
    > git push origin test
    
    • A电脑:本地创建 test 分支,拉取远程 test 分支的代码
    > git branch test
    > git checkout test
    Switched to branch 'test'
    > git pull
    
    • A电脑:回到 master 分支,合并 test 分支。
    > git merge test
    Updating 34fa4b3..7677952
    Fast-forward
     test1.py | 1 +
     test2.py | 1 +
     2 files changed, 2 insertions(+)
     create mode 100644 test1.py
     create mode 100644 test2.py
    
    > ls
    README.md a.py common.py dev1.py   dev2.py   dev_a.py  test1.py  test2.py
    

    master 分支就拥有了所有分支的代码。 在这个过程中,

    1、在代码分支合并的过程中如果出现冲突,就要解决冲突。
    2、我们在分支开发的过程中,也可以时长拉取master分支的内容,一般合并到master的代码都是相对完整的。这样可以避免master合并的时候出现过多的冲突。

  • 相关阅读:
    ES6
    JavaScript小练习2
    JavaScript实现多重继承
    一个定高,一个高度自适应的布局
    实例教程:1小时学会Python(转)
    备份文件的python脚本(转)
    Python2.5/2.6实用教程:基础篇(转)
    Python 读写 Excel(转)
    python实用技巧 : Filtering os.walk(转)
    Python:文件操作技巧(File operation)(转)
  • 原文地址:https://www.cnblogs.com/fnng/p/11664093.html
Copyright © 2020-2023  润新知