2

Any tool/technique/method for creating a new file from "git diff file1 file2" comment, so that the new file will only have the output of difference.

git diff file1 file2 
3
  • Does diff file1 file2 not work? Commented Jan 30, 2021 at 17:14
  • If you want to compare two different files from the working tree then you don't event need git. You can do it with any diff you you have installed on your computer. diff is one option if your OS is based on Unix (i.e. macOS or any Linux). You can redirect the output in a file (let's say file3 by adding > file3 at the end of the line. Commented Jan 30, 2021 at 17:14
  • @axiac Thanks, was pretty easy. I tried this with git also and it works as well. Commented Jan 30, 2021 at 18:21

2 Answers 2

3

If you want to store the output of git diff file1 file2 to another file, then you can achieve the same using the command git diff file1 file2 > output_file.
If you have some data in the output file and you want to append the new difference then you can use the command git diff file1 file2 >> output_file. This command will append the output of the git diff file1 file2 command to the output_file file.

Sign up to request clarification or add additional context in comments.

Comments

0

In general, git is NOT used to get the differences between 2 files.

For that, you shall just use the diff command. And for the case you want, use > to send to output to a file, like:

diff file1 file2 >output_file 

The git diff is used to get the differences between 2 git references (commits/branches/tags/...).

Sending the output to a file is actually a patch, which can then be applied to another file. This patch may contains the differences of more than 1 file.

If you want to restrict the differences of 1 file between 2 git references, then you would do:

git diff ref1..ref2 file >diffs_of_my_file_between_ref1_and_ref2.patch 

This patch can again be used against 1 file to reproduce the changes. With git apply or simply the (non-git) patch. That is another story.

Beware: there are different diff formats. It is possible to specify this format with the options.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.