首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >git命令(或简单脚本)来获取文件的前(旧)和后(新)副本

git命令(或简单脚本)来获取文件的前(旧)和后(新)副本
EN

Stack Overflow用户
提问于 2021-01-28 21:58:58
回答 1查看 23关注 0票数 0

有时,当与无法访问我的回购的人进行代码审查或协调接口时,我需要一种快速的方法来提供一些感兴趣的文件的前后副本。

这可能是因为他们无法访问我的回购,他们可能不使用git,他们可能使用其他版本控制或"diff“类型的软件,只需要使用自己的工具对文件的前后进行快速比较。

我知道git diff commit~..commit将显示提交commit引入的更改,git log --patch将显示所有提交带来的所有更改,git diff commit~..commit > patch.txt将将git diff输出存储到名为"patch.txt“的修补程序文件中,但是:

是否有git命令在感兴趣的文件的副本前后输出?

EN

回答 1

Stack Overflow用户

发布于 2021-01-28 21:58:58

我找不到一个执行此操作的git命令。因此,我使用了以下命令序列。我想出的是:

假设您的目标是在完成时同时拥有两个文件(“之前”和“之后”)--例如:发送给同事或同事,请执行以下操作。

代码语言:javascript
代码运行次数:0
运行
复制
# 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新/旧/

使用它们时,用以下方法除去它们:

代码语言:javascript
代码运行次数:0
运行
复制
rm -r files.zip old new

...and git status现在应该是干净的。

这可以/应该很容易地编写脚本。如果有人想要为它编写一个bash shell脚本,那就去做吧。否则,下次我需要做这个手术的时候我肯定会这么做的。

参考文献:

  1. How to get just one file from another branch
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65945835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档