简体   繁体   中英

How to merge specific file from one branch to another branch

I have two files in branchB called ComfirmPhone.java and DesignSignIn.java. I only want to merge these two code in the branchA. Here is my problem. ComfirmPhone.java is a brand new file that branchA does not exist whereas DesignSignIn.java exists in branchA already. There is little bit more code added in DesingSignIn.java branchB looking to combine the code in branchA.

Here is the command I've tried (I am in branchA now):

 git checkout --patch branchB ConfirmPhone.java 

However, the command does not detect 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.

The branchB has been commited already and both branch are up-to-date (git pull). If anyone can show me the command how to merge the ONLY two files to branchA, it would be helpful

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 . Then say

git checkout branchB -- B.txt

Presto, the file appears in the worktree. 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 ).

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. So let's say you are in branchA where there is a file A.txt ; that means the worktree also contains that version of the file 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. 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. If they cannot be automatically merged, you are left with a version of A.txt marked up with the merge conflict; edit it to resolve the conflict. Either way, now add the file, and commit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM