我想知道是否可以在git中创建补丁文件。我的意思是,如果我对几个文件进行了更改,则应根据文件名创建补丁文件。
git diff
在控制台上显示更改内容,但是否可能将输出分成不同的文件?git diff
在控制台上显示更改内容,但是否可能将输出分成不同的文件?假设你已经提交了一个修改了foo.txt
、bar.txt
和hello.txt
的提交aaa111
。
git format-patch -1 aaa111
它生成一个包含三个文件更改的补丁。
git format-patch -1 aaa111 -- foo.txt
它只生成foo.txt
更改的补丁。
git format-patch -1 aaa111 --stdout -- bar.txt > aaa111.bar.patch
它���成一个名为aaa111.bar.patch
的补丁,其中仅包括bar.txt
的更改。
更新于2022年6月4日
对于涉及二进制文件的提交,您应该在git format-patch
中添加--binary
选项。
-1
代表什么吗(以及它的作用)?我查了一下 git format-patch -h
但没找到。另外,如果我想为最近的 3 次提交创建补丁,每个提交一个文件,我该怎么做呢?谢谢。 - xpt@xpt -h
仅列出少量选项。请改用git format-patch --help
或man git format-patch
。-1
是-<n>
的一个实例,它“从最高的<n>次提交准备补丁”。如果没有-1
,它会为从aaa111
可达的每个提交创建补丁。使用-1
时,只创建aaa111
的补丁。因此,git format-patch -3
将为最近的3次提交创建补丁。请注意,-<n>
与-n
是git format-patch
的两个不同选项。 - ElpieKayHEAD~1
)所修改文件的补丁:# Enumerate the files modified on the desired commit
for file in $(git diff --name-only HEAD~1); do
# Generate the name of the patch file: replac '/' with '_'
# in the paths of the modified files and add the .patch termination
patch=${file//\//_}.patch
# Create one patch for each modified file
git diff HEAD~1 -- $file > $patch
done
HEAD~1
替换为相应的提交标识。
git diff
命令中即可。就是这么简单。 - axiac