Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?

Tags:

git

git-diff

diff

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?

like image 993
Mike_K Avatar asked Sep 07 '12 15:09

Mike_K


People also ask

How do I use a diff file?

Applying a DIFF File in the Command LineCopy the DIFF files to the root directory of your store. Open the terminal on the server or access the server remotely via SSH. Replace /path/to/cscart/root/directory with the actual path to the root directory of your store. Replace example.

How do I use the diff command in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.

What is git diff cached?

explainshell.com - git diff --cached. Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.


1 Answers

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 
like image 59
Philipp Avatar answered Sep 30 '22 20:09

Philipp