git create patch from unpushed commits

2019-02-06 23:20发布

问题:

I cannot push when working remotely, so I want to create a single patch from all the commits that have not yet been pushed on my develop branch to email it. How do I do that?

回答1:

git format-patch <commit-ish> creates a patch file for every commit you made since the specified commit.

So, to export all your unpushed commits, simply put

git format-patch origin/master -o patches/

and all of them will be output into the patches/ directory.

If you want to have a single file, add --stdout:

git format-patch origin/master --stdout > patches_$(date -I).patch

This will create a file named patches_2014-10-03.patch (or another date) with all your patches in it. Beware: patch or other simple patch applications cannot cope with the produced file. It will only work with git am.


Sidenote:
An easier (and more robust) thing to do might be to keep a copy of your repo on a thumbdrive or similiar. Then set up the thumbdrive as a remote (git remote add thumb /media/thumbdrive), push your commits to it (git push thumb master) and when back at your firm, pull from the drive and push to origin.



回答2:

Instead of creating a patch, you could create a bundle (meaning a file representing a git repo, from which you will be able to pull).
In your case, an incremental bundle is needed.

git bundle create ../yourRepo.bundle" --since=x.days.ago --all

Replace x by then number of days you want to put in that bundle: don't be afraid to put "too mayn" days in that repo: someone cloning from your bundle will get only the new commits, not the one he/she already had in the local repo.

A bundle is a single file, like a patch, but can be used as a Git repo: easy to copy around and easy to use (as a regular Git repo).
If your only use is to complete a local repo with commits done from a remote repo (from which you couldn't push directly), this is easier than patches.



标签: git patch