11

I am very new to Git and I have a slight problem.

In SVN [this feels like an Only Fools and Horses story by uncle Albert.."during the war..."] when I wanted to update a production site with my latest changes, I'd do a diff in TSVN and export all the changed/added files between two revisions. As you can imagine, it was easy to get those files to a production site afterwards.

However, it seems like I'm unable to find an "export changed files" option in Git. I can do a diff and see the changes, I can get a list of files, but I can't actually export them. Is there a reasonable way to do this? Am I missing something simple?

Just to clarify once again, I need to export all the changes between two specific commits.

Thanks in advance!

dr Hannibal Lecter
  • 6,665
  • 4
  • 33
  • 42

2 Answers2

17

How do you want to export them? You say you already have a list; what more do you want? Supposing you're getting your list with git diff --name-only ...

git archive --output=<file> HEAD $(git diff --name-only ...)

tar -czf <file> $(git diff --name-only ...)

cp --parents $(git diff --name-only ...) <export-directory>

Something like that?

Or you could even use the diff itself - it can be applied with git apply (or even patch, I believe).

Lockszmith
  • 2,173
  • 1
  • 29
  • 43
Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 1
    The first option is working for me! Ideally, I would use the cp variant but keep the folder structure (I'm guessing it's a matter of parameters for git diff?) I can't believe it's that simple..thanks! – dr Hannibal Lecter Jun 08 '10 at 13:47
  • Just saw your edit.. Unfortunately, I can't use `git apply` or `patch`, I only have FTP access to the site :-/ – dr Hannibal Lecter Jun 08 '10 at 13:49
  • @dr Hannibal Lecter: Yeah, I figured you needed to pass around actual files. And yeah, command substitution (the `$(...)`) is one of those key things that makes everything possible (and simple) in Linux shells. – Cascabel Jun 08 '10 at 13:56
  • Sadly, I'm currently 50% stuck on Windows, but git bash and cygwin tend to make my life easier.. even though I often have no idea what I'm doing :) Thanks again for the useful info! – dr Hannibal Lecter Jun 08 '10 at 14:00
  • How would you make a diff ignoring files with only modified permissions and not modified contents? – Cesar Jul 22 '10 at 22:42
  • with every day, I love git more! THIS IS AWESOME! – eckes Feb 09 '12 at 15:30
  • 1
    to use the third option here, you would probably want to add the `--parents` parameter to CP, so that it preserves file paths in the destination – Tao Jun 21 '12 at 13:06
  • how do you perform this in TortoiseGit? – John Woo Jan 16 '17 at 06:26
  • Adding `--diff-filter=AM` to `git diff` to filter out deleted files and such may be useful – SWdV Mar 30 '23 at 11:42
2

Borrowing from few of the answers in here, here is another way to export files that are modified in the workspace:

git diff --diff-filter=ACMRT --name-only HEAD | xargs tar -rf export.tar

You might need to execute the following beforehand to add untracked files, if you need to include them in the diff:

git add *

[This works in git-bash in Windows]

Community
  • 1
  • 1
Altair7852
  • 1,226
  • 1
  • 14
  • 23
  • While executing this command in the windows slave machine, I'm getting the following error "'xargs' is not recognized as an internal or external command, operable program or batch file." – Navin prasad Jan 23 '19 at 12:45
  • This assumes you run it in git-bash environment (different from regular command line), which has access to the additional unix tools – Altair7852 Jan 23 '19 at 14:41