Skip to content

Instantly share code, notes, and snippets.

@pwalkr
Created February 28, 2025 21:33
Show Gist options
  • Save pwalkr/b1ab917f0226ca0b1b50b464fbcebc2d to your computer and use it in GitHub Desktop.
Save pwalkr/b1ab917f0226ca0b1b50b464fbcebc2d to your computer and use it in GitHub Desktop.
Apply git patches to alternative folders in a repository
#!/bin/sh
# This script applies a git commit cherry-pick style from one path to another.
# A patch is generated relative to the source folder, and reapplied on the
# destination folder.
#
# This is useful for promotion in infrastructure-as-code (IaC) implementations
# using folder-per-environment layouts.
#
# TODO: apply backlink to source commit in message, and potentially message
# replacement e.g. to replace environmental keywords.
src="$1"
dst="$2"
ref="$3"
#set -ex
cd "$src"
git format-patch -1 "$ref" --relative --stdout \
| git am --directory "$dst"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment