Skip to content

Instantly share code, notes, and snippets.

@tsayen
Created April 14, 2016 10:59
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsayen/f1c1c4d62d4fda77abf1586bd39f9b74 to your computer and use it in GitHub Desktop.
Save tsayen/f1c1c4d62d4fda77abf1586bd39f9b74 to your computer and use it in GitHub Desktop.
#! /bin/bash
# Usage:
# ./git-move.sh path/to/file/or/dir path/to/destination/repo
echo "creating patch for path ${1}"
git log --name-only --pretty="format:" --follow "${1}" \
| sort -u | \
xargs git log --pretty=email --patch-with-stat --reverse --full-index --binary -m --first-parent -- > "${2}/_patch_" \
&& echo "moving to destination repo at ${2}" \
&& cd "${2}" \
&& echo "applying patch" \
&& git am --committer-date-is-author-date < _patch_ \
&& echo "OK"
@simonmichael
Copy link

Ditto, this was the best of the solutions I found today. I had one problem: diff.context was set to 0 in my ~/.gitconfig. This script needs it to be non-zero. (Otherwise git apply fails without the --unidiff-zero flag, which isn't available with git am).

@n8zach
Copy link

n8zach commented Apr 7, 2020

This worked beautifully. Thank you!

@dflock
Copy link

dflock commented Jan 16, 2021

I ended up using some of this, but in this form that supports multiple files, as well a deleted/renamed files:

cd repository

git log --pretty=email --patch-with-stat --reverse --full-index --binary -m --first-parent -- ./folder ./file1.txt ./file2.txt ./deleted_file.txt > ~/tmp/hist.patch

cd ../another_repository
git am --committer-date-is-author-date < ~/tmp/hist.patch 

@wesgarland
Copy link

Thanks, tsayen. I expanded on this a bit to meet my local needs -- https://github.com/wesgarland/git-relocate

@ob-ivan
Copy link

ob-ivan commented May 14, 2021

I liked the @dflock's version, but I wanted to keep the detailed history (not just merges), so I removed --first-parent and added --no-merges to the git-log invocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment