Question

Using Git (ver 1.7), SourceTree (ver 1.5.1.0), and preferably .NET (C#)

How can I get all the committed source code from commit C --> to commit G Only? Note: I don't want to get the entire code base, just the files that were committed.

Example Commit History

A --> B --> C --> D --> E --> F --> G --> H --> I

Trying to switch to Git, but a requirement is that we need an automated process of getting the source code for our release process & documentation.

Was it helpful?

Solution 3

Thanks to Magnus and James Ehly I got what I was looking for. This will copy the files in a range of commits (C to G) to a tar file. Note: this will only get the files commited. Not the entire repository.

git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT C~..G | xargs tar -rf myTarFile.tar

git-diff-tree Manual Page

-r --> recurse into sub-trees

--no-commit-id --> git diff-tree outputs a line with the commit ID when applicable. This flag suppressed the commit ID output.

--name-only --> Show only names of changed files.

--diff-filter=ACMRT --> Select only these files. See here for full list of files

C..G --> Files in this range of commits

C~ --> Include files from Commit C. Not just files since Commit C.

| xargs tar -rf myTarFile --> outputs to tar

OTHER TIPS

Use

git diff-tree --name-only -r C~..G

to get a list of all modified files in the desired commit range (using C~ since it seems you want to include changes made in commit C rather than listing changes since C), then for each file run git show to list its contents at the last commit in the range.

for f in $(git diff-tree --name-only -r C~..G) ; do git show G:$f ; done

(Assuming a Bourne-type shell. The equivalent is possible with cmd.exe if you're on Windows.)

To extract the files into a separate directory tree instead of just dumping them to the terminal this should do:

for f in $(git diff-tree --name-only -r C~..G) ; do
  mkdir -p DESTINATION/$(dirname $f) && git show G:$f > DESTINATION/$f
done

How about git format-patch C..G? That will give you a compiled, diff'd listing of all files that were changed and the changes made to each, between the two commits. Depending on your workflow, you can kick off the git process from C#, shell out, or possibly use one of the git libraries (I'm not sure if the C# ones have support for creating patches).

Patches are pretty machine-readable and not human-unfriendly, and can be applied to another copy of the code, included in documentation, mined for general statistics, or any number of other things.

I believe you can generate a single patch, or one for each commit, depending on how detailed your process is. Git can also generate an email of the changes, to be sent off.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top