Git:如何在文件夹或zip中更改所有文件和新文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4126300/
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 09:26:17  来源:igfitidea点击:

Git: how to get all the files changed and new files in a folder or zip?

git

提问by Mr Question

As my question says, after changing files and adding new files in my repository, I normally commit files with git, but sometimes I need all the modified / changed files copied to a folder for organizing-myself reasons.

正如我的问题所说,在我的存储库中更改文件并添加新文件后,我通常使用git提交文件,但有时出于组织我自己的原因,我需要将所有修改/更改的文件复制到文件夹中。

Any option?

有什么选择吗?

回答by Cascabel

Assuming you mean you haven't yet committed, and want to package up all of the files that currently have local modifications, you can get the list of modified files with git ls-files --modified. If you want the files which were changed by the last commit, you could use git diff --name-only HEAD^. Where you go from there is up to you. Examples:

假设你的意思是你还没有提交,并且想要打包所有当前有本地修改的文件,你可以使用git ls-files --modified. 如果您想要上次提交更改的文件,您可以使用git diff --name-only HEAD^. 你从那里去哪里取决于你。例子:

zip modified-files.zip $(git ls-files --modified)
cp $(git ls-files --modified) ../modified-files

Note that this is using the versions of files in the working tree currently.

请注意,这是当前使用工作树中文件的版本。

If you have spaces in filenames, you'll have to go to a little more trouble.

如果文件名中有空格,则需要多一些麻烦。

(Of course, depending on what you're really trying to do, you might be looking for git stash, which stashes away all modified files and leaves you with a clean working tree, or you could simply want to make a temporary branch to commit to.)

(当然,根据您真正想做的事情,您可能正在寻找git stash,它会隐藏所有修改过的文件并为您留下一个干净的工作树,或者您可能只想制作一个临时分支来提交。 )

回答by Sven Marnach

To do exactly what you requested (assuming you already committed and want to create an archive of the files changed by the last commit), you could do:

要完全按照您的要求执行(假设您已经提交并希望创建上次提交更改的文件的存档),您可以执行以下操作:

git archive --format=zip HEAD `git diff HEAD^ HEAD --name-only` > a.zip

If you have removed files in a commit, to prevent a pathspec error use --diff-filter=d:

如果您在提交中删除了文件,为了防止路径规范错误,请使用--diff-filter=d

git archive --format=zip HEAD `git diff --diff-filter=d HEAD^ HEAD --name-only` > a.zip

But maybe you actually want to create a patch using:

但也许您实际上想使用以下方法创建补丁:

git diff HEAD^ HEAD > a.patch

and apply this patch where you need it using:

并在需要的地方使用此补丁:

patch -p1 < a.patch

Of course, applying a patch only works if your target directory already contains the old version of your repository.

当然,只有当您的目标目录已经包含旧版本的存储库时,才能应用补丁。

回答by Aftershock

If you use TortoiseGIt, it provides this too.
Choose the folder, in explorer
Right click,Choose menu, TortoiseGit-> Show Log.

如果您使用 TortoiseGIt,它也提供此功能。
选择文件夹,在资源管理器中
右击,选择菜单,TortoiseGit-> Show Log。

Select working directory and the last commiitted version.
Right click. Compare revisions. Select files you want to save/export.
Right Click. Export to folder. Done.

选择工作目录和最后提交的版本。
右键点击。比较修订。选择要保存/导出的文件。
右键点击。导出到文件夹。完毕。

回答by Saifullah khan

Zip the modified and newly created files in the git repository

将修改后的和新创建的文件压缩到 git 仓库中

zip mychanges.zip $({ (git ls-files --others --exclude-standard) ; (git ls-files --modified)})

回答by BRT

Assuming you don't have deleted or renamed files, this should do the trick

假设您没有删除或重命名文件,这应该可以解决问题

Copy:

复制:

cp --parents $(git status -s | egrep "M|A|AM" | rev | cut -d" " -f1 | rev) destination_folder

Zip:

压缩:

zip modified.zip $(git status -s | egrep "M|A|AM" | rev | cut -d" " -f1 | rev)

回答by user3448451

mkdir -p /c/temp/blah && cp $(git diff <your commit hash> --name-only) /c/temp/blah

I'm using Git Bash on windows.

我在 Windows 上使用 Git Bash。

回答by ChauhanTs

Here is a script which can make this process a lot easier, it will copy all changed file to used defined directory and also maintain the directory structure of code base.

这是一个可以使这个过程更容易的脚本,它将所有更改的文件复制到使用的定义目录,并维护代码库的目录结构。

run: sh scr.sh

运行:sh scr.sh

================================================

================================================

#!/bin/sh
FILES=`git ls-files --modified`
for x in $FILES
do
        prev_dir=$PWD
        echo "MY Dir = $prev_dir"
        mkdir -p /$x
        cd /$x
        cd ../
        rm -r *
        cp $prev_dir/$x ./.
        cd $prev_dir
done

================================================

================================================