Skip to content

Instantly share code, notes, and snippets.

@jamischarles jamischarles/gitmove.sh
Last active Feb 21, 2019

Embed
What would you like to do?
Git move - Move file to new repo and preserve history
#!/bin/bash
# $ ./gitmove [destGitRepo] [src_file]
DEST_FOLDER=$1
SOURCE_FILE=$2
# get the earliest hash of a source file to copy
HASH=$(git log --format=%H $SOURCE_FILE | tail -1)
# echo $HASH
# create patch files for that hash range for the given file ($1)
# use the parent hash, so it includes the commit that generates the file
git format-patch -o $DEST_FOLDER/tmp_patch $HASH^..HEAD $SOURCE_FILE
# If you want to have a different path for file in dest folder change the file name BEFORE you apply them
# $ sed -i -- 's/path1/newPath1/g' path/to/patch/files
# $ sed -i -- 's/lib/lib2/g' *.patch
# move the dest file to a new path
# sed -i -- 's/lib\/utils\/auth.js/lib\/auth.js/g' *.patch
# APPLY the commit patches in the dest folder
# git am path/to/patch/files
#git am *.patch
# OPTIONAL - DON'T DO THIS if you want it in a different folder / file name in dest
# apply the patches in the destination repo
cd $DEST_FOLDER && git am tmp_patch/*.patch
# TODO:
# - how can we run multiple files through this?
# What happens when file is moved/renamed in source or target repo (after)?
# In target repo, it wipes history and blame for the file. LAME!!!!
# to follow rename in history:
# $ git log --follow ./path/to/file
# TODO: Find a solution for that.
# Is history preserved?
# What to do when file already exists in target repo?
# TODO:
# - try to delete the patch files after
# - assert am success and remove patch file on success
# - add console logs for the different actions
# - add flag to trigger rename with sed. Else assume same name and apply
# - look at tests
# - able to run against multiple files at once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.