如何在 SourceTree 中的两次 Git 提交之间导出所有更改的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30482130/
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
How to export all changed files between two Git commits in SourceTree?
提问by Ludwig
In TortoiseGit and TortoiseSVN it is possible to export all changed files between two revisions, including the directory structure, to an external folder.
在 TortoiseGit 和 TortoiseSVN 中,可以将两个修订版之间所有更改的文件(包括目录结构)导出到外部文件夹。
Is there a way to do so in Atlassian SourceTree (for Windows)?
在 Atlassian SourceTree(适用于 Windows)中有没有办法做到这一点?
回答by Martin Tonev
Try this:
尝试这个:
git archive --output=test_zip.zip HEAD $(git diff --diff-filter=ACMRTUXB --name-only hash1 hash2)
Just replace the hash 1 and hash 2 in the example with the desired commits hash and name the zip file where you want your change to be zipped.
只需将示例中的散列 1 和散列 2 替换为所需的提交散列,并命名要压缩更改的 zip 文件。
It works for me
这个对我有用
回答by CodeWizard
From within sourcetree:
从源树中:
- Choose the first commit to start from
- Hold the shift key
- Click on the last commit you want to export
- Right click with the mouse: and choose create patch
- Set the name of the file to save and you patch is ready
- 选择第一个提交开始
- 按住shift键
- 单击要导出的最后一次提交
- 用鼠标右键单击:然后选择创建补丁
- 设置要保存的文件名,您的补丁就准备好了
Using CLI:
使用命令行:
Open terminal (Icon on the sourcetree icon bar)
打开终端(源树图标栏上的图标)
Then type:
然后输入:
git diff <sha-1>..HEAD > my_all_commits.diff
It will generate a diff file with all the changes in the given range.
它将生成一个差异文件,其中包含给定范围内的所有更改。
How to generate single patch per commit
如何为每次提交生成单个补丁
git format-patch SHA-1..SHA-1
.
This commit will create set of patches per commit with all the changes in the commit. You can then choose to use them all or only to pick those you want to apply tot he second repo.
git format-patch SHA-1..SHA-1
.
此提交将为每个提交创建一组补丁,其中包含提交中的所有更改。然后,您可以选择全部使用它们或仅选择您想要应用到第二个存储库的那些。
Good Luck.
祝你好运。
回答by Constantin Gro?
Here's a solution using 7zip with a Custom Action (Tools > Options > Custom Actions > Add
):
这是使用带有自定义操作 ( Tools > Options > Custom Actions > Add
) 的7zip 的解决方案:
Menu caption: > dist.zip
[ ] Open in a separate window
[ ] Show Full Output
[X] Run command silently
Script to run: X:\Your\path\to-Zipz.exe
Parameters: a $REPO\dist.zip $FILE
(Restart SourceTree after creation for the changes to take effect!)
This action works from the context menu for Unstaged Files and changed files in commits from the Log / History (even with multiple files / multiple commits selected) and will add those files to a "dist.zip" in the repo root. Just note that the file will not be deleted before adding files, so if you want to start from scratch, remember to delete the zip file first.
此操作在 Unstaged Files 的上下文菜单中起作用,并且在来自日志/历史记录的提交中更改文件(即使选择了多个文件/多个提交),并将这些文件添加到存储库根目录中的“dist.zip”。需要注意的是,添加文件之前不会删除文件,所以如果要从头开始,记得先删除zip文件。
This has made it so much easier to update live systems with just the files that have changed, in projects where there's no build system involved. I wonder how I was able to live and work for so long without it. :-)
在不涉及构建系统的项目中,这使得仅使用已更改的文件更新实时系统变得非常容易。我想知道没有它我怎么能生活和工作这么久。:-)
回答by Edu Wass
Here's how I did it, using a custom action + a bash script. Please note this solution is for Unix, but you should be able to replicate this into a Windows .bat script.
这是我使用自定义操作 + bash 脚本的方法。请注意,此解决方案适用于 Unix,但您应该能够将其复制到 Windows .bat 脚本中。
The script
剧本
I'm going to save this as: archive-commit.sh
我将把它保存为: archive-commit.sh
#!/bin/sh
HASH1=
HOWMANYCOMMITS=${2:-1}
HASH2=$(git rev-parse $HASH1~$HOWMANYCOMMITS)
DATE="$(date "+%m_%d_%Y_%H%M")"
FILENAME="deploy-$DATE.zip" # Change this to wherever you want to save the file
git archive -o $FILENAME $HASH2 $(git diff --diff-filter=ACMRTUXB --name-only $HASH1 $HASH2)
When calling for example:
例如调用时:
./archive-commit.sh 17f6ca1993018761f7b936d46d2d600d4ee5ef85
./archive-commit.sh 17f6ca1993018761f7b936d46d2d600d4ee5ef85
It will create a zip of that commit compared to its precedent commit.
与之前的提交相比,它将创建该提交的 zip。
The script will also take a last numeric argument, let's say:
该脚本还将采用最后一个数字参数,让我们说:
./archive-commit.sh 17f6ca1993018761f7b936d46d2d600d4ee5ef85 3
./archive-commit.sh 17f6ca1993018761f7b936d46d2d600d4ee5ef85 3
It will create a .zip of this commit + the 2 previous ones (so the total 3).
它将创建此提交的 .zip + 前 2 个(因此总共 3 个)。
The custom action in SourceTree
SourceTree 中的自定义操作
Now to integrate this into SourceTree you can create your custom actions like this (change the script target to where you've saved the script):
现在要将其集成到 SourceTree 中,您可以创建这样的自定义操作(将脚本目标更改为您保存脚本的位置):
I have to create multiple actions, because right now SourceTree isn't able to get the two hashes if you select multiple commits AFAIK.
我必须创建多个操作,因为现在如果您选择多个提交 AFAIK,SourceTree 无法获得这两个哈希值。
The Result
结果
Now you'll have a convenient way to export zips when right-clicking over a commit directly in SourceTree.
现在,当直接在 SourceTree 中右键单击提交时,您将有一种方便的方法来导出 zip。
回答by Sebastian Rodriguez
I did this code for custom action on windwos .bat
我为windwos .bat 上的自定义操作做了这个代码
single commit (Search on google) [question]:Create archive of modified files in GIT via batch file
单次提交(在 google 上搜索)[问题]:通过批处理文件在 GIT 中创建修改文件的存档
setlocal enabledelayedexpansion
set output=
for /f %%G in ('git diff-tree --no-commit-id --name-only -r %1^^') do ( set output=!output! "%%G" )
git archive -o update.zip %1 %output%
endlocal
Between to commits (Variation of the top one)
在提交之间(顶部的变化)
setlocal enabledelayedexpansion
set output=
for /f %%G in ('git diff-tree --no-commit-id --name-only -r %2^^ %1') do ( set output=!output! "%%G" )
git archive -o update.zip %1 %output%
endlocal
You have to set by parameter the $SHA
您必须通过参数设置 $SHA