背景
Git沖突的原因,一般是修改了同一個(gè)文件導(dǎo)致的丛塌,這個(gè)文件有可能是別人提交到遠(yuǎn)程倉(cāng)庫(kù)里面疮薇,還有就是需要合并這個(gè)文件導(dǎo)致的笤昨。
解決方法
- 你確定你需要的是哪個(gè)倉(cāng)庫(kù)的文件
git checkout --theirs conflicted_file.txt # 保留遠(yuǎn)端的
git checkout --ours conflicted_file.txt # 保留本地的
- 然后執(zhí)行add和commit
git add -A
git commit -m "update conflict
舉個(gè)栗子
獲取遠(yuǎn)端服務(wù)器上的文件,提示沖突了需要合并
# git cherry-pick FETCH_HEAD
* branch refs/changes/85/12385/3 -> FETCH_HEAD
error: 'cherry-pick' is not possible because you have unmerged files.
hint: Fix them up in the work tree,
hint: and then use 'git add/rm <file>' as
hint: appropriate to mark resolution and make a commit,
hint: or use 'git commit -a'.
fatal: cherry-pick failed
查看當(dāng)前倉(cāng)庫(kù)的狀態(tài)
# git status
Not currently on any branch.
You are currently cherry-picking commit 53e5374.
(fix conflicts and run "git cherry-pick --continue")
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: file1.txt
用提示的命令執(zhí)行
# git cherry-pick --continue
U file1.txt
error: 'commit' is not possible because you have unmerged files.
hint: Fix them up in the work tree,
hint: and then use 'git add/rm <file>' as
hint: appropriate to mark resolution and make a commit,
hint: or use 'git commit -a'.
fatal: Exiting because of an unresolved conflict.
提示file1.txt有更新停士,確定替換為遠(yuǎn)程倉(cāng)庫(kù)的文件挖帘。這里用theirs
git checkout --theirs file1.txt
然后添加到本地倉(cāng)庫(kù)
git add -A
最后繼續(xù)cherry-pick「復(fù)制」
# git cherry-pick --continue
[detached HEAD 8f26ce8] Summary : test git checkout --theirs
Author: Rik
2 files changed, 0 insertions(+), 0 deletions(-)
小結(jié)
git ckeckout 和 帶參數(shù)的--ours和 --theirs還是有區(qū)別的。