有时,当与无法访问我的回购的人进行代码审查或协调接口时,我需要一种快速的方法来提供一些感兴趣的文件的前后副本。
这可能是因为他们无法访问我的回购,他们可能不使用git,他们可能使用其他版本控制或"diff“类型的软件,只需要使用自己的工具对文件的前后进行快速比较。
我知道git diff commit~..commit
将显示提交commit
引入的更改,git log --patch
将显示所有提交带来的所有更改,git diff commit~..commit > patch.txt
将将git diff
输出存储到名为"patch.txt“的修补程序文件中,但是:
是否有git命令在感兴趣的文件的副本前后输出?
发布于 2021-01-28 21:58:58
我找不到一个执行此操作的git
命令。因此,我使用了以下命令序列。我想出的是:
假设您的目标是在完成时同时拥有两个文件(“之前”和“之后”)--例如:发送给同事或同事,请执行以下操作。
# Create a directory named "new" and copy all files of interest into it. This assumes
# you are already on your more-recent, or "new" commit.
mkdir new
cp path/to/file1.c new
cp path/to/file2.c new
cp path/to/file3.c new
# Check out the "old" version of the files from the previous_commit_hash,
# make an "old" dir for them, and copy them into it.
git checkout previous_commit_hash -- path/to/file1.c path/to/file2.c path/to/file3.c
mkdir old
cp path/to/file1.c old
cp path/to/file2.c old
cp path/to/file3.c old
# Checking out the above files automatically stages them for commit, for some reason,
# so unstage them with `git reset`
git reset
# Check out the new files again from HEAD so you are back where you started.
# This causes the newer versions of these files to again be checked out and to
# overwrite the older versions of these files you had just checked-out above.
git checkout -- path/to/file1.c path/to/file2.c path/to/file3.c
# Package up the "new" and "old" directories into a "files.zip" zip file to send
# to a friend to look at on their end with their favorite difftool
# or merge tool, such as meld or whatever!
zip -r files.zip old new
# When done, run `git status` to see that you have 2 new directories plus the new
# .zip file.
git status
上面git status
的输出将显示您现在有这些未跟踪的文件,这是预期的:
未跟踪文件:(使用"git .“在将要提交的内容中包括) files.zip新/旧/
使用它们时,用以下方法除去它们:
rm -r files.zip old new
...and git status
现在应该是干净的。
这可以/应该很容易地编写脚本。如果有人想要为它编写一个bash
shell脚本,那就去做吧。否则,下次我需要做这个手术的时候我肯定会这么做的。
参考文献:
https://stackoverflow.com/questions/65945835
复制相似问题