git 如何放弃远程更改并将文件标记为“已解决”?

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

How can I discard remote changes and mark a file as "resolved"?

gitmergeconflictgit-merge-conflict

提问by Tom DeMille

I have some local files, I pull from remote branch and there are conflicts. I know that I would like to keep my local changes and ignore the remote changes causing conflicts. Is there a command I can use to in effect say "mark all conflicts as resolved, use local"?

我有一些本地文件,我从远程分支中提取并且存在冲突。我知道我想保留我的本地更改并忽略导致冲突的远程更改。是否有一个命令我可以用来实际上说“将所有冲突标记为已解决,使用本地”?

回答by Brian Campbell

git checkouthas the --oursoption to check out the version of the file that you had locally (as opposed to --theirs, which is the version that you pulled in). You can pass .to git checkoutto tell it to check out everything in the tree. Then you need to mark the conflicts as resolved, which you can do with git add, and commit your work once done:

git checkout可以--ours选择检出您在本地拥有的文件版本(而不是--theirs,这是您拉入的版本)。您可以传递.git checkout告诉它检查树中的所有内容。然后您需要将冲突标记为已解决,您可以使用git add,并在完成后提交您的工作:

git checkout --ours .  # checkout our local version of all files
git add -u             # mark all conflicted files as merged
git commit             # commit the merge

Note the .in the git checkoutcommand. That's very important, and easy to miss. git checkouthas two modes; one in which it switches branches, and one in which it checks files out of the index into the working copy (sometimes pulling them into the index from another revision first). The way it distinguishes is by whether you've passed a filename in; if you haven't passed in a filename, it tries switching branches (though if you don't pass in a branch either, it will just try checking out the current branch again), but it refuses to do so if there are modified files that that would effect. So, if you want a behavior that will overwrite existing files, you need to pass in .or a filename in order to get the second behavior from git checkout.

请注意,.git checkout命令。这很重要,而且很容易错过。git checkout有两种模式;在其中切换分支,在其中将文件从索引中检出到工作副本中(有时首先将它们从另一个修订版拉入索引)。它区分的方式是你是否传入了文件名;如果你没有传入文件名,它会尝试切换分支(尽管如果你也没有传入分支,它只会再次尝试检查当前分支),但如果有修改过的文件,它会拒绝这样做那会影响。因此,如果您想要覆盖现有文件的行为,则需要传入.或 文件名,以便从git checkout.

It's also a good habit to have, when passing in a filename, to offset it with --, such as git checkout --ours -- <filename>. If you don't do this, and the filename happens to match the name of a branch or tag, Git will think that you want to check that revision out, instead of checking that filename out, and so use the first form of the checkoutcommand.

在传入文件名时,用 来抵消它也是一个好习惯--,例如git checkout --ours -- <filename>. 如果您不这样做,并且文件名恰好与分支或标签的名称相匹配,Git 会认为您想要检出该修订版,而不是检出该文件名,因此请使用该checkout命令的第一种形式.

I'll expand a bit on how conflicts and mergingwork in Git. When you merge in someone else's code (which also happens during a pull; a pull is essentially a fetch followed by a merge), there are few possible situations.

我将稍微扩展一下Git 中的冲突和合并是如何工作的。当您合并其他人的代码时(这也发生在拉取过程中;拉取本质上是先取后合并),几乎没有可能出现的情况。

The simplest is that you're on the same revision. In this case, you're "already up to date", and nothing happens.

最简单的是你在同一个修订版上。在这种情况下,您“已经是最新的”,并且没有任何反应。

Another possibility is that their revision is simply a descendent of yours, in which case you will by default have a "fast-forward merge", in which your HEADis just updated to their commit, with no merging happening (this can be disabled if you really want to record a merge, using --no-ff).

另一种可能性是他们的修订只是你的后代,在这种情况下,默认情况下你会有一个“快进合并”,在这种情况下你HEAD只是更新到他们的提交,没有合并发生(这可以被禁用,如果你真的想记录合并,使用--no-ff)。

Then you get into the situations in which you actually need to merge two revisions. In this case, there are two possible outcomes. One is that the merge happens cleanly; all of the changes are in different files, or are in the same files but far enough apart that both sets of changes can be applied without problems. By default, when a clean merge happens, it is automatically committed, though you can disable this with --no-commitif you need to edit it beforehand (for instance, if you rename function footo bar, and someone else adds new code that calls foo, it will merge cleanly, but produce a broken tree, so you may want to clean that up as part of the merge commit in order to avoid having any broken commits).

然后您会遇到实际需要合并两个修订版的情况。在这种情况下,有两种可能的结果。一是合并发生得很干净;所有更改都在不同的文件中,或者在相同的文件中,但相距足够远,可以毫无问题地应用两组更改。默认情况下,当干净合并发生时,它会自动提交,但--no-commit如果您需要事先编辑它,您可以禁用它(例如,如果您将函数重命名foobar,并且其他人添加了调用 的新代码foo,它将干净地合并,但会产生一个损坏的树,因此您可能希望将其作为合并提交的一部分进行清理,以避免出现任何损坏的提交)。

The final possibility is that there's a real merge, and there are conflicts. In this case, Git will do as much of the merge as it can, and produce files with conflict markers (<<<<<<<, =======, and >>>>>>>) in your working copy. In the index (also known as the "staging area"; the place where files are stored by git addbefore committing them), you will have 3 versions of each file with conflicts; there is the original version of the file from the ancestor of the two branches you are merging, the version from HEAD(your side of the merge), and the version from the remote branch.

最后一种可能性是真正的合并,并且存在冲突。在这种情况下,Git会做尽可能多的合并,因为它可以和冲突标记(产生的文件<<<<<<<=======以及>>>>>>>在你的工作副本)。在索引(也称为“暂存区”;git add提交文件之前存储文件的地方)中,每个文件将有 3 个版本存在冲突;有来自您要合并的两个分支的祖先的文件的原始版本、来自HEAD(您合并的一方)的版本以及来自远程分支的版本。

In order to resolve the conflict, you can either edit the file that is in your working copy, removing the conflict markers and fixing the code up so that it works. Or, you can check out the version from one or the other sides of the merge, using git checkout --oursor git checkout --theirs. Once you have put the file into the state you want it, you indicate that you are done merging the file and it is ready to commit using git add, and then you can commit the merge with git commit.

为了解决冲突,您可以编辑工作副本中的文件,删除冲突标记并修复代码以使其正常工作。或者,您可以使用git checkout --ours或来从合并的一侧或另一侧检查版本git checkout --theirs。将文件置于所需状态后,表示已完成文件合并并准备使用 提交git add,然后可以使用 提交合并git commit

回答by VonC

Make sure of the conflict origin: if it is the result of a git merge, see Brian Campbell's answer.

确保冲突起源:如果是 a 的结果git merge,请参阅Brian Campbell回答

But if is the result of a git rebase, in order to discard remote(their) changes and use localchanges, you would have to do a:

但是如果是 a 的结果git rebase,为了丢弃远程(他们的)更改并使用本地更改,您必须执行以下操作:

git checkout --theirs -- .

See "Why is the meaning of “ours” and “theirs” reversed"" to see how oursand theirsare swapped during a rebase (because the upstreambranch is checked out).

请参阅“为什么“ ours”和“ theirs”的含义颠倒“”以了解如何在变基期间交换ourstheirs(因为已检出上游分支)。