简体   繁体   English

如何将特定文件从一个分支合并到另一个分支

[英]How to merge specific file from one branch to another branch

I have two files in branchB called ComfirmPhone.java and DesignSignIn.java.我在分支 B 中有两个文件,名为 ComfirmPhone.java 和 DesignSignIn.java。 I only want to merge these two code in the branchA.我只想在分支A中合并这两个代码。 Here is my problem.这是我的问题。 ComfirmPhone.java is a brand new file that branchA does not exist whereas DesignSignIn.java exists in branchA already. ComfirmPhone.java 是一个全新的文件,branchA 不存在,而 DesignSignIn.java 已经存在于 branchA 中。 There is little bit more code added in DesingSignIn.java branchB looking to combine the code in branchA.在 DesingSignIn.java 分支 B 中添加了更多代码,希望合并分支 A 中的代码。

Here is the command I've tried (I am in branchA now):这是我尝试过的命令(我现在在 branchA 中):

 git checkout --patch branchB ConfirmPhone.java 

However, the command does not detect ConfirmPhone.java.但是,该命令未检测到 ConfirmPhone.java。 and also I've tried:我也试过:

 git merge --no-ff --no-commit branchB

It does merge, but it does not specify two files (ComfirmPhone.java and DesignSignIn.java) I am looking for.它确实合并,但没有指定我正在寻找的两个文件(ComfirmPhone.java 和 DesignSignIn.java)。

The branchB has been commited already and both branch are up-to-date (git pull). branchB 已经提交,并且两个分支都是最新的(git pull)。 If anyone can show me the command how to merge the ONLY two files to branchA, it would be helpful如果有人可以向我展示如何将仅有的两个文件合并到分支 A 的命令,那将很有帮助

Every commit is a collection of all your files.每个提交都是您所有文件的集合。 Any file in any commit can be copied out into the worktree.任何提交中的任何文件都可以复制到工作树中。 A branch name is merely the name of a commit.分支名称只是提交的名称。

The case where the file doesn't exist is thus straightforward.文件不存在的情况因此很简单。 Let's say you are in branchA where there is no file B.txt , and you happen to know that branchB contains a file B.txt and you wish that version of that file were present in branchA .假设您在没有文件B.txtbranchA中,并且您碰巧知道branchB包含文件B.txt并且您希望该文件的版本存在于branchA中。 Then say然后说

git checkout branchB -- B.txt

Presto, the file appears in the worktree. Presto,文件出现在工作树中。 Of course, it is now a "new" file like any other "new" file;当然,它现在是一个“新”文件,就像任何其他“新”文件一样; it is not "part" of branchA until you add it and commit (while in branchA ).在您addcommit (在branchA中)之前,它不是branchA的“一部分”。

The case where the file does exist in both branches is similar but now if you don't want to overwrite the worktree version completely you want a --merge checkout.文件确实存在于两个分支中的情况类似,但现在如果您不想完全覆盖工作树版本,则需要--merge签出。 So let's say you are in branchA where there is a file A.txt ;因此,假设您在branchA中,其中有一个文件A.txt that means the worktree also contains that version of the file A.txt .这意味着工作树还包含该版本的文件A.txt And let's say you happen to know that branchB also contains a file A.txt and you want to merge that version of the file into this version of the file.假设您碰巧知道branchB还包含一个文件A.txt并且您想将该文件版本合并到该文件版本中。 Then say然后说

git checkout --merge branchB -- A.txt

The result is that the two versions of A.txt are merged in the worktree, if they can be automatically merged.结果是A.txt的两个版本在工作树中合并,如果它们可以自动合并的话。 If they cannot be automatically merged, you are left with a version of A.txt marked up with the merge conflict;如果它们不能自动合并,则留下一个带有合并冲突标记的A.txt版本; edit it to resolve the conflict.编辑它以解决冲突。 Either way, now add the file, and commit .无论哪种方式,现在add文件,并commit .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个分支中的特定文件合并到Git中的另一个分支中 - How can I merge a specific file from one branch into another branch in Git 如何将特定文件从一个分支合并到主分支? - How to merge a specific file from one branch to master? 如何合并来自另一个分支的一个文件的先前提交 - How to merge a previous commit of one file from another branch 如何将所有文件从一个分支合并到另一个分支,但一个文件除外 - how to merge all files from one branch to another branch but except one file 如何将文件从一个分支的目录合并到另一个分支? - How to merge files from one branch's directory into another branch? 使用 git,如何从另一个分支中的文件(绕过合并)强制覆盖一个分支中的文件并提交? - With git, how to force overwrite a file in one branch, from a file in another branch (bypass merge), and commit? 特定的增量需要在git上从一个分支合并到另一个分支 - specific delta need to merge on git from one branch to another Git合并(从一个分支合并到另一个分支) - Git Merge (merge from one branch to another) 在git中将特定文件的更改从一个分支合并到另一个分支 - Merge changes to specific file from one branch to other in git 将提交从一个分支合并到另一个分支 - merge commits from one branch into another one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM