使用Git将两次提交之间的不同文件复制到特定文件夹 [英] Copy differing files between two commits to a specific folder using Git

查看:118
本文介绍了使用Git将两次提交之间的不同文件复制到特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何从Git存储库中获取在给定提交中修改的文件.我想将增量"存储在单独的文件夹中.我对Git还是很陌生,所以这可能是一个相对基本的问题,但是仍然..

I am trying to figure out how to get files from a Git repository that were modified in a given commit. I would like to store the 'delta' in a separate folder. I'm quite new to Git, so this might be relatively basic question, but still..

我认为我应该先获取版本的存储库:

I thought I should first get the repository at the version:

git reset --hard a02ea0d

然后列出提交的内容:

git show --pretty="" --name-only a02ea0d

那又如何呢?我可以一起查看一下,以得到一些额外的文件夹,以便与该版本的实际文件一起保存吗?

But then what? Can I plumb this together to have some extra folder as a result just with the actual files at the version?

推荐答案

解决方案

git checkout <SHA of old commit>
git diff --name-only <SHA of old commit> <SHA of newer commit> | xargs git checkout-index -f --prefix='C:\changes\'

说明

git checkout <SHA of old commit>将导致以下git checkout-index从旧提交中复制文件.

Explanation

git checkout <SHA of old commit> will cause the following git checkout-index to copy files out of the old commit.

git diff --name-only <SHA of old commit> <SHA of newer commit>将返回在旧提交与新提交之间已更改的所有文件的列表.

git diff --name-only <SHA of old commit> <SHA of newer commit> will return a list of all files that have been changed between the old and the newer commit.

xargs git checkout-index -f --prefix='C:\changes\'将获取第一个命令返回的所有文件(使用管道),并将每行用作以下git checkout-index命令的参数.

xargs git checkout-index -f --prefix='C:\changes\' will take all the files returned by the first command (by using a pipe) and will use each line as an argument for the following git checkout-index command.

此示例中使用的环境是使用Git Bash运行Windows 10的计算机.

The environment used in this example is a machine running Windows 10 using Git Bash.

  • git init在某个文件夹中
  • 创建两个文件
      包含abc
    • file1.txt 包含cba
    • file2.txt
    • git init within a certain folder
    • Creat two files
      • file1.txt containing abc
      • file2.txt containing cba
      • 包含abcabc
      • 的file1.txt 包含cbacba
      • file2.txt
      • file1.txt containing abcabc
      • file2.txt containing cbacba

      命令中指定的文件夹现在将包含所有在两次提供的提交之间更改的文件,如下所示.

      The folder specified in the command will now contain all the files that changed between the two provided commits looking like this.

      • file1.txt包含abc
      • file2.txt包含cba
      • file1.txt contains abc
      • file2.txt contains cba

      这篇关于使用Git将两次提交之间的不同文件复制到特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆