Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git diff --name-only and copy that list

Tags:

git

People also ask

What is git diff -- name only?

The -z to with git diff --name-only means to output the list of files separated with NUL bytes instead of newlines, just in case your filenames have unusual characters in them. The -0 to xargs says to interpret standard input as a NUL-separated list of parameters.

How do I show only the names of changes in git?

git show --name-only SHA1 . you can also do: git diff --name-only HEAD@{3} HEAD@{0} for the exact commits you want to compare.

Which command shows the changes between commits?

The git diff command is commonly used to get the unstaged changes between the index and working directory. It can be also used to show changes between two arbitrary commits. To view the changes between two commits, you can provide the commit hashes.

What is the command to check diff in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.


Try 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 -z to with git diff --name-only means to output the list of files separated with NUL bytes instead of newlines, just in case your filenames have unusual characters in them.

  • The -0 to xargs says to interpret standard input as a NUL-separated list of parameters.

  • The -IREPLACE is needed since by default xargs would append the parameters to the end of the rsync command. Instead, that says to put them where the later REPLACE is. (That's a nice tip from this Server Fault answer.)

  • The -a parameter to rsync means to preserve permissions, ownership, etc. if possible. The -R means 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.)


Here's a one-liner:

List changed files & pack them as *.zip:

git diff --name-only | zip patched.zip -@

List last committed changed files & pack them as *.zip:

git diff --name-only HEAD~ HEAD | zip patched.zip -@

zip update.zip $(git diff --name-only commit commit)

#!/bin/bash
# Target directory
TARGET=/target/directory/here

for i in $(git diff --name-only)
    do
        # First create the target directory, if it doesn't exist.
        mkdir -p "$TARGET/$(dirname $i)"
        # Then copy over the file.
        cp -rf "$i" "$TARGET/$i"
    done

https://stackoverflow.com/users/79061/sebastian-paaske-t%c3%b8rholm


It works perfectly.

git diff 1526043 82a4f7d --name-only | xargs zip update.zip

git diff 1526043 82a4f7d --name-only | xargs -n 10 zip update.zip

No one has mentioned cpio which is easy to type, creates hard links, and handles spaces in filenames:

git diff --name-only $from..$to  | cpio -pld outdir