Categories
Tech

Move Project to a new Git Repository and Keep History

Since I switched from using SVN to using Git every project/solution seems to be better off in its own repository.
Here’s how to move project1 from repository1 to repository2 with keeping all the commit history.
Prerequisite: Create an empty repository2 using your tool of preference and push it to remote, or create it on remote directly (bitbucket, github etc)

  1. Clone repository1. If you already have it cloned, clone it again in a another directory or with another name. You can use a git client for that too.
     git clone <repository1>
     cd <repository1>
  2. Once you are in the root of your repository1 use this very important command:
     git filter-branch --prune-empty --subdirectory-filter folder/of/project1 -- --all

    After this the content of folder/of/project1 will be the root of your repostory1

  3. Now remove the current remote to repository1. This is also to avoid pushing these changes to your current repository1 and mess everything up (smile)
     git remote rm origin
  4. Add the new remote of repository2 (you can also use your preferred git client)
     git remote set-url origin <https to repository2>
  5. Push your changes to new repository2
     git push -u origin master

Depending on which client you used to create your repository2 you might be missing your master branch. In this case I used push to master branch assuming it was already created.

Happy Coding
A

Leave a Reply

Your email address will not be published. Required fields are marked *

41 − = 35