659

I have a .diff file created by a coworker, and would like to apply the changes listed in that diff file to my local branch of the exact same repository. I do not have access to that worker's pc or branch that was used to generate this diff file.

Obviously I could go line by line and retype everything, but i'd rather not subject the system to human error. What's the easiest way to do this?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Mike_K
  • 9,010
  • 5
  • 20
  • 27
  • I got in trouble using `git diff` and `git apply` because I used an alias which translated to `git diff --ignore-all-space`. This made my patch invalid. Make sure to just use bare `git diff`! – Christian Oudard Jun 02 '21 at 20:00

2 Answers2

981

Copy the diff file to the root of your repository, and then do:

git apply yourcoworkers.diff

More information about the apply command is available on its man page.

By the way: A better way to exchange whole commits by file is the combination of the commands git format-patch on the sender and then git am on the receiver, because it also transfers the authorship info and the commit message.

If the patch application fails and if the commits the diff was generated from are actually in your repo, you can use the -3 option of apply that tries to merge in the changes.

It also works with Unix pipe as follows:

git diff d892531 815a3b5 | git apply
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
Philipp
  • 67,764
  • 9
  • 118
  • 153
  • 3
    Thanks for the reply, but that caused an error saying, patch failed: filename.php:202 error:filename.php: patch does not apply. The good news is that its not the first filename in the file, so it at least would have been able to process some of the file. Any thoughts? – Mike_K Sep 07 '12 at 15:28
  • 4
    You also seem to have changes to that file which stop the patch from working. To solve this you could commit your changes, create a new branch, reset it to the commit where you and your co-worker diverged, apply the patch, commit it, and then merge the two branches. – Philipp Sep 07 '12 at 15:33
  • The file was too different from what existed in this case to use the diff file. I went ahead and did it manually, but +1 for the correct command, and i'll accept that this should have been the right answer. – Mike_K Sep 07 '12 at 16:27
  • How do you unapply the diff? – orlybg Sep 16 '14 at 17:29
  • 4
    @orlybg When you didn't commit it yet, do `git reset --hard` to return your working tree to the last commit. When you already committed it, append the revision you want to return to. – Philipp Sep 16 '14 at 17:47
  • Thank you @Philipp but what about when you had some other non-committed changes in the branch, and you just want to get rid of the ones coming from the diff? – orlybg Sep 16 '14 at 23:26
  • 5
    @orlybg Sorry, but then you are screwed. Git only creates a checkpoint you can return to when you make a commit. That's why many git guides recommend to [commit early and often](http://www.databasically.com/2011/03/14/git-commit-early-commit-often/). – Philipp Sep 17 '14 at 13:44
  • 4
    @orlybg at the very least, run `git stash` before you perform some action that you may want to reverse after. Then either way, you can bring back your stash, and commit at some later point. – maurice Sep 28 '15 at 22:57
  • 2
    @Philipp This is not strictly accurate, see `git apply --reverse`. – Resigned June 2023 May 25 '17 at 22:42
  • I feel that format-patch is much stabler than diff. I just failed 3 times using diff, but instant success using format-patch. – cwhsu Mar 07 '19 at 04:50
  • that's great! worked for me, needed to apply into master the differences from other branch just related to a specific path, so I did: `git diff master other_branch -- my_dir_path | git apply` – herrera Mar 19 '19 at 15:30
  • Works great. Although no need to put the diff file in the repo's root. It can be placed anywhere (e.g. ~/Desktop) – Jose_GD Apr 30 '19 at 20:08
  • if you when you run this command you get "xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun" you need to install XCode or XCode command line tools. https://www.studytonight.com/post/solved-mac-os-xcrun-error-invalid-active-developer-path-missing-xcrun – 1.21 gigawatts Feb 04 '21 at 07:36
  • the `--binary` flag might also be helpful if you have binary files added in the related commits – ktsangop Mar 10 '21 at 15:34
  • I got into trouble with this because I used an alias which translated to `git diff --ignore-all-space`. Make sure to just use bare `git diff`! – Christian Oudard Jun 02 '21 at 19:59
17

It seems like you can also use the patch command. Put the diff in the root of the repository and run patch from the command line.

patch -i yourcoworkers.diff

or

patch -p0 -i yourcoworkers.diff

You may need to remove the leading folder structure if they created the diff without using --no-prefix.

If so, then you can remove the parts of the folder that don't apply using:

patch -p1 -i yourcoworkers.diff

The -p(n) signifies how many parts of the folder structure to remove.

More information on creating and applying patches here.

You can also use

git apply yourcoworkers.diff --stat 

to see if the diff by default will apply any changes. It may say 0 files affected if the patch is not applied correctly (different folder structure).

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231