git 如何将更改合并到单个文件,而不是合并提交?

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

How do I merge changes to a single file, rather than merging commits?

gitmergegit-branch

提问by Isuru

I have two branches (A and B) and I want to merge a single file from branch A with a corresponding single file from Branch B.

我有两个分支(A 和 B),我想将分支 A 的单个文件与分支 B 的相应单个文件合并。

回答by loup

I came across the same problem. To be precise, I have two branches Aand Bwith the same files but a different programming interface in some files. Now the methods of file f, which is independent of the interface differences in the two branches, were changed in branch B, but the change is important for both branches. Thus, I need to merge just file fof branch Binto file fof branch A.

我遇到了同样的问题。准确地说,我有两个分支,A并且B有相同的文件,但在某些文件中有不同的编程接口。现在f独立于两个分支的接口差异的 file 的方法在 branch 中进行了更改B,但更改对两个分支都很重要。因此,我只需要将f分支B文件合并到分支文件fA

A simple command already solved the problem for me if I assume that all changes are committed in both branches Aand B:

一个简单的命令已经解决了这个问题对我来说,如果我认为所有的改变都在两个分支承诺AB

git checkout A

git checkout --patch B f

The first command switches into branch A, into where I want to merge B's version of the file f. The second command patches the file fwith fof HEADof B. You may even accept/discard single parts of the patch. Instead of Byou can specify any commit here, it does not have to be HEAD.

第一个命令切换到分支A,进入我想要合并B的文件版本f。第二个命令补丁文件ffHEADB。您甚至可以接受/丢弃补丁的单个部分。相反的B,你可以在这里指定任何承诺,也没有要HEAD

Community edit: If the file fon Bdoes not exist on Ayet, then omit the --patchoption. Otherwise, you'll get a "No Change." message.

社区编辑:如果文件fB不存在的A呢,则省略--patch选项。否则,您将得到“无变化”。信息。

回答by eggmatters

Here's what I do in these situations. It's a kludge but it works just fine for me.

这是我在这些情况下所做的。这是一个杂七杂八的东西,但对我来说效果很好。

  1. Create another branch based off of your working branch.
  2. git pull/git merge the revision (SHA1) which contains the file you want to copy. So this will merge all of your changes, but we are only using this branch to grab the one file.
  3. Fix up any Conflicts etc. investigate your file.
  4. checkout your working branch
  5. Checkout the file commited from your merge.
  6. Commit it.
  1. 根据您的工作分支创建另一个分支。
  2. git pull/git 合并包含要复制的文件的修订版 (SHA1)。所以这将合并您的所有更改,但我们仅使用此分支来获取一个文件。
  3. 修复任何冲突等。调查您的文件。
  4. 结帐你的工作分支
  5. 签出从您的合并提交的文件。
  6. 承诺吧。

I tried patching and my situation was too ugly for it. So in short it would look like this:

我尝试修补,但我的情况太糟糕了。简而言之,它看起来像这样:

Working Branch: A Experimental Branch: B (contains file.txt which has changes I want to fold in.)

工作分支:A 实验分支:B(包含我想要折叠的更改的 file.txt。)

git checkout A

Create new branch based on A:

基于 A 创建新分支:

git checkout -b tempAB

Merge B into tempAB

将 B 合并到 tempAB

git merge B

Copy the sha1 hash of the merge:

复制合并的 sha1 哈希:

git log

commit 8dad944210dfb901695975886737dc35614fa94e
Merge: ea3aec1 0f76e61
Author: matthewe <[email protected]>
Date:   Wed Oct 3 15:13:24 2012 -0700

Merge branch 'B' into tempAB

Checkout your working branch:

检查您的工作分支:

git checkout A

Checkout your fixed-up file:

检查您的固定文件:

git checkout 7e65b5a52e5f8b1979d75dffbbe4f7ee7dad5017 file.txt

And there you should have it. Commit your result.

你应该有它。提交你的结果。

回答by Mikael Kellgren

This uses git's internal difftool. Maybe a little work to do but straight forward.

这使用了 git 的内部 difftool。也许需要做一些工作,但要直截了当。

#First checkout the branch you want to merge into
git checkout <branch_to_merge_into>

#Then checkout the file from the branch you want to merge from
git checkout <branch_to_merge_from> -- <file> 

#Then you have to unstage that file to be able to use difftool
git reset HEAD <file> 

#Now use difftool to chose which lines to keep. Click on the mergebutton in difftool
git difftool

#Save the file in difftool and you should be done.

回答by Pawel Cioch

I found this approach simple and useful: How to "merge" specific files from another branch

我发现这种方法简单而有用:How to "merge" specific files from another branch

As it turns out, we're trying too hard. Our good friend git checkout is the right tool for the job.

git checkout source_branch <paths>...

We can simply give git checkout the name of the feature branch A and the paths to the specific files that we want to add to our master branch.

事实证明,我们太努力了。我们的好朋友 git checkout 是适合这项工作的工具。

git checkout source_branch <paths>...

我们可以简单地给 git checkout 提供特性分支 A 的名称以及我们想要添加到主分支的特定文件的路径。

Please read the whole article for more understanding

请阅读整篇文章以获得更多理解

回答by user1854182

The following command will (1) compare the file of the correct branch, to master (2) interactively ask you which modifications to apply.

下面的命令将 (1) 比较正确分支的文件,到 master (2) 以交互方式询问您要应用哪些修改。

git checkout --patch master

git checkout --patch master

回答by Noob

I will do it as

我会这样做

git format-patch branch_old..branch_new file

git format-patch branch_old..branch_new file

this will produce a patch for the file.

这将为该文件生成一个补丁。

Apply patch at target branch_old

在目标 branch_old 上应用补丁

git am blahblah.patch

git am blahblah.patch

回答by Salami

You can checkout the old version of the file to merge, saving it under a different name, then run whatever your merge tool is on the two files.

您可以检出要合并的旧版本文件,将其保存为不同的名称,然后在这两个文件上运行任何合并工具。

eg.

例如。

git show B:src/common/store.ts > /tmp/store.ts(where B is the branch name/commit/tag)

git show B:src/common/store.ts > /tmp/store.ts(其中 B 是分支名称/提交/标签)

meld src/common/store.ts /tmp/store.ts

meld src/common/store.ts /tmp/store.ts

回答by jbirkel

Assuming B is the current branch:

假设 B 是当前分支:

$ git diff A <file-path> > patch.tmp
$ git apply patch.tmp -R

Note that this only applies changes to the local file. You'll need to commit afterwards.

请注意,这仅适用于本地文件的更改。之后你需要提交。

回答by Soup

My edit got rejected, so I'm attaching how to handle merging changes from a remote branch here.

我的编辑被拒绝了,所以我在这里附上了如何处理来自远程分支的合并更改。

If you have to do this after an incorrect merge, you can do something like this:

如果您必须在不正确的合并后执行此操作,您可以执行以下操作:

# If you did a git pull and it broke something, do this first
# Find the one before the merge, copy the SHA1
git reflog
git reset --hard <sha1>

# Get remote updates but DONT auto merge it
git fetch github 

# Checkout to your mainline so your branch is correct.
git checkout develop 

# Make a new branch where you'll be applying matches
git checkout -b manual-merge-github-develop

# Apply your patches
git checkout --patch github/develop path/to/file
...

# Merge changes back in
git checkout develop
git merge manual-merge-github-develop # optionally add --no-ff

# You'll probably have to
git push -f # make sure you know what you're doing.