0

It seems, I have changed not only my backend files on the remote branch, but also many frontend files. Now they are changed in my branch and I can't merge it to the master branch.

How can I copy files chosen by extension from one branch into another?

  1. I am on the feature branch.
  2. The numerous *.html files in the project and on the feature branch are bad.
  3. There are many useful current changes in .java and .xml on the feature branch.
  4. I want to get all *.html files from the current master branch and put them into the current project. Or to the feature branch.
Gangnus
  • 24,044
  • 16
  • 90
  • 149

2 Answers2

0

Git does not allow the partial merging of branches. That is, when you merge you need to merge all the information - you can't ignore some files and include others... (well, you can, but it's very bad practice and involves just pretending you did a merge).

That said, there are several ways you can proceed.

You can ignore the fact that your branch is on a remote - in git branches are branches, irrespective of where they are. A git pull/fetch will download your remote changes so you can work on them.

1) Simple - Ignore history

This involves taking all your changes in your feature branch, adding only those with the extension, and making a large commit with those changes on master:

  • git checkout feature
  • git reset master..feature - reset to the intersection of master and feature
  • git add -- *.txt - add all the .txt files
  • git commit
  • (git reset --hard to eliminate the files you don't want)
  • git merge

2) Complex - Preserving history

  • Use git rebase -i to interactively break each commit into 2 - those that edit the .txt files (for example), and those that don't.
  • After, use git cherry-pick to take only the commits that you want into a new branch, called feature-txt-files-only for example.
  • Merge this with your master branch.
eedrah
  • 2,245
  • 18
  • 32
  • I see that your first 4 lines overwrite the old content on the intersection by the wrong current content? – Gangnus May 09 '18 at 08:24
  • Yes, but all the 'correct' content is saved in git, so a `git reset --hard` will fix that. I've updated my answer to reflect this. – eedrah May 09 '18 at 17:51
0

I will give an example for .py files. Try to do the merge, then

find -name '*.py' -print0|xargs -0 git checkout --ours --

or

find -name '*.py' -print0|xargs -0 git checkout --theirs --

"Ours" means the local branch, and "theirs" means the remote branch (except when rebasing, when their meanings are swapped).

You can do this as many times as necessary for the file types you want to use to clobber the other changes.

Then finish the merge in the normal way.

The only problem is this might not work reliably with files that have been deleted on one of the branches but changed on the other branch. So watch out for such cases.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
  • "error: pathspec './Projects/Examples/src/test/resources/SendEmailPreCreatedHtml/html_content.html' did not match any file(s) known to git." That is what I am getting for every file. Have you tried your command yourself? – Gangnus May 09 '18 at 07:59
  • Why are those files not known to git? Are they files deleted in one branch but modified in the other? Are they new files? Or are they files that git should be, or is, ignoring? – Robin Green May 09 '18 at 08:04
  • The exact command line was: "find -name '*.html' -print0|xargs -0 git checkout origin/develop ". These files are normal html files. They are known to grid when I use GridExtension. – Gangnus May 09 '18 at 08:21
  • Huh? What is grid? What is GridExtension? – Robin Green May 09 '18 at 08:23
  • Some graphic GIT interface by which I can check by right click if a file is in git or not. I think, that is the most quick way. – Gangnus May 09 '18 at 08:26