Skip to content

Instantly share code, notes, and snippets.

@yarick123
Forked from iluwatar/git_deep_clone_repository.sh
Last active August 29, 2015 14:24
Show Gist options
  • Save yarick123/7346f00e1fd6db9e2bf3 to your computer and use it in GitHub Desktop.
Save yarick123/7346f00e1fd6db9e2bf3 to your computer and use it in GitHub Desktop.
#!/bin/sh
# orig: https://help.github.com/articles/changing-author-info
#WD=~/tmp/wd
#DST_DIR="$WD/my_project"
#cd "$DST_DIR"
REPO=repo.git
git clone --bare https://github.com/user/"$REPO"
cd "$REPO"
rewrite_author() {
export OLD_EMAIL="$1"
export CORRECT_NAME="$2"
export CORRECT_EMAIL="$3"
git filter-branch --env-filter '
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
}
rewrite_author old@email.addr "new name" new@email.addr
## to make more changes:
#rm -rf .git/refs/original/refs
#rewrite_author old2@email.addr "new2 name" new2@email.addr
#...
## push back:
##git push --force --tags origin 'refs/heads/*'
##cd ..
##rm -rf "$REPO"
#/bin/bash -x
# combines only MASTER branches
# look at https://gofore.com/ohjelmistokehitys/merge-multiple-git-repositories-one-retaining-history/
## Absolute Directories:
#
ORIG=~/tmp/_original_repos_git
WD=~/tmp/wd
DST_DIR=$WD/combined_project
#
########################
rm -rf "$WD"
mkdir -p $WD
git init $DST_DIR
git_deep_clone_repository() {
REPO_URL=$1
LOCAL_DIR=$2
cd $WD
git clone $REPO_URL $LOCAL_DIR
cd $LOCAL_DIR
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
git branch --track ${branch##*/} $branch
done
git remote rm origin
}
git_cleanup_repository() {
LOCAL_DIR=$1
BIGFOLDER=$2
BIGFILE=$3
cd $WD/$LOCAL_DIR
git filter-branch --tag-name-filter cat --index-filter "git rm -r --cached --ignore-unmatch $BIGFOLDER" --prune-empty -f -- --all
git filter-branch --tag-name-filter cat --index-filter "git rm --cached --ignore-unmatch $BIGFILE" --prune-empty -f -- --all
}
git_rewrite_history() {
LOCAL_DIR=$1
cd $WD/$LOCAL_DIR
filter='git ls-files -s | sed "s-\t\"*-&'$LOCAL_DIR'/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
'
git filter-branch --index-filter \
"$filter" --tag-name-filter cat -f -- --all
}
git_merge_master() {
LOCAL_DIR=$1
cd "$WD/$LOCAL_DIR"
cd "$DST_DIR"
git remote add -f "$LOCAL_DIR" "$WD/$LOCAL_DIR"
git merge -m "Merge to the combined repository." "$LOCAL_DIR/master"
git remote rm "$LOCAL_DIR"
}
git_deep_clone_repository $ORIG/proj_A.git A
git_cleanup_repository nm_Branding bigfolder somefolder/bigfile.zip
git_rewrite_history A
git_merge_master A
git_deep_clone_repository $ORIG/proj_B.git B
git_cleanup_repository nm_Navigation bigfolder somefolder/bigfile.zip
git_rewrite_history B
git_merge_master B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment