Created
September 14, 2023 20:49
-
-
Save ConstantinoSchillebeeckx/8e54bc195c5054d6065f66977629fd0e to your computer and use it in GitHub Desktop.
Move files or directories from one repo to another while maintaining history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Utility script to copy over a file or directory from one repo to another while maintaining history | |
# Taken from: https://savorywatt.com/2015/01/25/move-files-and-folders-between-git-repos-using-patches/ | |
# Usage: | |
# ./scripts/move_file_with_git_history.sh ~/some/repo/some/file/foo.py | |
# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425 | |
set -e | |
set -u | |
set -o pipefail | |
HERE=$(pwd) | |
PATCH_DIR=/tmp/patches | |
# split path on "repo-name" and get repo location as well as desired sync files/dir | |
SRC_REPO=$(echo $1 | awk -F\repo-name '{print $1}')repo-name | |
TO_SYNC=$(echo $1 | awk -F\repo-name/ '{print $2}') | |
echo "Syncing git history..." | |
echo "repo: $SRC_REPO" | |
echo "file/dir: $TO_SYNC" | |
# Setup a clean directory to hold the patches | |
mkdir -p $PATCH_DIR | |
rm $PATCH_DIR/*.patch | |
# Create the patches | |
cd $SRC_REPO | |
git format-patch -o $PATCH_DIR --root $TO_SYNC | |
# Apply the patches in the new repo using a 3 way merge in case of conflicts | |
# (merges from the other repo are not turned into patches). | |
# The 3way can be omitted. | |
cd $HERE | |
git am --3way $PATCH_DIR/*.patch | |
# Cleanup | |
rm $PATCH_DIR/*.patch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment