我如何在我的 Git 存储库中解决合并冲突?
当前回答
请遵循以下步骤来解决 Git 中的合并冲突:
查看 Git 状态: git 状态 获取 patchset: git fetch (从您的 Git commit 查看正确的 patch) Checkout 一个本地分支(temp1 在我的例子中在这里): git checkout -b temp1 Pull 最近的内容从主: git pull -rebase 起源主 启动 mergetool 并检查冲突并纠正它们......并检查与您当前分支的远程分支中的变化: git mergetool Chec
其他回答
CoolAJ86的答案相当于一切. 如果您在同一代码中有两个分支的变化,您将不得不进行手动合并. 在任何文本编辑器中打开冲突中的文件,您应该看到下列结构。
(Code not in Conflict)
>>>>>>>>>>>
(first alternative for conflict starts here)
Multiple code lines here
===========
(second alternative for conflict starts here)
Multiple code lines here too
<<<<<<<<<<<
(Code not in conflict here)
选择一个替代品或两者的组合,以一种方式,你想要新的代码是,同时删除平等的标志和角膜。
git commit -a -m "commit message"
git push origin master
简单地说,如果你知道其中一个存储库的变化并不重要,并且想解决所有变化,有利于另一个,请使用:
git checkout . --ours
以您的存储库的利益来解决更改,或者
git checkout . --theirs
以解决其他或主要存储库的利益的变更。
或者你将不得不使用一个 GUI 合并工具,一步一步通过文件,说合并工具是 p4merge,或者写下你已经安装的任何一个的名字。
git mergetool -t p4merge
完成文件后,您将不得不保存和关闭,以便下一个将打开。
这个答案是为那些像我这样的Vim用户添加一个替代方案,他们更喜欢在编辑器内做任何事情。
饰 饰 DR
此分類上一篇
Tpope 帶來了這個偉大的插件為 Vim 稱為 fugitive. 一旦安裝,你可以執行 :Gstatus 檢查有衝突的檔案和 :Gdiff 開啟 Git 在三路合併。
一旦在三路合并,逃生会让你得到你合并的分支中的任何一个变化,如下:
:diffget //2,从原始(HEAD)分支中获取变更: :diffget //3,从合并分支中获取变更:
一旦你完成了合并文件,输入 :Gwrite 在合并的泡沫中。
Vimcasts发布了一个很好的视频,详细解释了这些步骤。
我明白什么是合并冲突,但当我看到Git diff的输出时,这对我一开始就看起来很荒谬:
git diff
++<<<<<<< HEAD
+ display full last name boolean in star table
++=======
+ users viewer.id/star.id, and conversation uses user.id
+
++>>>>>>> feat/rspec-tests-for-cancancan
但是,这里是帮助我的一点:
<<<<<和 =======之间的一切都是在一个文件中的一切,而在 =======和 >>>>>之间的一切都是在另一个文件中的一切,所以你必须做的就是打开与合并冲突的文件,并从任何分支中删除这些线条(或者只是让它们相同),合并将立即成功。
下面是可能的使用案例,从顶部:
你会拖一些变化,但哦,你没有到目前为止:
git fetch origin
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.
所以你更新并尝试一次,但有冲突:
git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.
所以你决定看看这些变化:
git mergetool
哦,我的,哦,我的,上流改变了一些事情,但只是用我的变化......不......他们的变化......
git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"
然后,我们试着最后一次。
git pull origin master
From ssh://gitosis@example.com:22/projectname
* branch master -> FETCH_HEAD
Already up-to-date.
是的!
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别