Skip to content

Instantly share code, notes, and snippets.

@r8or0pz
Last active August 29, 2015 14:06
Show Gist options
  • Save r8or0pz/c669aab828c60b83a603 to your computer and use it in GitHub Desktop.
Save r8or0pz/c669aab828c60b83a603 to your computer and use it in GitHub Desktop.
Moving directories over Git repositories keeping commits history
# clone sorce repo
git clone git://example.com/${sorce_repo}.git && cd ${sorce_repo}
# delete remote
git remote rm origin
# remove all unnecessary files and directories
git rm -r ...
# if you need to move directories just move them now
git mv $something $new_dir
# stage changes and commit, but don't push (you can't actually)
git add -- . && git commit -m 'deletion'
# do a filtering on branch
git ls-files > files-kept
git filter-branch --force --index-filter \
"git rm --ignore-unmatch --cached -qr .; cat $PWD/files-kept | xargs git reset -q \$GIT_COMMIT --" \
--prune-empty --tag-name-filter cat -- --all
# repo cleanup
rm -rf .git/refs/original/ &&\
git reflog expire --expire=now --all &&\
git gc --prune=now &&\
git gc --aggressive --prune=now
# if you need something to change after all, but repeat "repo cleanup" step introduced before
git filter-branch --tree-filter 'some commands || echo "Nothing to do"' HEAD
# change directory to destination repo
cd ../${destination_repo}
# add sorce repo as repote
git remote add ${sorce_repo} ../${sorce_repo}
# fetch, create new branch from source one, rebase it over master
git fetch ${sorce_repo} &&\
git branch ${sorce_repo}_branch remotes/${sorce_repo}/master &&\
git checkout ${sorce_repo}_branch &&\
git rebase master &&\
git push origin ${sorce_repo}_branch
# create pull request on GitHub
# once merged, delete sorce repo
git remote rm ${sorce_repo}
# something like this ;-)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment