15

Lets suppose there is a central repository where commits from satellite ones are pushed some time. Developer A makes some commits on his repo while B makes some on his own too.

Now, A wants to incorporate one of B's commits into his repo (which he cannot access directly to pull).

One way is to have B create a patch and send it to A but in that case there are two problems: 1. The patch will appear as local modifications to A who have then to commit it (with its own name) 2. Once the central repo is updated the changes will conflict (being pushed by two different authors).

Is there a way to have the patch applied directly "as a commit" on local A repo so that it will appear to him as if it were pulled from the central repo (i.e. origin)?

PS: (this might not apply: see comments: is there a way to strike text until verified?) After some more investigation and testing looks like also git am < git-formatted-patch would make it so that the patch appears committed to the local master (then I hope it will be recognized as the same commit when B pushes it to the central repo). It looks like it is git apply that leaves the patch uncommitted...

a1an
  • 3,526
  • 5
  • 37
  • 57
  • 3
    `git am` will **not** result in a commit that is identical to the original. The author identification and time will be preserved, but the new commit will have a different commit time and (unless you lie to git about who you are) a different committer identity. These differences will result in the resulting commit having a different SHA1 ID. – qqx Mar 22 '13 at 21:01

2 Answers2

36

git am is what you're looking for. Ask him to commit locally and do git format-patch. This will create a patch. You can then use git am to add it to your repo.

stdcall
  • 27,613
  • 18
  • 81
  • 125
5

B could create and send a bundle rather than a patch. This allows sending commits when none of the transports available for push or fetch will work.

qqx
  • 18,947
  • 4
  • 64
  • 68
  • What if the author is on vacation and all you have is the diff? Is there a way to commit the changes to the local repo as a specific identity? – Mutant Bob Aug 08 '19 at 20:32
  • 2
    The easiest way would be to use `git apply` to modify the files then `git -c author.name="" -c author.email="" commit`. But this is quite unrelated to the original question. This will not preserve the commit ID. In addition to the differences I noted in my comment on the question, the author time would also vary from any commit created by the author. – qqx Aug 09 '19 at 14:11