Skip to content

Instantly share code, notes, and snippets.

@JackLeo
Created March 18, 2014 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JackLeo/9621843 to your computer and use it in GitHub Desktop.
Save JackLeo/9621843 to your computer and use it in GitHub Desktop.
simple and crude script to combine few repos to one on master branch
#!/bin/bash
if [ ! $# -gt 2 ]; then
echo "Usage: ./git_combine.sh new_repo_folder repo_folder repo_folder ..."
exit
fi
if [ ! -d $1 ]; then
mkdir $1
fi
if [ ! -d $1/.git ]; then
cd $1
git init
fi
new_repo=$1
shift
while test $# -gt 0
do
if [ ! -d $1 ]; then
echo "$1 does not exist"
exit
fi
if [ ! -d $1/.git ]; then
echo "$1 is not a git repository"
exit
fi
echo "$(tput setaf 2)Moving $1$(tput sgr0)"
cd $1
git fetch --all
git checkout master
git pull origin master
git remote rm origin
for f in `ls`; do
if [ ! -d $1 ]; then
mkdir $1
fi
git mv $f $1/
done
git commit -m "Prepare to move $1 to $new_repo"
cd ../$new_repo
echo "$(tput setaf 2)Adding remotes$(tput sgr0)"
git remote add $1 ../$1
git pull $1 master
git remote rm $1
cd ..
rm -rf $1
shift
done
echo "$(tput setaf 2)Done$(tput sgr0)"
@JackLeo
Copy link
Author

JackLeo commented Mar 28, 2014

-gt is greater than
-d checks if it's directory
$1..$N is list of arguments being $0 git_combine.sh and shift does pop of $N array so after first shift $2 becomes $1 and I just run while statement checking if $# has any more arguments. $# is a number of total arguments given
$(tput setaf 2)Done$(tput sgr0) thats for colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment