采用cours/theirs 解决Git冲突
背景
git冲突一般是修改了同一个文件导致的。
解决方法
确定你需要的是哪个仓库的文件,然后执行相应的命令即可。
保留当前分支代码
git checkout --ours {codefiles}
保留要合并分支代码
git checkout --theirs {codefiles}
1 新建测试目录
新增readme.md
文件,文件内容为"hello"。保存后,新建两个分支分别为feature,develop
。
➜ git.worker git init
Initialized empty Git repository in /home/jinchunguang/worker/git.worker/.git/
➜ git.worker git:(master) echo "hello" > readme.md
➜ git.worker git:(master) ✗ cat readme.md
hello
➜ git.worker git:(master) ✗ git add .
➜ git.worker git:(master) ✗ git commit -m "create readme.md"
[master (root-commit) 00b953e] create readme.md
1 file changed, 1 insertion(+)
create mode 100644 readme.md
➜ git.worker git:(master) git branch feature
➜ git.worker git:(master) git branch develop
2 在feature
分支追加内容 "我是feature", 在develop
分支追加内容 "我是develop "
# feature 分支
➜ git.worker git:(master) git checkout feature
Switched to branch 'feature'
➜ git.worker git:(feature) echo "我是feature" >> readme.md
➜ git.worker git:(feature) ✗ cat readme.md
hello
我是feature
➜ git.worker git:(feature) ✗ git add .
➜ git.worker git:(feature) ✗ git commit -m "update readme.md"
[feature d6fa79b] update readme.md
1 file changed, 1 insertion(+)
# develop分支
➜ git.worker git:(feature) git checkout develop
Switched to branch 'develop'
➜ git.worker git:(develop) echo "我是develop" >> readme.md
➜ git.worker git:(develop) ✗ cat readme.md
hello
我是develop
➜ git.worker git:(develop) ✗ git add .
➜ git.worker git:(develop) ✗ git commit -m "update readme.md"
[develop 3576b42] update readme.md
1 file changed, 1 insertion(+)
3 测试
3.1 git merge
develop merge feature ,git checkout --ours 保留 develop 分支内容 ,反之亦然。
➜ git.worker git:(develop) git merge feature
Auto-merging readme.md
CONFLICT (content): Merge conflict in readme.md
Automatic merge failed; fix conflicts and then commit the result.
➜ git.worker git:(develop) ✗ cat readme.md
hello
<<<<<<< HEAD
我是develop
=======
我是feature
>>>>>>> feature
➜ git.worker git:(develop) ✗ git checkout --ours .
➜ git.worker git:(develop) ✗ cat readme.md
hello
我是develop
➜ git.worker git:(develop) ✗ git merge --abort
3.2 git rebase
存在变基,develop rebase feature 后,git checkout --ours 保留 我是feature ,反之亦然。**
➜ git.worker git:(develop) git rebase feature
First, rewinding head to replay your work on top of it...
Applying: update readme.md
Using index info to reconstruct a base tree...
M readme.md
Falling back to patching base and 3-way merge...
Auto-merging readme.md
CONFLICT (content): Merge conflict in readme.md
error: Failed to merge in the changes.
Patch failed at 0001 update readme.md
Use 'git am --show-current-patch' to see the failed patch
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
➜ git.worker git:(d6fa79b) ✗ cat readme.md
hello
<<<<<<< HEAD
我是feature
=======
我是develop
>>>>>>> update readme.md
➜ git.worker git:(d6fa79b) ✗ git checkout --ours .
➜ git.worker git:(d6fa79b) ✗ cat readme.md
hello
我是feature
➜ git.worker git:(d6fa79b) ✗ git rebase --abort
3.3 git cherry-pick
develop cherry-pick feature ,git checkout --ours 保留 develop 分支内容 ,反之亦然。
➜ git.worker git:(develop) git cherry-pick feature
error: could not apply d6fa79b... update readme.md
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
➜ git.worker git:(develop) ✗ cat readme.md
hello
<<<<<<< HEAD
我是develop
=======
我是feature
>>>>>>> d6fa79b... update readme.md
➜ git.worker git:(develop) ✗ git checkout --ours .
➜ git.worker git:(develop) ✗ cat readme.md
hello
我是develop
网友评论