简体   繁体   中英

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

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!

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM