Question

I would like to retrieve a list of commits as patches and then try to apply them to another repo in which I stored a very close project to the one from wich I want to retrieve the patch : how can I in one command retrieve all the patches ? what is the best method (and the differences) between git format-patch and git log -p ?

Was it helpful?

Solution 2

I believe your could try to use git bundle command as well to create binary archive and then apply it to another copy of the repository.

However I would say this approach is not that flexible than using git format-patch and git am, but might be more quick if you don't want to review each patch manually.

git bundle create file.name revision..list — will create file with file.name in current folder and put in it contents of the revisions indicated.

git bundle unbundle file.name to restore revisions in different place.

OTHER TIPS

git log -p shows you the changes in reverse chronological order (most recent first). This typically isn't good for generating patches, since you want to start patching with the earliest change rather than the most recent.

git format-patch generates patches in the correct direction (patch 0001 will be the earliest change), and it saves the patches one-commit-per-file and formats them as email messages suitable for use with git am on the receiving side.

So for what you want, it probably makes the most sense to run git format-patch on one repository and then git am on the target repository:

cat *.patch | git am

If you format the hash resulting from git log prepending the number of the path with the "-" symbol then all the patchs can be generated at once in different files. Example:

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

That will cover all the filter that you can apply from git log. They are not allways consecutive commits. For example taking the patchs corresponding to a given user:

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top