再学一遍git使用方法,git把分支代码合并到master主分支

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分支继续工作

 

 

 

 

posted @ 2020-09-26 19:30  我当道士那儿些年  阅读(2543)  评论(0编辑  收藏  举报