如何使用 Git 防止自动合并?

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

How do I prevent an automerge using Git?

git

提问by marckassay

I am trying to merge a local branch into the master branch without having Git to do an automerge. I would like to “hand pick” what I would like to be merged into master.

我正在尝试将本地分支合并到主分支,而无需 Git 进行自动合并。我想“挑选”我想合并到master中的东西。

When I use Git's difftool command, I am able to diff and select what I want to be added into the master branch. But then when I do a merge, I will lose what I selected prior because Git will do an automerge. I can commit the changes into master prior to the merge, but doing so seems unnatural.

当我使用 Git 的 difftool 命令时,我能够比较并选择要添加到 master 分支中的内容。但是当我进行合并时,我会丢失我之前选择的内容,因为 Git 会进行自动合并。我可以在合并之前将更改提交到 master,但这样做似乎不自然。

And Git's mergetool is only available when there are conflicts from a merge. But if Git does an automerge then usually there aren't conflicts, so I am unable to run the mergetool command.

Git 的合并工具仅在合并发生冲突时可用。但是如果 Git 进行自动合并,那么通常不会有冲突,所以我无法运行 mergetool 命令。

Update:

更新:

I am starting to think what I am trying to accomplish is bad practice or it's just not possible. That is, to merge a topic branch and only have it merge what I need from diffing. And in an addition, to have this reflected in history. At any rate, the question I posted surfaced when experimenting with Git.

我开始认为我想要完成的是不好的做法,或者根本不可能。也就是说,要合并一个主题分支,并且只让它合并我需要的差异。此外,要在历史中反映出这一点。无论如何,我发布的问题是在试验 Git 时浮出水面的。

回答by FractalSpace

You are trying to bypass Git from getting involved in the merge process and to hand-pick each line of each modified file to be merged. This not the same as git cherry-pick. Neither will git merge --no-commit, etc. help. You will need to do:

您试图绕过 Git 参与合并过程,并手动选择要合并的每个修改文件的每一行。这不一样git cherry-pick。也不会git merge --no-commit,等等。您需要执行以下操作:

$ git checkout master
$ git difftool -t kdiff3 local-branch HEAD

In the KDiff3window, the left hand side (A) is your local-branchand the right hand side (B) is your current branch (master).

KDiff3窗口中,左侧 ( A) 是您的本地分支,右侧 ( B) 是您当前的分支 (master)

Select Merge | Merge Current Filefrom the menu (or press the colorful diamond shaped icon with the same title).

Merge | Merge Current File从菜单中选择(或按具有相同标题的彩色菱形图标)。

You will then be shown a diff and conflicts (if any) for each file. And you will have the ability to pick left or right side (Aor B), or both, and/or manually tweak the merged file.

然后,您将看到每个文件的差异和冲突(如果有)。您将能够选择左侧或右侧(AB),或两者兼而有之,和/或手动调整合并的文件。

On another note, something is telling me you have some bigger issues with your workflow.

另一方面,有些事情告诉我您的工作流程存在一些更大的问题。

回答by eckes

git merge --no-commit --no-ff <local-branch>

does it.

可以。

When you executed it, the changes from local-branchare applied but not yet staged.

当您执行它时,更改来自local-branch应用但尚未暂存。

Then, you could look at the changes to be applied and –  in case that you want to take them all  – apply them with

然后,您可以查看要应用的更改,如果您想全部使用它们,请使用

git commit -a 

Otherwise, select the files to be taken, stage them with git addand finally commit them with git commit. Restore the unwanted files then with git checkout -- filename.

否则,选择要拍摄的文件,使用它们暂存git add,最后使用git commit. 恢复不需要的文件,然后使用git checkout -- filename.

回答by andrew pate

I can see you may wish to do this if you do not trust auto-merge, because two edits in different places in a file (which are done on different branches) may not cause auto-merge to raise a conflict but may not actually work together.

我可以看到如果您不信任自动合并,您可能希望这样做,因为在文件中不同位置的两次编辑(在不同分支上完成)可能不会导致自动合并引发冲突,但实际上可能不起作用一起。

You may want to look at using a custom merge driver. This page describes how to go about it.

您可能需要考虑使用自定义合并驱动程序。此页面描述了如何去做。

Git - how to force merge conflict and manual merge on selected file

Git - 如何在所选文件上强制合并冲突和手动合并

An alternative would be to find the files that differ between the branches first, before performing the merge, then checkout the file into the appropriate branch to ensure you get a clean merge and functional code that contains either one or the other of the two edits.

另一种方法是在执行合并之前首先找到分支之间不同的文件,然后将文件签出到适当的分支,以确保您获得一个干净的合并和功能代码,其中包含两个编辑中的一个或另一个。

git diff --name-only <branch1> <branch2> 
git checkout <branch1> -- <paths>

回答by user3751385

For vim & vim-fugitive users:

对于 vim 和 vim-fugitive 用户:

Add the following to ~/.gitconfig

将以下内容添加到 ~/.gitconfig

[difftool "fugitive"]
  cmd = vimdiff $LOCAL $MERGED $REMOTE
  trustExitCode = false
  keepBackup = false

Now use Fugitive for a 3 way diff

现在使用 Fugitive 进行 3 向差异

$ git checkout master
$ git difftool -t fugitive somebranch HEAD

Note on $LOCAL, $MERGED, $REMOTE:

关于 $LOCAL、$MERGED、$REMOTE 的注意事项:

$LOCAL The file on the branch where you are merging; untouched by the merge process when shown to you

$LOCAL 要合并的分支上的文件;向您展示时未受到合并过程的影响

$MERGED The partially merged file, with conflicts; this is the only file touched by the merge process and, actually, never shown to you in meld

$MERGED 部分合并的文件,有冲突;这是合并过程中唯一涉及的文件,实际上从未在 meld 中显示给您

$REMOTE The file on the branch from where you are merging; untouched by the merge process when shown to you

$REMOTE 您要合并的分支上的文件;向您展示时未受到合并过程的影响

回答by Simon Richter

I think you want to cherry-pickindividual changes:

我认为您想挑选个人更改:

git checkout master
git log --reverse --oneline topic ^master

and then invoke git cherry-pickfor each commit you want to have in the master branch.

然后git cherry-pick为您想要在主分支中进行的每个提交调用。

If the remaining changes in the topic branch should be preserved, you may also want to rebase this branch on top of the new master:

如果应保留主题分支中的其余更改,您可能还希望将此分支重新设置在新主节点之上:

git rebase master topic