0


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

目录


0.命令补充

  • git branch只能查看本地分支,如果要查看远程分支需要加上-r选项,即git branch -r- 如果本地是的分支情况是落后与远端的,需要先git pull一下才能看到远端分支$ git branch -r origin/dev origin/main $ git pullFrom github.com:DieSnowK/Git-Learn * [new branch] test -> origin/testAlready 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:由于先提交,没有遇到问题$ cat SnowK.txtDie$ git add .$ git commit -m "A push sth"[dev de4ac94] A push sth 1 file changed, 1 insertion(+) $ git pushEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 20 threadsCompressing 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 0To github.com:DieSnowK/Git-Learn.git 6981c9d..de4ac94 dev -> dev
  • 开发者B:提交时遇到问题,被远端仓库拒绝提交 - 原因:开发者A最新提交的代码和开发者B此时推送的提交有冲突$ cat SnowK.txtSnowK$ git add .$ git commit -m "B push sth"[dev 074d5ec] B push sth 1 file changed, 1 insertion(+) $ git pushTo 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 dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (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拉取下来- 然后,在本地进行合并,并解决冲突,再重新提交并推送$ git pullremote: 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 0Unpacking objects: 100% (3/3), 261 bytes | 16.00 KiB/s, done.From github.com:DieSnowK/Git-Learn 6981c9d..de4ac94 dev -> origin/devAuto-merging SnowK.txtCONFLICT (content): Merge conflict in SnowK.txtAutomatic merge failed; fix conflicts and then commit the result.# 解决冲突ing$ cat SnowK.txtDieSnowK$ git add .$ git commit -m "solve the conflict, merge from origin/dev"[dev 10cd204] solve the conflict, merge from origin/dev$ git pushEnumerating objects: 10, done.Counting objects: 100% (10/10), done.Delta compression using up to 20 threadsCompressing 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 0To github.com:DieSnowK/Git-Learn.git de4ac94..10cd204 dev -> dev
  • 此时,远端仓库的dev分支,已经是最新版本,但是要如何将该dev分支合并至main分支呢?- 方法一:在远端仓库提出Pull Request(PR),交由审查员审核后合并- 方法二:以下为一个习惯良好的流程,避免出现问题 - 切换至本地main分支,git pull拉取下最新的main分支$ git checkout mainSwitched to branch 'main'Your branch is up to date with 'origin/main'.$ git pullAlready up to date.- 本地切换至dev分支,合并main分支 - 目的:如果有冲突,可以在dev分支解决,以避免对main分支造成影响$ git checkout devSwitched to branch 'dev'Your branch is up to date with 'origin/dev'.$ git merge mainAlready up to date.- 切换至本地main分支,合并dev分支,并将main分支推送至远端$ git checkout mainSwitched to branch 'main'Your branch is up to date with 'origin/main'.$ git merge devUpdating 6241400..10cd204Fast-forward SnowK.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 SnowK.txt $ cat SnowK.txtDieSnowK$ git statusOn branch mainYour 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 mainTotal 0 (delta 0), reused 0 (delta 0), pack-reused 0To github.com:DieSnowK/Git-Learn.git 6241400..10cd204 main -> main $ git statusOn branch mainYour 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,最后删除分⽀

本文转载自: https://blog.csdn.net/qq_37281656/article/details/140923683
版权归原作者 DieSnowK 所有, 如有侵权,请联系我们删除。

“[Git][多人协作][上]详细讲解”的评论:

还没有评论