git pull origin master 出现合并冲突:我该怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20909667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git pull origin master gives merge conflict: what should I do?
提问by flossfan
I'm working on a branch (my-branch
) that I created from master.
我正在my-branch
从 master 创建的分支 ( )上工作。
$ git checkout -b my-branch
I edited some files, and then checked them into the new branch:
我编辑了一些文件,然后将它们签入新分支:
$ git commit -a -m 'Add new feature'
Then I pulled from master (I'm not completely sure why I did this, or if it's good practice):
然后我从 master 那里拉(我不完全确定我为什么这样做,或者它是否是好的做法):
$ git pull origin master
But pulling gave me lots of error messages:
但是拉给了我很多错误信息:
From github.com
* branch master -> FETCH_HEAD
Auto-merging styles/common/module_more_info.scss
CONFLICT (add/add): Merge conflict in styles/common/module_more_info.scss
Auto-merging app/support/stagecraft_stub/responses/cloud.json
CONFLICT (content): Merge conflict in app/support/stagecraft_stub/responses/cloud.json
Auto-merging app/support/backdrop_stub/response_fetcher.js
CONFLICT (content): Merge conflict in app/support/backdrop_stub/response_fetcher.js
Automatic merge failed; fix conflicts and then commit the result.
vagrant@pp-development-1:/var/apps/spotlight$ git status
Running git status
now shows lots of changed files:
git status
现在运行会显示许多更改的文件:
# On branch my-branch
# 在分支 my-branch
# Changes to be committed:
#
# modified: CONTRIBUTING.md
# modified: README.md
# modified: app/appBuilder.js
[lots more files]
#
# Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: app/support/backdrop_stub/response_fetcher.js
# both modified: app/support/stagecraft_stub/responses/cloud.json
# both added: styles/common/module_more_info.scss
Firstly, what has happened, and secondly, what do I do?
首先,发生了什么,其次,我该怎么办?
If I try to see what the differences are in any of the upper list of files, I get empty output, which is confusing (why does it want me to commit the file if there are no diffs?):
如果我尝试查看任何上层文件列表中的差异,我会得到空输出,这令人困惑(如果没有差异,为什么要我提交文件?):
$ git diff CONTRIBUTING.md
$
Should I review the three files that are under Unmerged paths
, and then commit this as a merge commit? UPDATE: Most importantly, can I then push it to my branch withoutmessing up the master branch?
我应该查看 下的三个文件,Unmerged paths
然后将其作为合并提交提交吗?更新:最重要的是,我可以将它推送到我的分支而不会弄乱主分支吗?
I don't seem to be able to roll back the last commit.
我似乎无法回滚上次提交。
回答by Ramesh Rajendran
Don't use merge tool, because it's confusing to you. Please take a backup your current code and revert your changes in current branch. Then checkout your master (dev), get latest then rewrite your changes, then push.
不要使用合并工具,因为它会让你感到困惑。请备份您当前的代码并恢复您在当前分支中的更改。然后检查您的主(开发),获取最新信息,然后重写您的更改,然后推送。
First you need to type gitk --all
in your github for which branch will currently open by shown top side
首先,您需要输入gitk --all
您的 github 当前将通过显示的顶部打开哪个分支
then you need to revert
that or rebase
your code with your server latest code:
那么你需要使用你的服务器最新代码revert
或者rebase
你的代码:
then
然后
check type the followings for push your branch to server master.
检查键入以下内容以将您的分支推送到服务器主服务器。
git status
git add .
git status
git commit -a -m "Comments"
git push origin yourbranchname
That's all...
就这样...
回答by Xelian
So first configure your merge tool. I propose to install this
所以首先配置你的合并工具。我建议安装这个
Now your changes are in conflict with the remote ones and you have to resolve the conflicts. Do for every file:
git merge tool
Save the file.
现在您的更改与远程更改发生冲突,您必须解决冲突。为每个文件做:
git合并工具
保存文件。
Now you said to git that you know that there were conflicts but you want to resolve them in the way you did it using merge tool. So the conflicts are now resolved.
现在您对 git 说您知道存在冲突,但您想以使用合并工具的方式解决它们。所以现在冲突已经解决了。
git status
Now you have to commit the resolved files and then to push them.
现在您必须提交已解析的文件,然后推送它们。
PS. Another approach is to stash your changes instead of a commit then to pull from the origin and then to pop your stashes.
附注。另一种方法是存储您的更改而不是提交,然后从源中提取,然后弹出您的存储。
git stash save
git pull
git stash pop
git commit
git pull
But from what I read if you are sure for your changes you have to commit them as you did and then to merge them. Merging is very often and you have to know how to handle it.
但是从我读到的内容来看,如果您确定自己的更改,则必须像以前一样提交它们,然后合并它们。合并很常见,您必须知道如何处理。
回答by gregor
Basically, yes. git mergetool
might be of some assistance. It simply opens a merge tool with the files that need your attention and afterwards adds them to the index. With a simple git commit
you can create the merge conflict.
基本上,是的。git mergetool
可能会有一些帮助。它只是打开一个包含需要您注意的文件的合并工具,然后将它们添加到索引中。使用简单的方法,git commit
您可以创建合并冲突。
You can always go back to the state before your pull by executing git reset --hard HEAD
.
您始终可以通过执行返回到拉取前的状态git reset --hard HEAD
。
回答by Ederson
What may have happened is that:
可能发生的事情是:
- Some one else was editing or merged it's code with Master.
- You have a different charset from the last user who edited Master.
- 其他人正在编辑或将其代码与 Master 合并。
- 您的字符集与上一个编辑 Master 的用户不同。
The solution is:
解决办法是:
git mergetool <you can left here blank your choose a mergetool from those you have installed in your machine>
回答by user3153632
Open the conflict file and resolve removing the diffs.
打开冲突文件并解决删除差异。
Then:
然后:
git add .
git commit -am "resolve conflicts"
git push origin master