bash 仅压缩最新提交的更改

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

Zip latest committed changes only

gitbash

提问by Marnix van Valen

Git has the very handy archivecommand which allows me to make a copy of a particular commit in a .zip archive like so:

Git 有一个非常方便的archive命令,它允许我在 .zip 存档中制作特定提交的副本,如下所示:

git archive -o ../latest.zip some-commit

This will contain the entire working tree for that commit. Usually I just need the changed files since a previous release. Currently I use this to get those files into a zip:

这将包含该提交的整个工作树。通常我只需要自上一个版本以来更改的文件。目前我使用它来将这些文件放入一个 zip 文件中:

git diff --name-only previous-commit latest-commit | zip ../changes.zip -@

This will however zip files from my working copy, which may have uncommitted changes. Is there some way to get only the changed files as they were committed directly into a zip?

然而,这将从我的工作副本中压缩文件,其中可能有未提交的更改。有没有办法只获取已更改的文件,因为它们直接提交到 zip 中?

回答by Cascabel

git archivewill accept paths as arguments. All you should need to do is:

git archive将接受路径作为参数。您需要做的就是:

git archive -o ../latest.zip some-commit $(git diff --name-only earlier-commit some-commit)

or if you have files with spaces (or other special characters) in them, use xargs:

或者,如果您的文件中有空格(或其他特殊字符),请使用 xargs:

git diff --name-only earlier-commit some-commit | xargs -d'\n' git archive -o ../latest.zip some-commit

If you don't have xargs properly installed, you could cook up an alternative:

如果您没有正确安装 xargs,您可以编写一个替代方案:

#!/bin/bash

IFS=$'\n'
files=($(git diff --name-only earlier-commit some-commit))

git archive -o ../latest.zip some-commit "${files[@]}"

Written as a shell script, but should work as a one-liner too. Alternatively, change earlier-commit, some-commit, and ../latest.zipto $1$2and $3and you've got yourself a reusable script.

编写为 shell 脚本,但也应作为单行脚本使用。或者,将earlier-commitsome-commit、 和更改../latest.zip$1$2and $3,您就拥有了一个可重用的脚本。

回答by Tim Henigan

If creating a patch is not an option, then how about something like this:

如果创建补丁不是一种选择,那么像这样的事情如何:

git stash
git checkout latest-commit
git diff --name-only previous-commit | zip ../changes.zip -@
git checkout master
git stash apply

NOTE: The git stashand git stash applyare only needed if you have working copy changes that have not been committed.

注意:git stashgit stash apply仅当您有尚未提交的工作副本更改时才需要。

NOTE 2: git checkout latest-commitwill put you on a detached head. Be sure to git checkout masterwhen you are done or you could wind up having problems with future commits.

注意 2:git checkout latest-commit会让你的头分离。确保git checkout master完成后,否则您可能会在以后的提交中遇到问题。

回答by Tim Henigan

In order to implement something useful to solve such a question for me, I've implemented a simple bash script. The script takes a revision hash and creates a tar.gz archive with all changed files including directory hierarchy. Anyone could use it or modify, I would be happy if it will help someone.

为了实现一些有用的东西来为我解决这样的问题,我实现了一个简单的 bash 脚本。该脚本采用修订哈希并创建一个 tar.gz 存档,其中包含所有更改的文件,包括目录层次结构。任何人都可以使用或修改它,如果它对某人有帮助,我会很高兴。

Just run script in the directory that is root for git repository and pass revision hash to the command line argumens.

只需在 git 存储库的根目录中运行脚本并将修订哈希传递给命令行参数。

Link to the script on hithub

链接到hithub上的脚本

回答by Denis Chevalier

I have comme up with a solution, one line and quite simple:

我想出了一个解决方案,一行很简单:

zip archive.zip $(git diff-tree --no-commit-id --name-only -r latest-commit)

You can as well use any other archiving method like tar with this method.

您也可以将任何其他归档方法(如 tar)与此方法一起使用。

回答by knittl

why not create a git patch which you can then apply to the other codebase?

为什么不创建一个可以应用于其他代码库的 git 补丁呢?

$ git format-patch HEAD^..HEAD