zebediah49
zebediah49

Reputation: 7611

Is there a way of convincing Git to merge changes between files, rather than branches?

Say I have a file, foo.txt. I decide I want a bar.txt, which is like foo.txt, but with some major changes to one part of it.

If I make a branch bar, and make those changes there (probably including a rename), I can the merge the changes to foo.txt into bar.txt with `git merge.

If, on the other hand, I just copy the file such that foo and bar are different files in the same branch, is there a way of keeping [certain] changes synchronized between them?

The "correct" choice is to make the code more modular, to remove the duplicate code -- but presuming that isn't an available solution, is there a way of accomplishing this?

Upvotes: 2

Views: 64

Answers (2)

pratZ
pratZ

Reputation: 3346

Use git merge-file

git merge-file <current-file> <base-file> <other-file>

git merge-file incorporates all changes that lead from the <base-file> to <other-file> into <current-file>. The result ordinarily goes into <current-file>. git merge-file is useful for combining separate changes to an original. Suppose <base-file> is the original, and both <current-file> and <other-file> are modifications of <base-file>, then git merge-file combines both changes.

In your case you can create an empty and the do a git merge-file,

git merge-file foo.txt empty.txt bar.txt

foo.txt is your merged file

Upvotes: 2

No, this is not possible, unless you have a base version of your file before the diverged.

Git always uses three file states in a merge: a base file C, a version A and a version B. It then effectively adds the diffs between C and A, and C and B, replaying them both on file C. Unless you have three different input files, the output will always be either file A or file B without any changes.

Upvotes: 2

Related Questions