1.不同分支下多人协作
- ⼀般情况下,如果有多需求需要多⼈同时进⾏开发,是不会在⼀个分⽀上进⾏多⼈开发,⽽是⼀个需求或⼀个功能点就要创建⼀个
feature
分⽀ - 情景设置:
- 目标:远程
main
分支下新增func1
和func2
文件 - 实现:开发者A新增
func1
,开发者B新增func2
- 条件:不同分支下协作完成,各自让某一个功能私有某一个分支
- 目标:远程
- 开发者A:在自己分支上,正常推送,没有问题
$ vim func1 $ cat func1 fun1 from A $ git add . $ git commit -m "A push func1" [feature-1 84e77e0] A push func1 1 file changed, 1 insertion(+) create mode 100644 func1 $ git push origin feature-1 Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 20 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 318 bytes | 159.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:DieSnowK/Git-Learn.git 10cd204..84e77e0 feature-1 -> feature-1
- 开发者B:在自己分支上,正常推送,没有问题
$ cat func2 fun2 fron B $ git add . $ git commit -m "B push func2" [feature-2 31c8cbb] B push func2 1 file changed, 1 insertion(+) create mode 100644 func2 $ git push origin feature-2 Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 20 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 343 bytes | 343.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:DieSnowK/Git-Learn.git 10cd204..31c8cbb feature-2 -> feature-2 PS C:\Users\w1752\Desktop\My_Repository\Git-Learn>
- 至此,开发者A和开发者B都互相看不到对方开发的文档,并且在自己的分支上各自推送时,没有任何冲突,两人互不影响,开发起来很丝滑
- 假如此时开发者B无法继续完成文档开发,需要开发者A接手,该怎么办?
- 开发者B将自己的分支名告诉开发者A,开发者A从远端仓库将该分支拉下来
$ git branch * feature-1 main $ git pull remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), 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), 323 bytes | 323.00 KiB/s, done. From github.com:DieSnowK/Git-Learn * [new branch] feature-2 -> origin/feature-2 There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> feature-1
- 开发者A本地创建一个
feature-2
分支,并且和远端的feature-2
分支关联起来$ git checkout -b feature-2 origin/feature-2 Branch 'feature-2' set up to track remote branch 'feature-2' from 'origin'. Switched to a new branch 'feature-2' $ cat func2 fun2 from B $ vim func2 # Coding... $ cat func2 fun2 fron B fun2 Done from A $ git add . $ git commit -m "A edit func2 Done" [feature-2 2e279ac] A edit func2 Done 1 file changed, 2 insertions(+), 1 deletion(-) $ git push origin feature-2 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), 280 bytes | 280.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To github.com:DieSnowK/Git-Learn.git 31c8cbb..2e279ac feature-2 -> feature-2
- 开发者B将自己的分支名告诉开发者A,开发者A从远端仓库将该分支拉下来
- 两人开发全部完毕后,都需要将各自的代码合并到
main
中才算真正的开发完毕,流程如下-
开发者A:
-
切换至
main
,git pull
,保证本地main
是最新内容 -
切换至
feature-1
分支,合并main
分支,有冲突就在feature-1
分支解决 -
切换至
main
分支,合并feature-1
分支 -
将
main
分支推送至远端
$ git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. $ git pull Already up to date. $ git checkout feature-1 Switched to branch 'feature-1' $ git merge main Already up to date. $ git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. $ git merge feature-1 Updating 10cd204..84e77e0 Fast-forward func1 | 1 + 1 file changed, 1 insertion(+) create mode 100644 func1 $ cat func1 fun1 from A $ git push Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:DieSnowK/Git-Learn.git 10cd204..84e77e0 main -> main
-
-
开发者B:
-
切换至
main
,git pull
,保证本地main
是最新内容 -
切换至
feature-2
分支,合并main
分支,有冲突就在feature-2
分支解决 -
由于
feature-1
分支已经merge
进来了新内容,为了保证feature-2
远程分支最新,最好在此时git push
一下git push
的另一个原因是在实际的开发中,main
的merge
操作⼀般不是由我们⾃⼰在本地进进行,而是由审查人员进行- 其他⼈员或某些平台
merge
时,操作的肯定是远程分⽀,所以就要保证远程分⽀的最新 - 如果此时
merge
发生冲突,解决完冲突后需要commit
之后才能push
-
切换至
main
分支,合并feature-2
分支 -
将
main
分支推送至远端
$ git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. $ git pull Already up to date. $ git checkout feature-2 Switched to branch 'feature-2' Your branch is up to date with 'origin/feature-2'. $ git merge main Merge made by the 'ort' strategy. func1 | 1 + 1 file changed, 1 insertion(+) create mode 100644 func1 $ ls SnowK.txt func1 func2 $ git status On branch feature-2 Your branch is ahead of 'origin/feature-2' by 2 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean $ git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 20 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 292 bytes | 292.00 KiB/s, done. Total 2 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To github.com:DieSnowK/Git-Learn.git 2e279ac..2250eeb feature-2 -> feature-2 $ git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. $ git merge feature-2 Updating 84e77e0..2250eeb Fast-forward func2 | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 func2 $ ls SnowK.txt func1 func2 $ git push Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 To github.com:DieSnowK/Git-Learn.git 84e77e0..2250eeb main -> main
-
-
- 至此,无论本地还是远端的
feature-1
和feature-2
分支都没用了,可以直接删掉了
2.远程分⽀删除后,本地git branch -a
依然能看到
- 问题描述:明明已经删除了远程的⼏个分⽀,但使⽤
git branch -a
发现很多在远程仓库已经删除的分⽀在本地依然可以看到 - 问题追溯:
git remote show origin
$ git remote show origin * remote origin Fetch URL: git@github.com:DieSnowK/Git-Learn.git Push URL: git@github.com:DieSnowK/Git-Learn.git HEAD branch: main Remote branches: main tracked refs/remotes/origin/dev stale (use 'git remote prune' to remove) refs/remotes/origin/test stale (use 'git remote prune' to remove) Local branch configured for 'git pull': main merges with remote main Local ref configured for 'git push': main pushes to main (up to date)
- 解决方案:使用
git remote prune origin
命令$ git remote prune origin Pruning origin URL: git@github.com:DieSnowK/Git-Learn.git * [pruned] origin/dev * [pruned] origin/test