Try the following command, which I have tested:
$ cp -pv --parents $(git diff --name-only) DESTINATION-DIRECTORY
Answer from York on Stack OverflowTry the following command, which I have tested:
$ cp -pv --parents $(git diff --name-only) DESTINATION-DIRECTORY
The following should work fine:
git diff -z --name-only commit1 commit2 | xargs -0 -IREPLACE rsync -aR REPLACE /home/changes/protected/
To explain further:
The
-zto withgit diff --name-onlymeans to output the list of files separated with NUL bytes instead of newlines, just in case your filenames have unusual characters in them.The
-0toxargssays to interpret standard input as a NUL-separated list of parameters.The
-IREPLACEis needed since by defaultxargswould append the parameters to the end of thersynccommand. Instead, that says to put them where the laterREPLACEis. (That's a nice tip from this Server Fault answer.)The
-aparameter torsyncmeans to preserve permissions, ownership, etc. if possible. The-Rmeans to use the full relative path when creating the files in the destination.
Update: if you have an old version of xargs, you'll need to use the -i option instead of -I. (The former is deprecated in later versions of findutils.)
Edit [Solved]: As per u/ppww's comment, I wasn't in the root directory of the repo, so my paths were invalid.
If I do the following command, I get a really long list of changed files. Most of which I expect, but some shouldn't have changed:
git diff --name-only myBranch1 myBranch2
For example, one of the files mentioned in the diff is src/general/Log.php
But if I use the following command to check the difference in that particular file, it just prints an empty line and exits the diff:
git diff myBranch1 myBranch2 -- src/general/Log.php
Trying to diff a directory instead of a file does the same thing.
I did however confirm that if I do a full diff across the two branches (not name-only) it does actually show the differences, but as there is a lot in there, most of which isn't what I'm looking for, it would be really tedious to view the whole diff.
What am I doing wrong?