[Git][多人协作][上]详细讲解

目录


0.命令补充

  • git branch只能查看本地分支,如果要查看远程分支需要加上-r选项,即git branch -r

    • 如果本地是的分支情况是落后与远端的,需要先git pull一下才能看到远端分支

      plaintext 复制代码
      $ git branch -r
        origin/dev
        origin/main
        
      $ git pull
      From github.com:DieSnowK/Git-Learn
       * [new branch]      test       -> origin/test
      Already up to date.
      
      $ git branch -r
        origin/dev
        origin/main
        origin/test
  • 查看本地和远程仓库的所有分支git branch -a

  • 查看本地分支和远端分支的联系情况git branch -vv

  • 查看远程仓库详细信息git remote show origin

  • 本地如何获取到远端已有的新分支?

    • 在本地git pull之后,本地是没有dev分支的,需要在本地新建dev分支,并与远端的origin/dev分支建立联系
    • 方法一 :本地新建分支时直接建立联系
      • git checkout -b branch_name origin/branch_name
    • 方法二 :本地已经建立分支了,后续再单独建立联系
      • git branch --set-upstream-to=origin/branch_name branch_name
  • 如果要在远端仓库上新建分支,该如何做?

    • 方法一 :直接在远端仓库新建分支(推荐 )
      • 这样能保证新建的分支绝对是以main为基准,且是最新的
    • 方法二 :本地创建分支,再push
      • 此时会有些许麻烦 --> 本地的main是最新的吗?
      • 为了解决上述问题,通常需要在新建本地分支前,在本地main分支git pull一下
      • 然后创建本地分支,并直接推送至远端git push origin branch_name

1.同一分支下多人协作

  • 情景设置

    • 目标master分支下,SnowK.txt文件新增代码DieSnowK
    • 实现 :开发者A新增Die,开发者B新增SnowK
    • 条件:在一个分支下协作完成
  • 开发者A :由于先提交,没有遇到问题

    plaintext 复制代码
    $ cat SnowK.txt
    Die
    
    $ git add .
    
    $ git commit -m "A push sth"
    [dev de4ac94] A push sth
     1 file changed, 1 insertion(+)
     
    $ git push
    Enumerating objects: 5, done.
    Counting objects: 100% (5/5), done.
    Delta compression using up to 20 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done.
    Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    To github.com:DieSnowK/Git-Learn.git
       6981c9d..de4ac94  dev -> dev
  • 开发者B :提交时遇到问题,被远端仓库拒绝提交

    • 原因:开发者A最新提交的代码和开发者B此时推送的提交有冲突
    plaintext 复制代码
    $ cat SnowK.txt
    SnowK
    
    $ git add .
    
    $ git commit -m "B push sth"
    [dev 074d5ec] B push sth
     1 file changed, 1 insertion(+)
     
    $ git push
    To github.com:DieSnowK/Git-Learn.git
     ! [rejected]        dev -> dev (fetch first)
    error: failed to push some refs to 'github.com:DieSnowK/Git-Learn.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  • 如何解决上述问题?

    • 开发者B先git pull将远端代码从origin/dev拉取下来

    • 然后,在本地进行合并,并解决冲突,再重新提交并推送

      plaintext 复制代码
      $ git pull
      remote: Enumerating objects: 5, done.
      remote: Counting objects: 100% (5/5), done.
      remote: Compressing objects: 100% (2/2), done.
      remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
      Unpacking objects: 100% (3/3), 261 bytes | 16.00 KiB/s, done.
      From github.com:DieSnowK/Git-Learn
         6981c9d..de4ac94  dev        -> origin/dev
      Auto-merging SnowK.txt
      CONFLICT (content): Merge conflict in SnowK.txt
      Automatic merge failed; fix conflicts and then commit the result.
      
      # 解决冲突ing
      
      $ cat SnowK.txt
      Die
      SnowK
      
      $ git add .
      
      $ git commit -m "solve the conflict, merge from origin/dev"
      [dev 10cd204] solve the conflict, merge from origin/dev
      
      $ git push
      Enumerating objects: 10, done.
      Counting objects: 100% (10/10), done.
      Delta compression using up to 20 threads
      Compressing objects: 100% (4/4), done.
      Writing objects: 100% (6/6), 636 bytes | 636.00 KiB/s, done.
      Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
      To github.com:DieSnowK/Git-Learn.git
         de4ac94..10cd204  dev -> dev
  • 此时,远端仓库的dev分支,已经是最新版本,但是要如何将该dev分支合并至main分支呢?

    • 方法一 :在远端仓库提出Pull Request(PR),交由审查员审核后合并
    • 方法二 :以下为一个习惯良好的流程,避免出现问题
      • 切换至本地main分支,git pull拉取下最新的main分支

        plaintext 复制代码
        $ git checkout main
        Switched to branch 'main'
        Your branch is up to date with 'origin/main'.
        
        $ git pull
        Already up to date.
      • 本地切换至dev分支,合并main分支

        • 目的 :如果有冲突,可以在dev分支解决,以避免对main分支造成影响
        plaintext 复制代码
        $ git checkout dev
        Switched to branch 'dev'
        Your branch is up to date with 'origin/dev'.
        
        $ git merge main
        Already up to date.
      • 切换至本地main分支,合并dev分支,并将main分支推送至远端

        plaintext 复制代码
        $ git checkout main
        Switched to branch 'main'
        Your branch is up to date with 'origin/main'.
        
        $ git merge dev
        Updating 6241400..10cd204
        Fast-forward
         SnowK.txt | 3 +++
         1 file changed, 3 insertions(+)
         create mode 100644 SnowK.txt
         
        $ cat SnowK.txt
        Die
        SnowK
        
        $ git status
        On branch main
        Your branch is ahead of 'origin/main' by 5 commits.
          (use "git push" to publish your local commits)
        
        nothing to commit, working tree clean
        
        $ git push origin main
        Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
        To github.com:DieSnowK/Git-Learn.git
           6241400..10cd204  main -> main
           
        $ git status
        On branch main
        Your branch is up to date with 'origin/main'.
        
        nothing to commit, working tree clean
      • dev分支已经完成它的任务,可以删除远端和本地的dev分支了

  • 总结:在同⼀分⽀下进⾏多⼈协作的⼯作模式

    • ⾸先,可以试图⽤git push origin branch-name推送⾃⼰的修改
    • 如果推送失败,则因为远程分⽀⽐本地更新,需要先⽤git pull试图合并
    • 如果合并有冲突,则解决冲突,并在本地提交
    • 没有冲突或者解决掉冲突后,再⽤git push origin branch-name推送就能成功
    • 功能开发完毕,将分⽀mergemain,最后删除分⽀
相关推荐
晓理紫6 小时前
使用git lfs向huggingface提交较大的数据或者权重
git
我不是程序猿儿8 小时前
【GIT】sourceTree的“当前分支“,“合并分支“与“检出分支的区别
git
_OLi_15 小时前
IDEA中新建与切换Git分支
java·spring boot·git
PyAIGCMaster19 小时前
ubuntu下安装 git 及部署cosyvoice(1)
git
维__kxs766719 小时前
小程序 + AI 自动直播:一部手机开启抖音挂载小程序流量主变现之旅
git·eclipse
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ1 天前
GIT GUI和 GIT bash区别
开发语言·git·bash
zhuyan1081 天前
【git】使用记录
git
KrisZhang101 天前
Git分支
git·1024程序员节
孤影&碧空1 天前
书生大模型第三关Git 基础知识
git
code .1 天前
git使用的一般流程
git