Git diff --name-only 并复制该列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5605347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:34:55  来源:igfitidea点击:

Git diff --name-only and copy that list

git

提问by BazZy

I just want to get a list of changed files between two revisions, which is simple:

我只想获取两个修订版之间已更改文件的列表,这很简单:

git diff -–name-only commit1 commit2 > /path/to/my/file

But, what should I write, if I want copy all that listed files to another place? And I need completely identical directory structure for copied files.

但是,如果我想将所有列出的文件复制到另一个地方,我应该写什么?我需要完全相同的目录结构来复制文件。

For example, I have modified and added files:

例如,我修改并添加了文件:

/protected/texts/file1.txt
/protected/scripts/index.php
/public/pics/pic1.png

I want to have in /home/changes/all those changed and added files:

我想在/home/changes/所有这些更改和添加的文件中:

/home/changes/protected/texts/file1.txt
/home/changes/protected/scripts/index.php
/home/changes/public/pics/pic1.png

采纳答案by York

Try the following command, which I have tested:

尝试以下我已经测试过的命令:

$ cp -pv --parents $(git diff --name-only) DESTINATION-DIRECTORY

回答by Mark Longair

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 with git 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 -0to xargssays to interpret standard input as a NUL-separated list of parameters.

  • The -IREPLACEis needed since by default xargswould append the parameters to the end of the rsynccommand. Instead, that says to put them where the later REPLACEis. (That's a nice tip from this Server Fault answer.)

  • The -aparameter to rsyncmeans to preserve permissions, ownership, etc. if possible. The -Rmeans to use the full relative path when creating the files in the destination.

  • -zgit diff --name-only同NUL分隔的文件的方式输出列表字节而不是换行,以防万一您的文件名中都有特殊字符。

  • -0xargs说,解释标准作为输入参数的NUL分隔的列表。

  • -IREPLACE需要,因为默认xargs将参数追加到的结束rsync命令。相反,这意味着将它们放在后者所在的REPLACE位置。(这是这个 Server Fault answer的一个很好的提示。)

  • -a对参数rsync的手段维护权限,所有权等如果可能的话。的-R手段来创建在目标文件时使用完整的相对路径。

Update:if you have an old version of xargs, you'll need to use the -ioption instead of -I. (The former is deprecated in later versions of findutils.)

更新:如果您有旧版本的xargs,则需要使用该-i选项而不是-I。(前者在更高版本的findutils. 中已弃用。)

回答by kolypto

Here's a one-liner:

这是一个单行:

List changed files & pack them as *.zip:

列出更改的文件并将它们打包为 *.zip:

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

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

列出最后提交的更改文件并将它们打包为 *.zip:

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

回答by Mark

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

回答by Surjit Sidhu

#!/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

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

回答by knight2016

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

回答by hexten

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

没有人提到cpio哪个易于输入、创建硬链接并处理文件名中的空格:

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