0.命令补充
git branch
只能查看本地分支,如果要查看远程分支需要加上-r
选项,即git branch -r
- 如果本地是的分支情况是落后与远端的,需要先
git pull
一下才能看到远端分支$ 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
文件新增代码Die
和SnowK
- 实现:开发者A新增
Die
,开发者B新增SnowK
- 条件:在一个分支下协作完成
- 目标:
- 开发者A:由于先提交,没有遇到问题
$ 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此时推送的提交有冲突
$ 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
拉取下来 - 然后,在本地进行合并,并解决冲突,再重新提交并推送
$ 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
- 开发者B先
- 此时,远端仓库的
dev
分支,已经是最新版本,但是要如何将该dev
分支合并至main
分支呢?- 方法一:在远端仓库提出
Pull Request(PR)
,交由审查员审核后合并 - 方法二:以下为一个习惯良好的流程,避免出现问题
- 切换至本地
main
分支,git pull
拉取下最新的main
分支$ 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
分支造成影响
$ 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
分支推送至远端$ 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
推送就能成功 - 功能开发完毕,将分⽀
merge
进main
,最后删除分⽀
- ⾸先,可以试图⽤