本系列为个人学习Git参照廖雪峰老师的笔记
- 本文内容:Git远程分支管理
参考笔记:
https://www.liaoxuefeng.com/wiki/896043488029600
文章目录
3 Git分支管理
3-1 创建与合并分支
概念:
- 当我们创建新的分支,例如
dev
时,Git新建了一个指针叫dev
,指向master
相同的提交,再把HEAD
指向dev
,就表示当前分支在dev
上 - 就是直接把
master
指向dev
的当前提交,就完成了合并
查看分支
git branch – 查看分支
新建分支
git branch dev —新建分支dev
切换分支
git checkout dev —切换到dev分支
git switch dev
创建并切换
git checkout -b dev --创建并切换到新的分支
git switch -c dev
合并分支
git merge 合并某个分支到当前分支
删除分支
git branch -d 删除分支
Practice
新建分支并修改文件
➜ learngit git:(master) git branch dev
➜ learngit git:(master) git checkout dev
切换到分支 'dev'
➜ learngit git:(dev) ls
laoye_lu.txt teacher_ma.txt
➜ learngit git:(dev) vim laoye_lu.txt
➜ learngit git:(dev) ✗ cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
gai ya %%%%%
➜ learngit git:(dev) ✗ git add laoye_lu.txt
➜ learngit git:(dev) ✗ git commit -m "add gaiya"
[dev 06ed295] add gaiya
1 file changed, 1 insertion(+)
切换分支并合并
➜ learngit git:(dev) git switch master
切换到分支 'master'
➜ learngit git:(master) ls
laoye_lu.txt teacher_ma.txt
➜ learngit git:(master) cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
➜ learngit git:(master) git merge dev
更新 333b891..06ed295
Fast-forward
laoye_lu.txt | 1 +
1 file changed, 1 insertion(+)
➜ learngit git:(master) cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
gai ya %%%%%
ps: 无法删除当前所在的分支
➜ learngit git:(dev) git branch -d dev
error: 无法删除检出于 '/home/rei/文档/rhw/learngit' 的分支 'dev'。
➜ learngit git:(dev) git branch
➜ learngit git:(dev) git switch master
切换到分支 'master'
➜ learngit git:(master) git branch -d dev
已删除分支 dev(曾为 333b891)。
3-2 解决冲突
菱形冲突
合并变成了
3-3 分支管理策略
通常,合并分支时,如果可能,Git会用Fast forward
模式,但这种模式下,删除分支后,会丢掉分支信息。
如果要强制禁用Fast forward
模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息。
下面我们实战一下--no-ff
方式的git merge
:
合并:
git merge --no-ff -m "merge with no-ff" dev
Practice
➜ learngit git:(master) git branch tmp
➜ learngit git:(master) git switch tmp
切换到分支 'tmp'
➜ learngit git:(tmp) ls
laoye_lu.txt teacher_ma.txt
➜ learngit git:(tmp) vim laoye_lu.txt
➜ learngit git:(tmp) ✗ cat laoye_lu.txt
laoye lu is disapper ...
laoye lu is so cai !
game is fun \/
➜ learngit git:(tmp) ✗ git add laoye_lu.txt
➜ learngit git:(tmp) ✗ git commit -m "add game is fun"
[tmp e211d49] add game is fun
1 file changed, 1 insertion(+), 1 deletion(-)
➜ learngit git:(tmp) git switch master
切换到分支 'master'
➜ learngit git:(master) git merge --no-ff -m "merge with no-ff" tmp
Merge made by the 'recursive' strategy.
laoye_lu.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
➜ learngit git:(master) git log --graph --pretty=oneline --abbrev-commit
* 5010425 (HEAD -> master) merge with no-ff
|\
| * e211d49 (tmp) add game is fun
|/
* 06ed295 (dev) add gaiya
* 333b891 remove test.txt
* 0028773 create new file test.txt
* f764581 新增 laoye lu is disapper
* b1a98b3 teacher ma is shutdown
* 176fa53 teacher ma is unstoppable
* 357ea31 新增laoye_lu.txt
* 2871442 gank middle road
* ce1ee22 teacher ma is back to home
* f2a1a21 teacher Ma's lesson
3-4 Bug分支
git stash 存储 解决紧急bug
// 存储你当前的分支状态
git stash
用于复制一个特定的提交
git cherry-pick 4c805e2
3-5 Feature合并
用户强行删除合并前包含的分支
是这个包含机密资料的分支还是必须就地销毁:
git branch -D feature-vulcan
3-6 多人协作
你的小伙伴要在dev
分支上开发,就必须创建远程origin
的dev
分支到本地,于是他用这个命令创建本地dev
分支
git checkout -b dev origin/dev
指定本地dev
分支与远程origin/dev
分支的链接,根据提示,设置dev
和origin/dev
的链接
git branch --set-upstream-to=origin/dev dev
Branch ‘dev’ set up to track remote branch ‘dev’ from ‘origin’.
然后再进行代码拉取
git pull
3-7 Rebase
将错乱的提交记录整理成直线,其实也就是以主线为主,删除不必要的合并
合并后