41

I have a file called test_module.c that has some differences that I want to apply to my local working copy.

I tried to create patch file from the remote by doing the following. However, git didn't complain about any errors. And didn't create any patch file either.

git format-patch master/dev_branch test/test_module.c

It is possible to create a patch of a single file, that I can apply?

(Using git version 1.7.5.4)

Garrett
  • 4,007
  • 2
  • 41
  • 59
ant2009
  • 27,094
  • 154
  • 411
  • 609

2 Answers2

47

If you give git format-patch a single revision, it will produce patches for each commit since that revision. If you see no output from that command, then I suspect that there were no changes to that file between origin/master and your current HEAD. As an alternative, you can provide a revision range (e.g. origin/master~3..origin/master) which covers the changes introduced to that file. Or, if the changes you want to produce a patch for are just contained in the single commit at the tip of origin/master, you can use the -1 parameter, as in:

git format-patch -1 origin/master test/test_module.c
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • I used the following with the hash being the last commit. git format-patch [hash] origin/master test/test_module.c However, I got these errors. What would normally cause these? error: patch failed: test/test_module.c:176 error: test/test_module.c: patch does not apply Thanks. – ant2009 Oct 25 '11 at 06:59
  • I think you may want `git format-patch [hash]..origin/master test_module.c` instead, or `origin/master` may be regarded as a path. – Mark Longair Oct 25 '11 at 07:22
  • 1
    Any idea where this is documented? The official man page doesn't seem to say anything about being to specify a file (though it does work): https://git-scm.com/docs/git-format-patch. – Ohad Schneider Feb 06 '17 at 12:27
  • Note the `-1` parameter (which says “give me the changes from this commit, as opposed to everything since this commit”) works with a hash, not just the tip of the branch. I.e. you can do `git format-patch -1 abc1234 path/to/file`. – duozmo Apr 30 '17 at 20:01
  • 2
    @OhadSchneider The `-1` option is documented in the `git-format-patch` manual: *- Prepare patches from the topmost commits.* – Lekensteyn Sep 21 '18 at 10:56
19

You can use following syntax for creating patch for single file:

git format-patch [commit_hash] [file]
Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126