Skip to content

Instantly share code, notes, and snippets.

@zanieb
Created July 18, 2023 21:21
Show Gist options
  • Save zanieb/2bac264f0942f64a1083e5c226dbdcc6 to your computer and use it in GitHub Desktop.
Save zanieb/2bac264f0942f64a1083e5c226dbdcc6 to your computer and use it in GitHub Desktop.
Applies the contents of file(s) at the given ref (e.g. branch or commit) to the current working tree.
#!/usr/bin/env bash
#
# git-pickfile
#
# Applies the contents of file(s) at the given ref (e.g. branch or commit) to
# the current working tree. If there are no conflicts, the changes will be
# staged. If there are conflicts, the changes will need to be resolved and
# committed.
#
# Unlike `git checkout <ref> -- <file>`, this allows you to resolve conflicts
# instead of just discarding changes in the current tree.
REF=$1;
shift;
FILES="$*";
if [ -z "$FILES" ]; then
echo "Usage: git-pickfile <ref> <file> [<file> ...]"
exit 1;
fi
set -euxo pipefail
git diff $(git merge-base @ "$REF") "$REF" $FILES | git apply --3way
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment