Skip to content

Instantly share code, notes, and snippets.

@yeuser
Last active September 10, 2020 13:55
Show Gist options
  • Save yeuser/b1379043ceb39dadb262897e52a48f15 to your computer and use it in GitHub Desktop.
Save yeuser/b1379043ceb39dadb262897e52a48f15 to your computer and use it in GitHub Desktop.
Merging one git repository into another repository
#!/bin/bash
if [[ $# -lt 3 ]] ; then
echo "Usage: merge-my-repos <sub_directory> <path_to_repo> <prefix_for_tags>"
exit 1
fi
DIR=$1
REPO=$2
TAG_PREFIX=$3
shopt -s extglob
if [ -d "$DIR" ]; then
echo "Specified Directory exits!"
exit 1
fi
for old_tag in `git tag`
do
git tag -d $old_tag
done
git remote add other $REPO
git fetch other --tags
for old_tag in `git tag`
do
git tag "${TAG_PREFIX}${old_tag}" $old_tag
git tag -d $old_tag
done
for branch in `git branch -r | grep -v HEAD | grep "other/" | cut -d '/' -f 2- `
do
git checkout -b temporaryBranch other/$branch
rm -rf $DIR
mkdir $DIR
mv ./!($DIR) $DIR
mv ./.!(git|idea||.) $DIR
git add .
git commit -q -m "Moved all files in branch '$branch' to subdirectory '$DIR'"
git branch -D $branch
git checkout -t origin/$branch
if [[ $? -ne 0 ]] ; then
git branch $branch
git checkout $branch
else
git merge temporaryBranch --allow-unrelated-histories --commit --no-edit
fi
git branch -D temporaryBranch
done
git checkout master
git remote remove other
git push --all -u
git push --tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment