我有两个分支(A和B),我想合并来自分支A的单个文件与来自分支B的相应单个文件。
当前回答
下面的命令将 (1)比较文件的正确分支,以掌握 (2)交互式地询问您应用哪些修改。
git checkout --patch master <fn>
其他回答
我也遇到过同样的问题。准确地说,我有两个分支A和B,它们有相同的文件,但在一些文件中有不同的编程接口。现在在分支B中修改了独立于两个分支的接口差异的文件f的方法,但这一更改对两个分支都很重要。因此,我只需要将分支B的文件f合并到分支A的文件f。
一个简单的命令已经为我解决了这个问题,如果我假设所有的更改都在分支A和B中提交:
git checkout A
git checkout --patch B f
第一个命令切换到分支A,在那里我想合并B的文件f的版本。第二个命令用B的HEAD的f修补文件f。你甚至可以接受/丢弃补丁的单个部分。你可以在这里指定任何提交,而不是B,它不必是HEAD。
团体编辑:如果B上的文件f在A上还不存在,那么省略——patch选项。否则,您将收到“No Change.”消息。
我的编辑被拒绝了,所以我附上了如何从远程分支处理合并更改在这里。
如果你必须在错误的合并后这样做,你可以这样做:
# 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.
假设B是当前分支:
$ git diff A <file-path> > patch.tmp
$ git apply patch.tmp -R
注意,这只对本地文件应用更改。之后你需要做出承诺。
您可以签出要合并的文件的旧版本,将其保存在不同的名称下,然后在两个文件上运行合并工具。
eg.
B:src/common/storeTs > /tmp/store。ts(其中B是分支名称/提交/标记)
·梅尔德src /普通专卖店。ts / tmp / store ts。
这使用git的内部扩散工具。 也许有一点工作要做,但是直截了当。
#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.
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- 如何合并字典的字典?
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 在GitHub上有一个公共回购的私人分支?
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支