Skip to content

Instantly share code, notes, and snippets.

@tsayen
Created April 14, 2016 10:59
Show Gist options
  • 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"
@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