git:如何提取最新更改并解决冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25260926/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:18:00  来源:igfitidea点击:

git: how to pull latest changes and resolve conflicts

gitgithubconflictpull

提问by Boosted_d16

I want to pull in all the latest changes from a github repo. There is only one branch and only one person (the developer) adds and modifies the code. I simply pull everything in, once a new feature is added.

我想从 github 存储库中提取所有最新更改。只有一个分支,只有一个人(开发人员)添加和修改代码。一旦添加了新功能,我就简单地将所有内容都拉进来。

Normally, all I do is:

通常,我所做的就是:

git pull

But this time I got an error message which reads:

但是这次我收到一条错误消息,内容如下:

Automatic merge failed; fix conflicts and then commit the results.

自动合并失败;修复冲突,然后提交结果。

I'd simply like to pull in the latest changes from the repo. I'm not sure why there are conflicts when only one person is responsible for this repo.

我只是想从 repo 中提取最新的更改。我不知道为什么只有一个人负责这个 repo 时会发生冲突。

Note: I don't want to commit anything to the repo.

注意:我不想向 repo 提交任何内容。

回答by Andrey Deineko

Basically, if you just watch the repo and do not change the files (or do not want your changes to remain after update) do:

基本上,如果您只观看 repo 并且不更改文件(或不希望您的更改在更新后保留),请执行以下操作:

git reset --hard HEADfollowed by git pull.

git reset --hard HEAD其次是git pull

This must overwrite the code on your local machine with code from github repo.

这必须使用 github 存储库中的代码覆盖本地计算机上的代码。

Another option would be git fetch --allfollowed by git reset --hard origin/master.

另一种选择是 git fetch --all后面git reset --hard origin/master

回答by torek

  1. git pull= git fetch+ git merge.
  2. Things went wrong in the second half, the git merge.
  3. For git mergeto choose not to do a "fast forward" instead of a real merge, someone (perhaps the developer, perhaps you) has to have done something unusual.
  4. Only a real merge can have merge conflicts,
  1. git pull= git fetch+ git merge
  2. 下半场出了问题,git merge.
  3. 为了git merge选择不进行“快进”而不是真正的合并,必须有人(可能是开发人员,也可能是您)做了一些不寻常的事情。
  4. 只有真正的合并才会有合并冲突,

So, the solution to the problem lies in figuring out the "something unusual" and/or the person involved.

因此,问题的解决方案在于找出“不寻常的事情”和/或所涉及的人。

Note that if you are actively making your ownchanges, the merge conflict is not unusual after all. I'm taking you at your word that you have not done your own git commits.

请注意,如果您正在积极进行自己的更改,那么合并冲突毕竟并不罕见。我相信你的话,你没有做你自己git commit的事情。

In any case, the first thing to do (if you don't want to just re-clone everything from scratch) is to terminate the conflicted merge, so that you're back to where you would be if you had used git fetchand not also used git merge. (You did use both, via git pull, and it was the mergethat failed.) To end the attempt to merge, since you're in this state where it failed, you can just use:

在任何情况下,要做的第一件事(如果您不想从头开始重新克隆所有内容)是终止冲突的合并,以便您回到使用过的位置git fetch而不是用过git merge。(你确实使用了两个, via git pull,它merge失败了。)为了结束合并尝试,因为你处于失败的状态,你可以使用:

git merge --abort

Next, it's probably worth figuring out why git wants to do a merge in the first place. The easiest way to do that is to run these two commands:

接下来,可能有必要弄清楚为什么 git 首先要进行合并。最简单的方法是运行以下两个命令:

git fetch
git log --graph --oneline --decorate --all

The logwill show you where yourbranch—probably named master—ends, and where the "remote" (origin/master) ends. (The git fetchstep is just in case you're using an older git, where git pullruns git fetchin such a way that it skips a step, which will make the git logoutput misleading.)

log会告诉你在哪里你的分支可能命名master-ends和“远程”(其中origin/master)结束。(这git fetch一步是为了以防万一您使用的是较旧的 git,它以跳过一个步骤的方式git pull运行git fetch,这会使git log输出产生误导。)

If you see something like this:

如果你看到这样的东西:

* f96824d (HEAD, master) some commit message
* d1574b8 (origin/master) another commit message
* b9491ea a third commit message
...

this means you have a commit they don't.1If you see:

这意味着你有一个他们没有的承诺。1如果您看到:

* 676699a (origin/master) here a commit message
| * 9124325 (HEAD, master) there a commit message
|/
* a19ce3f everywhere a commit message
...

then again, you have a commit they don't.

再说一次,你有一个他们没有的承诺。

If youdid not make this (or these) commit(s), theymust have retractedsome commit(s). You have them because you grabbed them at some point, with an earlier git fetch, and then put them on your masterbranch with an earlier git merge. But they've retracted these commit(s), and then perhaps made more new ones. With your latest git mergeyou're asking git to combine their old commit(s) and their new ones, and those are conflicting.

如果没有进行这个(或这些)提交,他们一定已经撤回了一些提交。您拥有它们是因为您在某个时候使用了 early 抓住了它们git fetch,然后使用了 early将它们放在了你的master分支上git merge。但是他们已经撤回了这些提交,然后可能做了更多的新提交。使用您的最新版本,git merge您要求 git 将他们的旧提交和新提交结合起来,而这些是相互冲突的。

In both cases you can simply force your masterto move to the same commit as their origin/master, with:

在这两种情况下,您都可以简单地强制您master移动到与他们相同的提交origin/master,使用:

git reset --hard origin/master

The resetcommand means "take whatever my current branch is"—in this case, master—"and make it point to the exact same commit as I name here: origin/master". The --hardmeans "and also, throw away my current work-tree and replace it with the one attached to the commit I'm moving to now."

reset命令的意思是“获取我当前分支的任何内容”——在本例中,master——“并使其指向与我在此处命名的完全相同的提交:origin/master”。这--hard意味着“而且,扔掉我当前的工作树,并将其替换为附加到我现在要移动到的提交的工作树。”



The normal case—normal for your setup anyway—is that they can have commit(s) that you don't, but every commit you have, they also have. In this case your git mergesees that it can simply "slide your branch up" to match theirs, which is a "fast forward" operation. In cases like that, the git log --all --decorate --graph --oneline(the order of these options does not matter) output looks more like:

正常情况——无论如何对你的设置来说都是正常的——是他们可以有你没有的提交,但你有的每一次提交,他们也有。在这种情况下,您会git merge看到它可以简单地“向上滑动您的分支”以匹配他们的分支,这是一个“快进”操作。在这种情况下,git log --all --decorate --graph --oneline(这些选项的顺序无关紧要)输出看起来更像是:

* 7cf123a (origin/master) lots of twisty
* b9cce10 little commit messages,
* 009cda1 (HEAD, master) all somewhat alike
...

In this case, just imagine that the labels (HEAD, master) are slid up along the lines connecting the *s. (The actual lines are too small to draw here—there's just not enough room between lines—but git log --graph --all --decoratewill show them: this time we leave out --onelineso there are more lines; as will graphical viewers like gitk. As shown above, in some cases there is some room for lines, which git logdraws with |and /characters and such.)

在这种情况下,想象一下标签 ( HEAD, master) 沿着连接*s的线向上滑动。(实际的线条太小,无法在此处绘制 - 线条之间没有足够的空间 - 但git log --graph --all --decorate会显示它们:这次我们省略了--oneline所以有更多的线条;图形查看器如gitk。如上所示,在某些情况下有一些用于线条的空间,用于git log绘制|/字符等。)



1Actually, if you see this, you should just get an everything up to datefrom your git merge, and hence no conflict. So I'm expecting something like the 2nd one.

1实际上,如果你看到这个,你应该只everything up to date从你的 中得到一个git merge,因此没有冲突。所以我期待像第二个这样的东西。

回答by Vishwanath

You are getting conflicts because you have made some commits on the local repo which conflicts with what is pushed on the origin.

您遇到冲突是因为您在本地存储库上进行了一些提交,这些提交与源上推送的内容冲突。

There can be multiple ways to solve this.

可以有多种方法来解决这个问题。

  1. Reset the local repo with origin. Assuming your branch name is master. First download all the server changes with git fetch and just repoint your current branch to origin branch with reset hard.

    git fetch --all
    git reset --hard origin/master

  2. Resolve the conflicts in the file and then commit them back again. Check the files that are in conflict. Add them to index after you have resolved the conflicts and then commit the changes.

    git add

    git commit -m

  1. 使用原点重置本地存储库。假设您的分支名称是 master。首先使用 git fetch 下载所有服务器更改,然后通过重置硬将当前分支重新指向原始分支。

    git fetch --all
    git reset --hard origin/master

  2. 解决文件中的冲突,然后再次提交它们。检查冲突的文件。解决冲突后将它们添加到索引,然后提交更改。

    添加

    git commit -m

回答by Umar Asghar

You can do the following way

您可以通过以下方式进行

git fetch origin // that will pull all the updates for all the branches
git checkout <branch>
// either of these two below
git pull origin <branch> 
// if the above command does not work and shows you conflicts
// to get rid off from conflicts try this
git reset --hard origin/<branch>