Git 快速使用 之 冲突 merge 快速解决

在项目开发中我都会遇到多人协作同时开发同一个项目,在使用Git 时也经常出现冲突的情况,下面我就介绍一下我在开发中遇到的冲突的解决方法。

在push 远程的时候出现冲突

在开发中,在我们向远程Git 仓库push 代码的时候,经常会因为本地的版本落后于远程的,报出类似下面的错误提示。

error: failed to push some refs to '[email protected]:CNCFOX/NDKLearn.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.

我们来看一下我的一个完整提交过程,看看这个错误是怎么出来的。

cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes in working directory)

        modified:   app/src/main/res/values/colors.xml

no changes added to commit (use "git add" and/or "git commit -a")

cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git add app/src/main/res/values/colors.xml
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git commit -s
[dev 1c39ee7] push meger
 1 file changed, 1 insertion(+), 1 deletion(-)
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git push origin dev
To github.com:CNCFOX/NDKLearn.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to '[email protected]:CNCFOX/NDKLearn.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.

上面的这些操作我是想把本地修改的colors.xml文件commit 后提交到远程仓库中,但是,在我提交之前有一个人也修改了此文件并且提交到了远程仓库中,在我提交时,本得的代码就不是最新的了,落后于远程仓库的代码,所以这也是为什么经常会有人告诉我们,在提交代码前要先同步一下代码的原因。在Git 中同步代码常用的有两种方式 ,一种是使用pull 命令,一种是使用fetch + rebase ,会有专门的文章介绍这两种的用法。下面我们使用pull 方法进行同步一下代码。

操作如下:

cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git pull
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 5), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
From github.com:CNCFOX/NDKLearn
   7e3f2e9..1bd642b  dev        -> origin/dev
Auto-merging app/src/main/res/values/colors.xml
CONFLICT (content): Merge conflict in app/src/main/res/values/colors.xml
Automatic merge failed; fix conflicts and then commit the result.

上面的信息的意思是说 colors.xml 文件发生冲突,我们需要解决这个冲突,然后重新提交。

下面我们打开看一下冲突的文件:



    #3F51B5
    #303F9F
    #FF4081

<<<<<<< HEAD
#FF4081
=======
#FF4081
>>>>>>> 1bd642b86f99072e0930e72e84168ab3faf43d88

 #FF4081


没看过的,看第一眼就蒙了,其实很好理解的:<<<<<<<>>>>>>>之间的是发生冲突的部分,中间的=======是将冲突的部分进行分离,<<<<<<< HEAD=======是本地的代码,=======>>>>>>> 1bd642b86f99072e0930e72e84168ab3faf43d88是远程仓库的代码。如下:

本地代码:

<<<<<<< HEAD
#FF4081
=======

远程仓库代码:

=======
#FF4081
>>>>>>> 1bd642b86f99072e0930e72e84168ab3faf43d88

我们将冲突的部分合并后重新提交就可以push 了。
下面是实际操作:

cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git status
On branch dev
Your branch and 'origin/dev' have diverged,
and have 1 and 1 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 ..." to mark resolution)

        both modified:   app/src/main/res/values/colors.xml

no changes added to commit (use "git add" and/or "git commit -a")
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git add  app/src/main/res/values/colors.xml
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git commit -s
[dev fa5738d] Merge le
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git pull
Already up-to-date.
cfox@cfox-PC:~/Disk/gitTest/NDKLearn
$ git push origin dev
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (16/16), 1.40 KiB | 0 bytes/s, done.
Total 16 (delta 10), reused 0 (delta 0)
remote: Resolving deltas: 100% (10/10), completed with 5 local objects.
To github.com:CNCFOX/NDKLearn.git
   1bd642b..fa5738d  dev -> dev

你可能感兴趣的:(Git 快速使用 之 冲突 merge 快速解决)