发布时间:2025-06-24 17:51:54 作者:北方职教升学中心 阅读量:651
****[需要这份系统化资料的朋友,可以点击这里获取](https://bbs.csdn.net/topics/618540462)****一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、面试辅导),让我们一起学习成长!**
发布时间:2025-06-24 17:51:54 作者:北方职教升学中心 阅读量:651
****[需要这份系统化资料的朋友,可以点击这里获取](https://bbs.csdn.net/topics/618540462)****一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、面试辅导),让我们一起学习成长!**
如果存在以上两种行为进行切换分支,则切换分支时不仅会对HEAD指针、222
另外还需要注意的是,Git与SVN的代码冲突逻辑不一致。
==在非同轴开发中,后面的版本(v3)需要合并前面的版本(v2),这种合并方式称为典型合并;==由于典型合并存在于非同轴
需要注意的是,如果是同轴开发,后面版本的内容肯定包含前面版本的内容,因此==同轴开发不存在典型合并==,只存在快进合并。
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git checkout testSwitched to branch 'test'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* adaec78 (master) master合并test分支|\| * 7b14536 (HEAD -> test) bbb* | eeca4c9 aaa|/* 5f41035 111 222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ cat aaa.txt111222bbbAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git merge masterUpdating 7b14536..adaec78Fast-forward aaa.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ cat aaa.txt111aaa222bbbAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* adaec78 (HEAD -> test, master) master合并test分支|\| * 7b14536 () bbb* | eeca4c9 aaa|/* 5f41035 111 222
典型合并时的代码冲突就是我们之前遇到的那种情况,典型合并属于不同轴的开发,并且后面版本需要合并前面版本的情况,由于不同轴,因此后面版本的内容不一定包含前面版本的内容,由于Git代码冲突的特点,因此典型合并必定会出现代码冲突,因为后面版本的内容不可能和前面版本内容一致。
快进合并属于前面的版本合并后面的版本,需要注意的是==前面的版本并非是"旧版本",后面的版本并非是"新版本"==,这主要取决于是否是同轴开发。另外,非同轴开发也存在快进合并,例如上图中的③步骤中,前面的版本(v2)想要合并后面的版本(v3),这也是一种快进合并。职场吐槽、
【使用Git演示代码冲突】
①初始化项目:
rm -rf .git ./*git initAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat aaa.txt111222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git add ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git commit -m "111 222" ./
②创建test分支
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git branch testAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* 45fa06b (HEAD -> master, test) 111 222
③继续使用master分支开发
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git checkout masterAlready on 'master'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat aaa.txt111aaa222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git commit -m "aaa" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* c86283b (HEAD -> master) aaa* 45fa06b (test) 111 222
④切换到test分支,继续开发
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git checkout testSwitched to branch 'test'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ cat aaa.txt111222bbbAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git commit -m "bbb" ./[test e61df5a] bbb 1 file changed, 1 insertion(+), 1 deletion(-)Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git log --oneline --all --graph* e61df5a (HEAD -> test) bbb| * c86283b (master) aaa|/* 45fa06b 111 222
⑤使用test分支合并master分支(属于典型合并)
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git merge masterAuto-merging aaa.txtCONFLICT (content): Merge conflict in aaa.txtAutomatic merge failed; fix conflicts and then commit the result.Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING)$ cat aaa.txt<<<<<<< HEAD111222bbb=======111aaa222>>>>>>> masterAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING)$ cat aaa.txt111aaa222bbbAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test|MERGING)$ git commit -a -m "合并master分支,并解决冲突"[test 448d619] 合并master分支,并解决冲突Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git log --oneline --all --graph* 448d619 (HEAD -> test) 合并master分支,并解决冲突|\| * c86283b (master) aaa* | e61df5a bbb|/* 45fa06b 111 222
同样的,如果此时切换回master分支,合并test分支虽然属于快进合并并且不同轴,但合并依旧不会出现冲突,因为test分支的内容是解决冲突并且经过用户确认之后的内容。
git merge {branchName}
同轴开发:创建一个新分支,使用新分支开发,等待开发功能趋于成熟稳定后,我们可以将master分支与新分支进行合并,这样其他分支的功能就集成到主分支上了,这也是企业里面经常用的一种开发方式;
对于前面的版本想要合并后面的版本,我们称为快进合并
在同轴开发中,多个分支处于同一根开发轴上,后面版本的代码包含前面版本的代码,因此同轴开发一般只会存在快进合并;
【示例代码】
初始化项目:
rm -rf .git ./*git initecho '用户名+密码登录' >> login.txtgit add ./git commit -m '用户名+密码登录功能完成' ./
①创建login分支
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git branch loginAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all2f5d87a (HEAD -> master, login) 用户名+密码登录功能完成
②切换到login分支
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git checkout loginSwitched to branch 'login'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ git log --oneline --all2f5d87a (HEAD -> login, master) 用户名+密码登录功能完成
③使用login分支开发:集成QQ登录
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ echo "集成QQ登录" >> login.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ git commit -m "集成QQ登录" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ git log --oneline --alla879a6c (HEAD -> login) 集成QQ登录2f5d87a (master) 用户名+密码登录功能完成
④使用login分支开发:集成微信登录
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ echo "集成微信登录" >> login.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ git commit -m "集成微信登录" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ git log --oneline --allc5a2584 (HEAD -> login) 集成微信登录a879a6c 集成QQ登录2f5d87a (master) 用户名+密码登录功能完成
当login分支基于稳定后,将功能集成到maste分支:
⑤切换回master分支,将login分支的代码合并到master分支:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (login)$ git checkout masterSwitched to branch 'master'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat login.txt用户名+密码登录Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git merge loginUpdating 2f5d87a..c5a2584Fast-forward login.txt | 2 ++ 1 file changed, 2 insertions(+)Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat login.txt用户名+密码登录集成QQ登录集成微信登录
将login分支的代码合并到master分支,其实本质上就是让master也指向与login一样的提交对象;
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # master与login分支指向的是同一个Commit对象$ cat .git/refs/heads/loginc5a258428f0bec398017159126f0b87b0d05a1e4Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat .git/refs/heads/masterc5a258428f0bec398017159126f0b87b0d05a1e4
非同轴开发:创建一个新分支,使用新分支开发,与此同时master分支也在开发行功能,等到新分支开发的功能完毕后,将master分支与新分支进行合并,将新分支集成到master分支中,但是新分支在开发过程中master分支也进行了一部分开发。学习资源、
总结:
Tips:典型合并必定是非同轴,快进合并可以是非同轴也可以是同轴。
使用分支开发功能从角度上划分一般为同轴开发和非同轴开发。大厂内推、
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git log --oneline --all --graph* 448d619 (HEAD -> test) 合并master分支,并解决冲突### 给大家的福利**零基础入门**对于从来没有接触过网络安全的同学,我们帮你准备了详细的学习成长路线图。对于同轴开发,后面的版本自然是新版本,其内容肯定包含前面版本的内容。新文件的操作都会影响工作空间和暂存区;
【演示新分支影响工作空间】
初始化仓库
rm -rf ./* .gitgit initecho '111' >> aaa.txtgit add ./git commit -m '111' ./git branch b1 Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # b1分支创建后整个工作空间还没有提交过任何一次文件,属于新分支$ git log --oneline --all4b3c6bc (HEAD -> master, b1) 111 # 指针还是指向master分支的
在master分支编辑文件,然后切换到b1分支,查看工作空间的内容,发现变更了(影响了b1分支的工作空间内容):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 编辑master分支的文件内容$ echo "222" >> aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # master分支的状态$ git statusOn branch masterChanges not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: aaa.txtno changes added to commit (use "git add" and/or "git commit -a")Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支,发现能够切换成功$ git checkout b1Switched to branch 'b1'M aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1分支的文件内容$ cat aaa.txt111222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1分支的状态$ git statusOn branch b1Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: aaa.txtno changes added to commit (use "git add" and/or "git commit -a")
可以看到在遇到新分支时,当前工作空间的操作即使未提交也可以切换到其他分支,而且影响其他分支的工作空间状态及工作空间内容;注意,此时将master未提交的数据也影响到了b1分支,当分支切换到master然后提交,b1分支被影响的内容就消失了;
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # 切换到master分支$ git checkout masterSwitched to branch 'master'M aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 提交操作$ git commit -m "222" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支$ git checkout b1Switched to branch 'b1'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # 查看内容,发现影响消失了$ cat aaa.txt111
重新切换到master分支,编辑文件未提交,切换到b1分支失败(此时b1分支不属于新分支了):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git checkout masterSwitched to branch 'master'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "333" >> aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支失败,此时b1分支不属于新分支了;$ git checkout b1error: Your local changes to the following files would be overwritten by checkout: aaa.txtPlease commit your changes or stash them before you switch branches.Aborting
【演示新文件影响工作空间】
把之前编辑的内容删除,将工作空间恢复到干净状态
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 把之前编辑的内容删除$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ cat aaa.txt111222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 工作空间又回到了干净状态(所有的操作均已提交)$ git statusOn branch masternothing to commit, working tree clean
使用master创建一个新的文件,切换到b1分支,查看b1分支的工作空间内容,发现也多了一个bbb.txt文件(影响了b1分钟的工作空间):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 编辑一个新的文件$ echo "333" >> bbb.txt # 注意,此时创建了一个新的文件bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git statusOn branch masterUntracked files: (use "git add <file>..." to include in what will be committed) bbb.txtnothing added to commit but untracked files present (use "git add" to track)Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支,发现切换成功$ git checkout b1Switched to branch 'b1'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ lltotal 2-rw-r--r-- 1 Adminstrator 197121 5 Oct 23 09:23 aaa.txt-rw-r--r-- 1 Adminstrator 197121 4 Oct 23 09:23 bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1的工作空间多了一个文件$ git statusOn branch b1Untracked files: (use "git add <file>..." to include in what will be committed) bbb.txtnothing added to commit but untracked files present (use "git add" to track)
切换到master分支,提交操作,然后切换回b1分支,发现b1分支的bbb.txt文件清除了:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # 切换回master分支$ git checkout masterSwitched to branch 'master'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git add ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 提交操作$ git commit -m "333-bbb" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 切换到b1分支$ git checkout b1Switched to branch 'b1'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git statusOn branch b1nothing to commit, working tree cleanAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1工作空间的bbb.txt文件清除了$ lltotal 1-rw-r--r-- 1 Adminstrator 197121 5 Oct 23 09:26 aaa.txt
2) 影响暂存区
【演示新分支影响暂存区】
初始化仓库:
rm -rf ./* .gitgit initecho '111' >> aaa.txtgit add ./git commit -m '111' ./git branch b1 Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # b1分支创建后整个工作空间还没有提交过任何一次文件,属于新分支$ git log --oneline --all19fd84b (HEAD -> master, b1) 111 # 指针还是指向master分支的
在master分支编辑文件,然后添加到暂存区,切换到b1分支,查看b1分支的暂存区内容,发现变更了(影响了b1分支的暂存区);
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "222" >> aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git add ./warning: LF will be replaced by CRLF in aaa.txt.The file will have its original line endings in your working directoryAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout b1Switched to branch 'b1'M aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git ls-files -s100644 a30a52a3be2c12cbc448a5c9be960577d13f4755 0 aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git cat-file -p a30a52a3111222
重新切换回master,提交之前的操作,然后再切换回b1分支,查看暂存区内容,发现影响消除了(此时b1不再属于新分支了):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git checkout masterSwitched to branch 'master'M aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git commit -m "222" ./warning: LF will be replaced by CRLF in aaa.txt.The file will have its original line endings in your working directory[master 2ae7cda] 222 1 file changed, 1 insertion(+)Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout b1Switched to branch 'b1'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git ls-files -s100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git cat-file -p 58c9bdf9111
切换回master分支,编辑文件,还没有提交,切换到b1分钟发现出错(此时b1分支不属于新分支了):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "333" >> aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git add ./warning: LF will be replaced by CRLF in aaa.txt.The file will have its original line endings in your working directoryAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout b1error: Your local changes to the following files would be overwritten by checkout: aaa.txtPlease commit your changes or stash them before you switch branches.Aborting
【演示新文件影响工作空间】
提交之前的操作,将工作空间恢复到干净状态
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git commit -m "333" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git log --oneline --allf5d54ea (HEAD -> master) 3332ae7cda 222e9b98e3 (b1) 111
创建一个新文件,添加到暂存区,然后切换到b1分支,发现b1分支的暂存区也多了一个文件(影响了b1分支的暂存区):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "444-bbb" >> bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git add ./warning: LF will be replaced by CRLF in bbb.txt.The file will have its original line endings in your working directoryAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout b1Switched to branch 'b1'A bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1) # b1分支的暂存区也多了一个文件$ git ls-files -s100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt100644 a864cef2a0696aabcc85e6e55f04dc12230bab84 0 bbb.txt
切换回master,提交之前的操作,然后再切换回b1分支,查询b1分支的暂存区内容被消除了(消除影响):
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git checkout masterSwitched to branch 'master'A bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git commit -m "444-bbb" ./warning: LF will be replaced by CRLF in bbb.txt.The file will have its original line endings in your working directory[master d76cc6f] 444-bbb 1 file changed, 1 insertion(+) create mode 100644 bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout b1Switched to branch 'b1'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git ls-files -s100644 58c9bdf9d017fcd178dc8c073cbfcbb7ff240d6c 0 aaa.txt
此时bbb.txt文件已经不属于新文件了,回到master分支,编辑bbb.txt文件,未提交,然后切换到b1分支,发现切换不了:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (b1)$ git checkout masterSwitched to branch 'master'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "555-bbb" >> bbb.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git add ./warning: LF will be replaced by CRLF in bbb.txt.The file will have its original line endings in your working directoryAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout b1error: Your local changes to the following files would be overwritten by checkout: bbb.txtPlease commit your changes or stash them before you switch branches.Aborting
1.3.3 switch切换分支
git switch
是Git 2.23版本推出的一个新的命令,专门用于分支的切换,而git checkout
除了做分支的切换还有一些其他的功能,如恢复文件,恢复暂存区等;初始化仓库:
rm -rf ./* .gitgit initecho '111' >> aaa.txtgit add ./git commit -m '111' ./
使用git checkout恢复文件:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "222" >> aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git statusOn branch masterChanges not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: aaa.txtno changes added to commit (use "git add" and/or "git commit -a")Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout aaa.txtUpdated 1 path from the indexAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git statusOn branch masternothing to commit, working tree clean
使用git checkout恢复暂存区:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ echo "222" >> aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 添加到暂存区$ git add ./warning: LF will be replaced by CRLF in aaa.txt.The file will have its original line endings in your working directoryAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git statusOn branch masterChanges to be committed: (use "git restore --staged <file>..." to unstage) modified: aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git reset aaa.txtUnstaged changes after reset:M aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master)$ git checkout aaa.txtUpdated 1 path from the indexAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace/xiaohui/test01 (master) # 成功恢复$ git status On branch masternothing to commit, working tree clean
但
git switch
命令是专门用于切换分支的,并且git switch
切换分支也会存在我们探究的新分支、
【SVN代码冲突模拟】
SVN的情况:两个用户编辑的代码是同一行则出现冲突
xiaohui | xiaolan |
---|---|
创建abc.txt | |
内容为: | |
111 | |
222 | |
执行add | |
执行commit | |
执行update | |
修改内容为: | |
111aaa | |
222 | |
执行commit | |
修改内容为: | |
111bbb | |
222 | |
执行update(冲突) |
如果"xiaolan"用户最后更改的文件内容为:
111222bbb
然后再执行update,则不会出现代码冲突,因为SVN判断代码冲突的代码单元为行,即:两个用户编辑的不是同一行就不会出现代码冲突。工作空间等地方修改,还会对如下地方进行修改:
Tips:我们在切换分支时,要保证当前分支是提交的状态,否则会对其他分支造成不必要的影响;
新分支、不过具体情况还得细分如下:
Tips:我们本章主要讨论分支合并时的代码冲突。但对于非同轴开发,后面的版本内容大概率是不包含前面的版本内容的;
在同轴开发情况下,快进合并不会产生代码冲突,但如果在非同轴开发情况下,快进合并就会产生代码冲突了。
我们观察下列操作:
master分支 | test分支 |
---|---|
创建abc.txt | |
内容为: | |
111 | |
222 | |
执行add | |
执行commit | |
创建分支 – 此时test分支的abc.txt的内容也是111、
| |
修改内容为: | |
111aaa | |
222 | |
执行commit | |
切换到test分支 | |
修改内容为: | |
111 | |
222bbb | |
执行commit – 此时test分支和master分支已经不同轴了 | |
切换回master分支,合并test分支,属于快进合并,但会出现冲突 |
Tips:注意,上述操作在SVN中则不会认为是冲突,因为SVN判断是否冲突的标准是操作同一个文件的同一行记录;
【使用Git演示代码冲突】
①初始化项目
rm -rf .git ./*git initAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat aaa.txt111222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git add ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git commit -m "111 222" ./
②创建test分支
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git branch test
③继续使用master分支开发
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* d40d605 (HEAD -> master, test) 111 222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ cat aaa.txt111aaa222Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git commit -m "aaa" ./Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* 09fe9cb (HEAD -> master) aaa* d40d605 (test) 111 222
④切换到test分支开发
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git checkout testSwitched to branch 'test'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) # 注意,和master分支修改的文件内容不是同一行$ cat aaa.txt111222bbbAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test)$ git commit -m "bbb" ./[test dfe1b42] bbb 1 file changed, 1 insertion(+), 1 deletion(-)Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) # 查看日志,test与master分支已经不同轴$ git log --oneline --all --graph * dfe1b42 (HEAD -> test) bbb| * 09fe9cb (master) aaa|/* d40d605 111 222
⑤切换回master分支,合并test分支,出现冲突:
Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (test) # 切换回master分支$ git checkout masterSwitched to branch 'master'Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master) # 合并test分支,属于快进合并,但是会产生冲突$ git merge testAuto-merging aaa.txtCONFLICT (content): Merge conflict in aaa.txtAutomatic merge failed; fix conflicts and then commit the result.Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING)$ cat aaa.txt<<<<<<< HEAD111aaa222=======111222bbb>>>>>>> testAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING)$ vi aaa.txtAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING)$ cat aaa.txt111aaa222bbbAdminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master|MERGING) # 解决冲突$ git commit -a -m "master合并test分支"[master adaec78] master合并test分支Adminstrator@LAPTOP-OC90J78H MINGW64 ~/Desktop/workspace (master)$ git log --oneline --all --graph* adaec78 (HEAD -> master) master合并test分支|\| * 7b14536 (test) bbb* | eeca4c9 aaa|/* 5f41035 111 222
需要注意的是:此时切换回test分支,合并master分支内容不会出现冲突(属于快进合并,但是内容不一致,但不会出现冲突):因为master分支的内容是刚刚解决了冲突之后用户确定了的内容。我们观察下列操作:
master分支 | test分支 |
---|---|
创建abc.txt | |
内容为: | |
111 | |
222 | |
执行add | |
执行commit | |
创建分支 – 此时test分支的abc.txt的内容也是111、同时每个成长路线对应的板块都有配套的视频提供:因篇幅有限,仅展示部分资料**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。 【在当前HEAD指针指向的位置创建分支】
查看 【继续使用master分支开发】
1.2.2 指定提交对象创建分支【指定提交对象来创建分支】
1.3 切换分支1.3.1 checkout分支切换创建好了分支之后,我们可以使用
【准备环境】
【创建分支】
【切换到b1分支进行开发】
【查看所有版本情况】 默认情况下使用
1.3.2 checkout原理通常情况下,当前分支如果存在未提交的操作时,则无法切换到其他分支;所以我们切换分支时最好保证当前工作空间的状态为 在切换分支时,Git会对以下地方进行修改:
【代码示例】
【操作master分支的文件但不提交,切换到b1分支发现切换失败】
可以看出,如果当前分支存在未提交的操作时,Git不允许切换分支。 【示例代码】 初始化项目:
①创建login分支:
②切换到login分支,使用login分支进行开发:
③切换回master分支添加注册功能:
④将login分支的合并到master分支:产生代码冲突
解决代码冲突:
经过上一次合并后,master与login属于同轴了,当切换为login分支,可以使用快进合并来合并master分支;
1.5 Git的代码冲突1.5.1 Git的代码冲突的分类与特点Git存在代码冲突的情况分为协同开发时的代码冲突与分支合并时的代码冲突;其中分支合并时的代码冲突又分为快进合并代码冲突与典型合并代码冲突。可以说是最科学最系统的学习路线,大家跟着这个大的方向学习准没问题。
上一篇:卡牌游戏:年度最佳榜单公布
|