Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
moves a folder from one git repo into another repo with history intact
#!/usr/bin/env bash
# Usage:
# ./git_move.sh git@repo_site.com:/my_repo.git origin/folder/path/ /destination/repo/path/ new/folder/path/
repo=$1
folder=$2
dest_repo=$3
dest_folder=$4
clone_folder='__git_clone_repo__'
echo 'cloning repo...'
#clone repo
git clone $repo $clone_folder
#move to new folder and get path for later
cd $clone_folder
#get old branch with no trailing slash
old_branch_path=`pwd | sed "s,/$,,"`
echo 'filtering branch...'
#removes everything but the folder we need
git filter-branch --subdirectory-filter $folder -- -- all
echo 'making destination folder...'
#make destination folder recursive
base=''
base_dest=''
count=0
IFS='/' read -ra ADDR <<< "$dest_folder"
for i in "${ADDR[@]}"; do
if [["$count" = "0"]]
then
base_dest=$i
fi
count=$(($count+1))
base=$base''$i'/'
mkdir $base
done
echo 'moving files into destination folder in old repo...'
#move all one by one or it complains
for file in `ls`; do
#move it all
if [[ "$file" != "$base_dest" ]]
then
git mv $file $dest_folder
fi
done
#commit to make the history work
git commit -m "removed all data but folder to move"
#move to destination _repo_ folder
cd $dest_repo
echo 'adding local remote to new repo...'
git remote add _repo_1 $old_branch_path
#fetch the remote source, create a branch and merge it with the destination repository in usual way
git fetch _repo_1
git branch _repo_1 remotes/_repo_1/master
git merge _repo_1
echo 'removing remote and dummy branch...'
git remote rm _repo_1
git branch -d _repo_1
echo 'cleaning up temp repo...'
chmod 0777 $old_branch_path
rm -r -f $old_branch_path
echo 'Done. Remeber to commit and push the changes to the destination branch.'
@r2n2

This comment has been minimized.

Copy link

commented Apr 9, 2014

Heh heh - don't run this script w/o args. I did (dumb me I know!) and lost everything. There were some repos in sub-dirs but everything is gone. Fortunately I have all the "important" scripts in remote repos. Some stuff is gone forever; but, lesson learned. I do feel pretty stupid!

@yikdoj

This comment has been minimized.

Copy link

commented Nov 7, 2014

Use before read comment !!
rm -fr in the end , just be aware

@swthomas55

This comment has been minimized.

Copy link

commented Jan 12, 2016

I suggest adding, at the beginning of the script, a quick argument check

if [ $# -lt 4 ] ; then
echo "Usage: $0 git@repo_site.com:/my_repo.git origin/folder/path/ /destination/repo/path/ new/folder/path/"
exit
fi

Which I did in my fork of the gist. Also my version of bash didn't like the if [[ ]] syntax without spaces around the [[ and ]].

@wilsonmar

This comment has been minimized.

Copy link

commented Feb 4, 2016

I created a shell script to create the data (git_move_setup.sh) and a shell script containing this script (git_move.sh) at https://github.com/wilsonmar/git-utilities. After I ran the scripts, I noticed that history associated with folders and files from the origin repo is not carried forward to the destination folder. Would you mind taking a look at them with me, since the title of this gist contains "history intact".

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.