如何合并来自Git分支的特定文件 [英] How to merge specific files from Git branches

查看:86
本文介绍了如何合并来自Git分支的特定文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个git分支branch1和branch2,我想将branch2中的file.py合并到branch1中的file.py中,并且只合并该文件。

实质上,我只想处理branch1中的file.py,但想要利用merge命令。什么是最好的方式来做到这一点?

解决方案

接受的答案已经提到使用 git checkout code>。现在,可以从 branch2 中的 file.py 中的内容不再适用于 BRANCH1 。这种情况将需要选择一些变化并留下其他人。因此,要完全控制使用 - patch 开关进行交互式合并:

  $ git checkout --patch branch2 file.py 

该人的交互模式部分页面为 git-add(1)解释了要使用的键:

  y  - 登台这个笨蛋
n - 不要登上这个笨蛋
q - quit;不要放弃这个庞然大物,也不要放弃任何剩余的
a - 在文件
d中放置这个庞然大物和所有后来的笨蛋 - 不要放置这个大块,也不放在文件
g中的任何后来的块 - 选择一个大块去
/ - 搜索一个与给定的正则表达式匹配的大块
j - 留下这个大块未定,看下一个未定的大块
J - 让这块大块未定,看下一个块
k - 留下这个大块未定,见前面未定的大块
K - 留下这个大块未定,见前面的块hb
s - 将当前块分割成更小的块
e - 手动编辑当前块h
? - 打印帮助

split命令尤其有用。


I have 2 git branches branch1 and branch2 and I want to merge file.py in branch2 into file.py in branch1 and only that file.

In essence I just want to work on the file.py in branch1 but want to take advantage of the merge command. What is the best way to do this?

解决方案

The accepted answer already mentions using git checkout. Now, there could be content in file.py from branch2 that no longer is applicable in branch1. Such a situation will require picking some changes and leaving others. Therefore, to have full control do an interactive merge using the --patch switch:

$ git checkout --patch branch2 file.py

The interactive mode section in the man page for git-add(1) explains the keys that are to be used:

y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk nor any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk nor any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

The split command is particularly useful.

这篇关于如何合并来自Git分支的特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆