user2453382
user2453382

Reputation:

How to add specific files from another branch to my branch without merging?

I have two branches:

-- rakib/test1
   - file1, file2, file3, file4, filex, filey
-- rakib/test2
   - file3, file4, file5, file6

I want to add file1 and file2 from rakib/test1 to rakib/test2, but a git merge rakib/test1 operation has following undesired effects:

  1. overwrites/modifies file3, file4
  2. I don't want filex, filey in the rakib/test2

Maybe I am missing something really obvious. Like is there any options in merge or add to achieve this? Or do I have to use something else? Thanks.

[EDIT] I tried git merge --no-commit --no-ff rakib/test1 from this question, it proceeds to auto-merging:

rakib.amin@rakibamin-mac:~/AndroidAuto$ git merge --no-commit --no-ff rakib/test1
Performing inexact rename detection: 100% (4347/4347), done.
Auto-merging xxxxxxxxxx
CONFLICT (add/add): Merge conflict in xxxxxx
....
Automatic merge failed; fix conflicts and then commit the result.

[EDIT] The Duplicate question requires me to perform one file at a time, I am also interested in how to do it on a directory/regex basis, as this is a regular practice for me for making PRs for old projects (The ones I haven't touched in months). If you are aware of a better practice for making clean branches while making Pull Requests with codes from old branches, please add as well, I'll be happy to learn.

Upvotes: 3

Views: 1275

Answers (1)

Venkataraman R
Venkataraman R

Reputation: 13009

You can try below command for specific file from another branch to your branch. Here, instead of merging branch, you are specifically checking out only one file from source branch to your branch. Take care, in this case, your local file is completely overwritten by the source branch file.

git checkout rakib/test2  # checkout your target branch 
git checkout rakib/test1 -- file1 # checkout only one file from source branch
git checkout rakib/test1 -- file2 # checkout only one file from source branch

This works for directories too.

git checkout rakib/test1 -- dirname

Upvotes: 4

Related Questions