3

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.

  • you can find the solution in the below link https://stackoverflow.com/questions/10935226/git-interactive-merge – arun Apr 15 '19 at 03:51
  • Sorry I just did but `git merge --no-commit --no-ff rakib/test1` seems to proceed to auto-merge anyway, throwing merge conflicts. –  Apr 15 '19 at 04:03
  • 1
    Possible duplicate of [How to get just one file from another branch](https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch) – phd Apr 15 '19 at 04:12
  • https://stackoverflow.com/search?q=%5Bgit%5D+files+another+branch – phd Apr 15 '19 at 04:12

1 Answers1

2

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
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58