xxxxxxxxxx
git pull = git fetch + git merge
第一步git fetch将远程的版本库的对象(提交,tag,分支等)拉在本地即可
第二部git merge 将其他人push上的远程仓库和本地仓库的提交进行合并
git merge [选项 ...] <commit> ...
相当于git pull隐式的会合并本地和远程仓库的提交
默认情况下,合并后的结果会自动提交,如果使用--no-commit选项,则合并的结果会放入暂存区,用户可以对合并结果进行检查,更改,然后手动提交。
合并并非总是成功,可能会出现冲突、
xxxxxxxxxx
jagent@ubuntu:~/xin/user1$ echo "this is a 3.txt user" >> 3.txt
jagent@ubuntu:~/xin/user1$ git add -A;git commit -m "usr1 submit"
[master 50e1096] usr1 submit
2 files changed, 3 insertions(+)
create mode 100644 config
jagent@ubuntu:~/xin/user1$ git push origin HEAD
Counting objects: 11, done.
Delta compression using up to 48 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 348 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1)
remote: Processing changes: done
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
fdf4348..50e1096 HEAD -> master
jagent@ubuntu:~/xin/user2$ echo "this is a 4.txt user2" >> 4.txt
jagent@ubuntu:~/xin/user2$ git add -A;git commit -m "user2 commit "
jagent@ubuntu:~/xin/user2$ git fetch
remote: Counting objects: 6, done
remote: Finding sources: 100% (4/4)
remote: Total 4 (delta 1), reused 4 (delta 1)
Unpacking objects: 100% (4/4), done.
From ssh://10.148.192.216:29418/ST/mytest
fdf4348..50e1096 master -> origin/master
jagent@ubuntu:~/xin/user2$ git merge origin/master
jagent@ubuntu:~/xin/user2$ git log --graph -3 --stat
* commit 9e7bed4e4d8f2fb60aa644c6263d726d76b961a5
|\ Merge: 350f4b1 50e1096
| | Author: user2 <user2@122.com>
| | Date: Mon Nov 16 15:18:21 2020 +0800
| |
| | Merge remote-tracking branch 'origin/master'
| |
| * commit 50e109692ce233217be9dd234577439367c6d83a
| | Author: user1 <user1@122.com>
| | Date: Mon Nov 16 14:54:04 2020 +0800
| |
| | usr1 submit
| |
| | 3.txt | 1 +
| | config | 2 ++
| | 2 files changed, 3 insertions(+)
| |
* | commit 350f4b1257d90026041d1d2412233c07850bb599
|/ Author: user2 <user2@122.com>
| Date: Mon Nov 16 15:17:00 2020 +0800
|
| user2 commit
|
| 4.txt | 1 +
| 1 file changed, 1 insertion(+)
最后通过git log --graph -3 --stat看出合并记录
修改相同文件的不同区域和上面情况类似
逻辑冲突这种情况存在代码逻辑里,头文件等改名,而其他c文件的函数找不到需要调用头文件的函数,
xxxxxxxxxx
如果两个用户修改了同一文件的同一区域
jagent@ubuntu:~/xin/user1$ cat 1.txt
11
this is user1 modifiy
jagent@ubuntu:~/xin/user1$ git add -A;git commit -m "user1 commit"
[master 0a5e505] user1 commit
1 file changed, 1 insertion(+), 1 deletion(-)
jagent@ubuntu:~/xin/user1$ git push origin HEAD
Counting objects: 3, done.
Delta compression using up to 48 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 416 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Processing changes: done
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
50e1096..0a5e505 HEAD -> master
jagent@ubuntu:~/xin/user2$ cat 1.txt
11
this is user2 modifiy
jagent@ubuntu:~/xin/user2$ git push origin HEAD
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
! [rejected] HEAD -> master (fetch first)
error: failed to push some refs to 'ssh://xwx496887@10.148.192.216:29418/ST/mytest'
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.
jagent@ubuntu:~/xin/user2$ git pull
remote: Counting objects: 5, done
remote: Finding sources: 100% (3/3)
remote: Total 3 (delta 0), reused 3 (delta 0)
Unpacking objects: 100% (3/3), done.
From ssh://10.148.192.216:29418/ST/mytest
50e1096..0a5e505 master -> origin/master
Auto-merging 1.txt
CONFLICT (content): Merge conflict in 1.txt
Automatic merge failed; fix conflicts and then commit the result.
jagent@ubuntu:~/xin/user2$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 3 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
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")
合并过程是通过.git/MERGE_HEAD MERGE_MODE MERGE_MSG记录的
jagent@ubuntu:~/xin/user2/.git$ git ls-files -s
100644 0f990c36b4239e0962eba6fa4a84b73ebacc3e89 1 1.txt
100644 1d38e54f8cea7ce452f339b36e0a46e02cbd6d03 2 1.txt
100644 c9ce0ccc509e993775cabae0aef64c36dfa124f4 3 1.txt
100644 5699c356ebd2937f794e526b2bca10a18a36d50e 0 2.txt
100644 b4867aa0fb50e759c193cb4ddce018d7379028c5 0 3.txt
100644 878950e8c5ffd4f9fae08ee52db1fca2d5ac8f2b 0 4.txt
100644 c3f407c0955bb5738e40a82664c79b63f04a9adb 0 5.txt
100644 09567e828d6c4e7fe358df25ce19966c0a6b273a 0 README.md
100644 0f25ec8885ca8be4c5d11f01dda38dc25f583422 0 config
大于0的文件就是冲突文件(了解)
编号1的暂存区保存冲突文件修改之前的副本git show :1:1.txt
编号2的暂存区保存冲突文件修改之前的副本git show :2:1.txt(当前分支)
编号3的暂存区用于保存当前冲突文件在合并版本中修改的git show :3:1.txt
冲突解决
11
<<<<<<< HEAD
this is user2 modifiy
=======
this is user1 modifiy
>>>>>>> 0a5e50511aedff98ddb7485cd15be42bf7c9fe9d
<<<<<<< HEAD
this is user2 modifiy
=======当前本地最新提交
this is user1 modifiy
>>>>>>> 0a5e50511aedff98ddb7485cd15be42bf7c9fe9d 远程库最新的提交
图形化git mergetool一般是vimdiff 操作完会生成一个.orig文件
解决完.git/MERGE_HEAD MERGE_MODE MERGE_MSG就消失了
xxxxxxxxxx
如果一个用户将某个文件改名,另外一个用户将同样的文件改为其他的名字,这2个用户提交会产生冲突,这种冲突叫树冲突。这种情况少不举实例
xgit合并操作可以指定使用的合并策略,默认会选择最适合的合并策略。例如:两个分支的合并默认使用recursive合并策略、两个以上分支的合并默认使用octpus合并策略。我们也可以通过传递参数来指定使用的合并策略,命令行模式为:gig merge -s 合并策略 -x 合并策略参数 <commit>...
resolve策略:只能用于两个分支的合并,这个合并策略被认为是最安全、最快的合并策略
recursive策略:只能用于两个分支的合并,两个分支的合并默认使用该策略。该策略可以使用选项:
ours:在遇到冲突的时候,选择当前分支的版本,而忽略远程合并过来的版本。
theirs:和ours选项相反
subtree[=path]:使用子树合并策略
octpus策略:用于合并两个以上分支的策略,该策略拒绝执行需要手动解决的复杂合并。
ours:可以用于任意多个分支的合并策略,合并的结果总是使用当前分支的内容而丢弃远程合并过来的版本内容
subtree:这是一个经过调整后的recursive策略,子树合并策略相关内容可以去百度,这里就不详解了(主要是博主也不会~~)
合并相关的设置
可以通过命令git config来设置与合并相关的配置变量,对合并进行配置。
merge.conflictstyle:该配置变量定义冲突文件中冲突内容的标记风格,有两个可用的风格:默认的merge和diff3
默认的merge风格使用标准的冲突分界符<<<<<<<=======>>>>>>>读冲突内容进行标识,两个文字块分别是本地的修改和他人的修改
diff3风格会在冲突文件中出现三个文字块,<<<<<<< ||||||| ======= >>>>>>>,分别表示本地更改版本、共同的原始版本、他人更改的版本
merge.tool:设置执行命令git mergetool进行冲突解决时调用的图形化工具
merge..path:设置对应的图形化工具的安装位置