Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving multiple directories from one git project to another, preserving their history

Tags:

git

I would like to move several directories from one git project into another one preserving their histories. The problem is, all the examples I'm seeing seem to be showing how to do extract a directory into it's own git project, preserving the history using git filter-branch. Is it possible to move these directories from one repository to another, keeping their history, if the destination repository already has other versioned files (not conflicting with the ones to be moved in any way)...?

Could somebody please show me an example of how to do this? Thanks!

like image 952
carlspring Avatar asked Jul 30 '13 16:07

carlspring


1 Answers

Answering my own question with this little script I knocked up in bash (make sure you read it first and have backed up your files):

#!/bin/bash

gitURL=$1
project=$2
srcProjectBaseDir=$3
destProjectBaseDir=$4

# Remove stale checkouts in order to do a clean clone
rm -rf ${srcProjectBaseDir}

git clone --no-hardlinks $gitURL ${srcProjectBaseDir}
cd ${srcProjectBaseDir}
git remote rm origin

# These make sure your extracted module is called as the directory's name.
# If this is of no interest to you, comment out these three lines and you
# should be alright.
mkdir temp
git add temp
git mv ${project}/ temp/

git commit -m "Refactoring: Isolated files for filter-branch."

git filter-branch --subdirectory-filter temp HEAD

git add .
git commit -m "Refactoring: Filter branched."


if [ ! -d ${destProjectBaseDir} ]; then
    mkdir ${destProjectBaseDir}
    cd ${destProjectBaseDir}
        git init
    cd ..
fi

cd ${destProjectBaseDir}
git remote add repositoryAbranch ${srcProjectBaseDir}
git pull repositoryAbranch master
git remote rm repositoryAbranch

This script is based on most of the instructions seen here (with a few additions).

like image 177
carlspring Avatar answered Oct 01 '22 01:10

carlspring