6

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spiskula
  • 130
  • 7
  • what do you call "the delta"? Do you mean the diff output? If so that's a single file. – spectras Apr 15 '18 at 18:01
  • pretty sure you do not need the reset part to show info about old commits. anyways, do you want the whole new files, the whole old files or just the delta/patch? – mnagel Apr 15 '18 at 18:02
  • `git diff --name-only SHA1 SHA2` this will give you all the files changed from commit `SHA1` to commit `SHA2`. – Bee Apr 15 '18 at 18:04
  • I want to get the modified files only (perhaps should not call it the delta?) I dont want the whole repo at given commitid state, i want to extract the fies that were modified in the commit. I dont want just the names, i want the result to be a folder with the files that were in the commit. Thanks – spiskula Apr 15 '18 at 18:06
  • Wanna try the command I posted and check out if it gives you the files you actually want? – Bee Apr 15 '18 at 18:06
  • @mnagel - i just want the delta/patch which would be the files (and their content) that was that were modified in the commit. so i want the files in the sate they are in given commit – spiskula Apr 15 '18 at 18:13
  • 1
    Possible duplicate of [How to list all the files in a commit?](https://stackoverflow.com/questions/424071/how-to-list-all-the-files-in-a-commit) – phd Apr 15 '18 at 18:28
  • @DavidMichaelHuber - i am not looking just for file names. I want their content too. So i want to create a new folder that would contain all the files returned by the command you suggest. – spiskula Apr 15 '18 at 18:31
  • Do you want diffs or the whole contents of the modified files? I can't tell what you are after from your description or your comments. :-( – John Szakmeister Apr 15 '18 at 18:35
  • @jszakmeister whole files please. Apologies for misuse of some terms. I want content of every file that is in the commit to be copied to a separate folder – spiskula Apr 15 '18 at 18:37
  • @spiskula I suggested a solution which will copy all the files that differ between two commits into a certain directory. – Bee Apr 15 '18 at 18:45

1 Answers1

6

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
Bee
  • 1,306
  • 2
  • 10
  • 24
  • 1
    The OP seeks all file contents and the names in a certain commit. – evolutionxbox Apr 15 '18 at 18:38
  • `git checkout-index` will save the files containing differences at the given path. – Bee Apr 15 '18 at 18:39
  • I would guess that *most* of the time, you'd want to checkout the new commit instead of the old one, so that you can deploy the newly changed files somewhere. And with the new commit, you probably also want to filter out deleted files. – TTT Aug 19 '21 at 20:23