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

本文详细介绍了如何在Git中仅合并两个分支间特定文件的更改,而非整个提交历史。通过具体步骤和命令示例,如使用`git checkout --patch`和`git merge-file`,帮助读者解决复杂的文件合并场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:How do I merge changes to a single file, rather than merging commits?

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


#1楼

参考:https://stackoom.com/question/jFXv/如何将更改合并到单个文件-而不是合并提交


#2楼

I came across the same problem. 我遇到了同样的问题。 To be precise, I have two branches A and B with the same files but a different programming interface in some files. 确切地说,我有两个分支AB具有相同的文件,但在某些文件中有不同的编程接口。 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. 现在,文件f的方法(与两个分支中的接口差异无关)在分支B被改变,但是这两个分支的变化很重要。 Thus, I need to merge just file f of branch B into file f of branch A . 因此,我需要合并只是文件f分支B到文件f分支的A

A simple command already solved the problem for me if I assume that all changes are committed in both branches A and 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 . 第一个命令切换到分支A ,进入我要合并文件f B版本的位置。 The second command patches the file f with f of HEAD of B . 第二个命令使用BHEADf修补文件f You may even accept/discard single parts of the patch. 您甚至可以接受/丢弃补丁的单个部分。 Instead of B you can specify any commit here, it does not have to be HEAD . 您可以在此处指定任何提交,而不是B ,它不必是HEAD

Community edit : If the file f on B does not exist on A yet, then omit the --patch option. 社区编辑 :如果文件fB不存在上A还没有,那么忽略--patch选项。 Otherwise, you'll get a "No Change." 否则,你将获得“无变化”。 message. 信息。


#3楼

Here's what I do in these situations. 这是我在这些情况下所做的。 It's a kludge but it works just fine for me. 这是一个kludge但它对我来说很好。

  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. git pull / git合并包含要复制的文件的修订版(SHA1)。 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. 承诺吧。

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.) 工作分支:实验分支: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 <matthewe@matthewe.com>
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. 提交你的结果。


#4楼

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.

#5楼

You could use: 你可以使用:

    git merge-file

Tip: https://www.kernel.org/pub/software/scm/git/docs/git-merge-file.html 提示: https//www.kernel.org/pub/software/scm/git/docs/git-merge-file.html


#6楼

This uses git's internal difftool. 这使用git的内部difftool。 Maybe a little work to do but straight forward. 也许还有一点工作要做,但要直截了当。

#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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值