Applying patches created with git log -p

I have a patch file (they all apply to a single file) generated using

git log -p file-of-interest.txt >patches.txt 

Now I want to apply these fixes in another repo, but when I try

 git am patches.txt 

I am getting the error "Patch format error."

( git apply doesn't work either). What is the correct way to apply these patches?

Edit: what I want to do is extract all the changes in one file between two commits into a set of patches, and then apply these changes in another repo. git log -p from..to -- the-file will generate diff. If it is not possible to apply a patch from git log , is it possible to make format-patch (or another command) generate patches for only one file?

+7
source share
4 answers

You need to break the patches into separate patches. You can do this manually from your git log -p output, and then use git apply to git apply them sequentially. git log -p output was not intended for git to handle ...

But the best option would be to use git format-patch to create a sequence of patch files for you (no manual separation required), then use git am to apply them all at once ...

 git format-patch -o <output_directory> <from_revision>..<to_revision> -- file-of-interest.txt 

Also note that git am expecting email-formatted fixes (e.g. generated by git format-patch , so you get "patch format detection failed"). Patches created using diff or git diff should be applied using git apply , not git am . But the git format-patch / git am workflow is more flexible and generally more reliable.

+10
source

git log will not generate a patch file this way. Use the git log to find out the commit numbers you want to compare, and use git diff instead:

 git diff 073dd505f fff28ff65 > changes.patch 
0
source

If you format the hash from git log by adding the path number with the "-" symbol, then all patches can be generated immediately in different files. Example:

 git format-patch $(git log --reverse 073dd505f..fff28ff65 --pretty="%H" -- the/needed/path | awk '{print -NR, " ", $0}') 

This will cover the whole filter that you can apply in the git log. They are not always consistent with each other. For example, taking the corrections corresponding to this user:

 git format-patch $(git log --reverse --author="John Doe" --pretty="%H" -- the/needed/path | awk '{print -NR, " ", $0}') 

The -reverse option is needed in order to give you a patch in chronological order, because git log gives most repetitions first.

0
source

You can use git diff hash1...hash2 >a.patch to create a patch file only for those commits that hash2 does not have hash1 (note 3 points ... ). This will be equivalent to git log -p hash1..hash2 (note 2 points .. ), but the output will be in a format with which you could actually fix the files.

The more common git diff hash1 hash2 >a.patch will create a patch file with all the differences between hash1 and hash2, including the commits that hash1 has, but hash2 does not.

0
source

All Articles