Today I learned about creating and applying patches with git format-patch
and git am
.
I was working on a remote JupyterLab instance, happily git commit
'ing some stuff,
only to figure out too late that I lacked git push permissions there.
As it was a throwaway remote instance,
I didn't want to spend my time on setting up the necessary SSH pubkey configuration. Instead, I took the poor man's route to transport commits:
-
"Export" the commits as a patch file:
git format-patch origin/main
This created a patch file for each commit I did on top op
origin/main
, following an auto-increment prefix filename pattern (e.g.0001-Fix-wibble.patch
). -
Download the patch files to my local machine (just a right-click away in JupyterLab).
-
Apply the patch files to my local clone of the repo, e.g.
git am 00*.patch
This locally recreated the git history I created remotely, ready to push.
Note that it is also possible to achieve the same with git diff
and git apply
.
However, with git format-patch
and git am
more metadata about
the original commits seems to be preserved by default.
I just scratch the surface here. Look elsewhere for more in-depth discussion and fine-tuning options of these git subcommands.