【Git】之分支合并命令

一、Git分支管理

1、查看Git分支

git branch


按Q键返回。

2、创建分支以及删除分支

git branch newtest

git branch -d newtest

3、切换分支

git checkout newtest

4、合并分支

① 主干上创建文件

zhangc@test:~/Desktop/GitTest/gitDemo|master 
⇒  touch text.txt
zhangc@test:~/Desktop/GitTest/gitDemo|master⚡ 
⇒  git add .
zhangc@test:~/Desktop/GitTest/gitDemo|master⚡ 
⇒  git commit -m "add"
[master 57ccd61] add
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 text.txt
zhangc@test:~/Desktop/GitTest/gitDemo|master 
⇒  ll
total 0
-rw-r--r--  1 zhangc  staff     0B Mar  9 17:14 README
-rw-r--r--  1 zhangc  staff     0B Mar 10 11:08 text.txt

② 分支上创建文件,并删除分支上的test.txt文件

zhangc@test:~/Desktop/GitTest/gitDemo|newtest 
⇒  ll
total 0
-rw-r--r--  1 zhangc  staff     0B Mar  9 17:14 README
-rw-r--r--  1 zhangc  staff     0B Mar 10 11:08 text.txt
zhangc@test:~/Desktop/GitTest/gitDemo|newtest 
⇒  touch zc.php
zhangc@test:~/Desktop/GitTest/gitDemo|newtest⚡ 
⇒  git rm -rf ./text.txt 
rm 'text.txt'
zhangc@test:~/Desktop/GitTest/gitDemo|newtest⚡ 
⇒  git add ./
zhangc@test:~/Desktop/GitTest/gitDemo|newtest⚡ 
⇒  git commit -m "add\del"
[newtest dee7c36] add\del
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename text.txt => zc.php (100%)
zhangc@test:~/Desktop/GitTest/gitDemo|newtest 
⇒  ll
total 0
-rw-r--r--  1 zhangc  staff     0B Mar  9 17:14 README
-rw-r--r--  1 zhangc  staff     0B Mar 10 11:10 zc.php

③ 切换到主干,并合并分支到主干

|newtest ⇒  git checkout master  
Switched to branch 'master'
|master ⇒  ll
total 0
-rw-r--r--  1 zhangc  staff     0B Mar  9 17:14 README
-rw-r--r--  1 zhangc  staff     0B Mar 10 11:11 text.txt

|master ⇒  git merge newtest  
Updating 57ccd61..dee7c36
Fast-forward
 text.txt => zc.php | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename text.txt => zc.php (100%)

|master ⇒  ll
total 0
-rw-r--r--  1 zhangc  staff     0B Mar  9 17:14 README
-rw-r--r--  1 zhangc  staff     0B Mar 10 11:16 zc.php

再查看时,test.txt文件被删除,合并分支的文件生成

5、合并冲突

①修改主干上的文件内容

|master 
⇒  echo "888" > zc.php 
|master⚡ 
⇒  git add .
zhangc@test:~/Desktop/GitTest/gitDemo|master⚡ 
⇒  git commit -m "edit"
[master 0c5604f] edit
 1 file changed, 1 insertion(+), 1 deletion(-)

②修改分支上的文件内容

|newtest 
⇒  echo "777" > zc.php 
zhangc@test:~/Desktop/GitTest/gitDemo|newtest⚡ 
⇒  git add .
zhangc@test:~/Desktop/GitTest/gitDemo|newtest⚡ 
⇒  git commit -m "m"
[newtest 4834eb6] m
 1 file changed, 1 insertion(+), 1 deletion(-)

③分支合并到主干

|master 
⇒  git merge newtest  
Auto-merging zc.php
CONFLICT (content): Merge conflict in zc.php
Automatic merge failed; fix conflicts and then commit the result.

|master⚡ 
⇒  cat zc.php 
<<<<<<< HEAD
888
=======
777
>>>>>>> newtest

报错冲突

④接下来我们需要手动去修改它

vim zc.php 

|master⚡ 
⇒  cat zc.php 

888

777


⇒  git add .
zhangc@test:~/Desktop/GitTest/gitDemo|master⚡ 
⇒  git commit -m "add"
[master 9ad231f] add

现在我们成功解决了合并中的冲突,并提交了结果。

posted @ 2021-03-10 12:00  Owen_ET  阅读(388)  评论(0编辑  收藏  举报