For example, let's say I have two files, A and B. I created B as a copy of A but I want them to be very similar - I only want a couple lines different in B. Is there a way in Git to sync these changes in any way, or a more efficient way to accomplish this task?
Asked
Active
Viewed 1,206 times
7
-
Perhaps you could clarify your question a bit. It is unclear to me what you are trying to accomplish. Perhaps a quick example would clear things up. – davethebrave Nov 25 '15 at 07:28
-
I'd rather consider a different design, for it looks to me that you suffer from duplication of code. Unfortunately, there exists no tool that is able to help in dealing with a poor design. – skypjack Nov 25 '15 at 07:28
-
yeah, I was thinking this was a design issue. But let's say I have a method in both files with most of the code the same. However, I make changes in one file that I want to change in the other file. – rb612 Nov 25 '15 at 07:31
-
@rb612 this is a bit off-topic and I don't know what language you are using, but consider something like the template method pattern to deal with such a problem. – skypjack Nov 25 '15 at 07:37
3 Answers
3
You have the following options:
Use apply-patch-to-file. Where you can generate the patch using:
git format-patch HEAD^
And then apply using:
apply-patch-to-file -i ~/patches/0001-my-test-commit.patch
You will be prompted and asked on what files the patch should be applied.
Use directly the
patch
command, by creating the diff like:git diff HEAD^ -- hello.test > ~/patch_file
Then apply the diff to a different file:
patch -p1 another_path/subdir/different_file.test ~/patch_file

dan
- 13,132
- 3
- 38
- 49
0
You can store the diff changes in a file and patch A and B individually using this patch file.

Prasanna
- 1,184
- 8
- 12
0
If you can generate that difference, then you could:
- version
A
- don't version
B
at all (and add it to your.gitignore
actually) - add a smudge script which, when reading A content
git checkout
would generateB
.
This is called a content filter driver, using a .gitattributes
declaration.
(image from "Customizing Git - Git Attributes", from "Pro Git book")