Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy differing files between two commits to a specific folder using Git

Tags:

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

Then list the content of the commit:

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?

like image 605
spiskula Avatar asked Apr 15 '18 17:04

spiskula


1 Answers

Solution

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\'

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> 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\' 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.

Example

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

  • git init within a certain folder
  • Creat two files
    • file1.txt containing abc
    • file2.txt containing cba
  • git add all files afterwards git commit them
  • Change file1.txt and file2.txt
    • file1.txt containing abcabc
    • file2.txt containing cbacba
  • git add all files afterwards git commit them
  • git log and find the SHA1 for the first and the second commit
  • Apply the solution I offered above

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

  • file1.txt contains abc
  • file2.txt contains cba
like image 192
Bee Avatar answered Oct 23 '22 04:10

Bee