Git Merge by file type

Viewed 819

I have two git branches, master and mybranch. Mybranch has all the correct *.json files, and master has all the other file types correct. How do I merge them?

There are many, many merge conflicts in all the files. If I could somehow merge by file type, I wouldn't have to go through all the merge files.

2 Answers

git checkout has options --ours and --theirs to get the respective versions during a merge. So you do something like

git checkout master
git merge mybranch
git checkout --theirs -- *.json
git checkout --ours -- <the-other-files>
git add .
git commit

where you'll have to play with the shell a bit to list the files you want to take from one or the other branch. After you check out the desired versions to the work tree, add should clear the conflict records.

Remember, --ours is the branch that you passed as argument to initial checkout and --theirs is the branch that you passed as argument to merge.

Also, before you call add, you can change your mind with the --ours and --theirs, because the index still contains all the versions. So you should be actually able to do git checkout --ours -- . and then git checkout --theirs *.json.

Given you want to merge Mybranch in master, general advice would be:

git checkout master
git merge Mybranch 

If there are merge conflicts - resolve them.

It is recommended to perform often a rebase onto master or a merge to the local branch in order to avoid a huge amount of merge conflicts when you are ready to bring changes from your local branch into master.

UPD: The fastest solution would be using diff patch:

  1. Checkout master branch.

  2. Create a diff patch only for JSON files.

    git diff master Mybranch *.json > json.diff

  3. Apply diff.

    git apply json.diff

  4. Commit changes.

Notes: If there are conflicts in working directory - patch won't apply. Might be helpful to make for several or every JSON its own diff in order to localize problematic json file. For conflicting diff you could open a diff file and edit it before applying. Either way - if there are conflicts, you must solve them.

Related