Time to be praised as “The Guru” in your office – or how to move a folder from one Git repo to another Git repo and preserve the history
Ok. This is the 5th or 6th time that I am doing this and one of my colleagues asked me.
Could you please write down how you do it so that we could do it without you next time.
So here it is. 2 minutes read and you can move files from one git repo to another repo and move the commit history for some of the files. Commands below, but first:
Word of caution (please):
Such an enormous power under your fingers would make you and object of great attention in your company. Colleagues will sing songs about you. You would be praised as “The Guru”. Small talks before meetings will start with “Have you heard of that dude that can move files to a new git repo and keep all the history”. (believe me, it is like walking on water). Also I can not promise, but I am pretty sure you can get laid with such knowledge. I once got laid for knowing JUnit and Eclipse so… who knows.
The disadvantage is that once people learn about you and your knowledge, you will be their go to person for questions about git. Git is quite complex and many people are lazy when it comes to reading documentation, so naturally many people would start asking you questions. Mostly stupid questions, of course. Can you handle the load?
Task
Moving folder integration/processors from old_repo repo to new_repo
Commands
# Enter new repo
$ cd new_repo/
# Make sure you are up to date
$ git pull
Already up to date.
# Check remotes. Just to see what you've got
$ git remote -v
origin git@host:new_repo (fetch)
origin git@host:new_repo (push)
# You are in the new repo. Add the remote to the old_repo
$ git remote add old_repo git@host:old_repo
# Make sure the remote for old_repo is added
$ git remote -v
old_repo git@host:old_repo (fetch)
old_repo git@host:old_repo (push)
origin git@host:new_repo (fetch)
origin git@host:new_repo (push)
# Fetch from the branch of old_repo as it is now an origin
$ git fetch old_repo
...
From host:old_repo
* [new branch] dev -> old_repo/dev
# Checkout the branch from old_repo
$ git checkout --track old_repo/dev
# Remove all the paths that you don't need. Keep the paths that you do need. Bunch of magic. Better read the documentation about it.
$ git filter-branch --force --index-filter "git rm --cached -r --ignore-unmatch PATH_1 PATH_2 EVERYPATH_THAT_DOES_NOT_INCLUDE_INTEGRATION/PROCESSORS" --prune-empty --tag-name-filter cat -- --all
# Return to your master branch
$ git checkout master
# Merge the already filtered branch to your master.
$ git merge dev --allow-unrelated-histories
# Think not twice, but three times. After this there is not turning back. It's the Fame or the Shame!!!
$ git push -f
Reply
You must be logged in to post a comment.