1. test分支合並到master,合並分支代碼
a. 創建分支
git branch test
b. 切換分支
git checkout test
c. 需求:把test分支合並到master合並代碼步驟
1). 修改test分支代碼
2).提交test分支代碼到test分支
git add . git commit -m "" git push origin test
3).切換到需要合並代碼的分支
當前分支為test,切換到master
git checkout master
4).master分支需要pull一下
有時候如果改了有時候會報錯
git checkout . 這樣就全恢復回來了
5).合並代碼
備注:現在在分支master上
git merge test 這個時候會出現代碼沖突,強制合並為test分支的代碼 git status 查看狀態,以及出現沖突的文件 git checkout test <沖突的wenj> git checkout test 1.txt 強制合並,把沖突的test代碼覆蓋到master上來
6).master提交代碼
git add .
git commit
git push origin master
7). 切換到test分支(保持一個好習慣pull一下)
git pull origin test
# git pull origin master, 其實這里要注意一下,一般test代碼為最新的,master代碼都是生產環境代碼比較穩定,根據現實情況是否需要在test分支pull master代碼
2. 協同開發,合並代碼,解決沖突
需求:
甲在test分支開發,已經提交了一次,而乙還沒有pull test分支,並且繼續開發,怎么合並代碼並提交代碼
a. 甲開發代碼並提交到test分支
git add . git commit -m "" git push origin test
2. 乙開發並沒有更新pull甲的代碼
乙相對於甲還在上一個版本並開發中
3. 乙push提交代碼到test分支,報錯
doman@LAPTOP-BGQ47KT1 MINGW64 ~/Desktop/test_git_file/two_people_dev_file/test_git (test) $ git add . doman@LAPTOP-BGQ47KT1 MINGW64 ~/Desktop/test_git_file/two_people_dev_file/test_git (test) $ git commit -m "乙修改了代碼" [test 310cb0c] 涔欎慨鏀逛簡浠g爜 1 file changed, 2 insertions(+)
doman@LAPTOP-BGQ47KT1 MINGW64 ~/Desktop/test_git_file/two_people_dev_file/test_git (test) $ git push origin test ====================================================================================================================================== 報錯啦 To https://github.com/renfanzi/test_git.git ! [rejected] test -> test (fetch first) error: failed to push some refs to 'https://github.com/renfanzi/test_git.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.
4. 乙需要git pull origin test
doman@LAPTOP-BGQ47KT1 MINGW64 ~/Desktop/test_git_file/two_people_dev_file/test_git (test) $ git pull origin test remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), 283 bytes | 21.00 KiB/s, done. From https://github.com/renfanzi/test_git * branch test -> FETCH_HEAD c6e0160..3d92ce9 test -> origin/test Auto-merging 1.txt CONFLICT (content): Merge conflict in 1.txt ----------------------------------》看這里,合並沖突到1.txt conflict 沖突的意思 Automatic merge failed; fix conflicts and then commit the result. ----------------》自動合並失敗,修改沖突並提交
5. 乙需要git status查看狀態,有沖突代碼
doman@LAPTOP-BGQ47KT1 MINGW64 ~/Desktop/test_git_file/two_people_dev_file/test_git (test|MERGING) $ git status On branch test Your branch and 'origin/test' have diverged, and have 1 and 3 different commits each, respectively. (use "git pull" to merge the remote branch into yours) You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: 1.txt --------------------》 看這里 no changes added to commit (use "git add" and/or "git commit -a")
6. 手動修改解決沖突
doman@LAPTOP-BGQ47KT1 MINGW64 ~/Desktop/test_git_file/two_people_dev_file/test_git (test|MERGING) $ cat 1.txt 123456 test_master 修改了test分支代碼 <<<<<<< HEAD ----------------》 刪代碼吧,手動,其實可以ctrl + f搜一下 乙修改了代碼 ======= master已經合並test代碼 甲修改了代碼 >>>>>>> 3d92ce95a546197f614f9a76c7ccb3ae6987c24f
7. 乙再次提交
git add . git commit -m "" git push origin test
8. 再次合並到master,按照上面的步驟
3. 切換分支,並保存修改的代碼
需求:
當我在test開發的時候,這個時候需要在master上做出一些修改;然后再切換回test分支繼續完成工作,怎么辦?
git commit
步驟:
1. test上工作
git add . git commit -m ""
2. 切換到master分支,修改代碼
git checkout master 修改代碼 git add . git commit -m ""
3. 切換到test分支繼續工作