Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you apply the same commit on a different file in a working directory?

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?

like image 234
rb612 Avatar asked Nov 25 '15 07:11

rb612


1 Answers

You have the following options:

  1. 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.

  1. 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
like image 126
dan Avatar answered Oct 21 '22 05:10

dan