In our organization, we have been using Git as our version control system. And we were using it in local server. Everything was running fine. Recently we planned to move from local server to bitbucket. The reasons behind this move are:
- We have added slack as our team collaboration tool and we wanted to use webhook to get notified in slack channels about repo changes.
- We have hired people from abroad, and sometimes colleagues needed to work from home. They couldn’t connect to local network to push to Git without vpn.
- Bitbucket offers unlimited number of repositories compared to github.
We started to use bitbucket for our new projects and planned to migrate all the repositories from local server to bitbucket. The following tasks are described in bitbucket documentation to import an existing Git project into Bitbucket Server.
- Check out the repository from your existing Git host. Use the –bare parameter:
git clone --bare https://email@example.com/exampleuser/old-repository.git
- Log into Bitbucket Server and create a new repository (we’ve called it
repo.gitin this example).
- Locate the clone URL in the nav panel on the left (for example:
- Add Bitbucket Server as another remote in your local repository:
cd old-repository git remote add bitbucket https://firstname.lastname@example.org:7999/yourproject/repo.git
- Push all branches and tags to the new repository in Bitbucket Server:
git push --all bitbucket git push --tags bitbucket
- Remove your temporary local repository:
cd .. rm -rf old-repository
We have a number of repositories to move to bitbucket and we realized it would take a lot of efforts and work hours to do this manually one by one. So we planned to automate the process and we came up with a bash script. We grabbed a list of all our existing repositories in a text file
repo.txt containing one repository name per line.
repo.txt is the input parameter for the bash script.
As you can see the script is reading the
repo.txt file on line at a time, cloning the repo from local git server, creating new repository in Bitbucket using bitbucket-api, and pushing the code to newly created Bitbucket repo and finally remove the local repo from the machine where it is running.
git clone --bare and
git push --mirror commands are important as they ensure that all tags and branches are migrated over to Bitbucket.
Finally we were able to do the whole migration with this little script with no real manual effort and saving a lot of times. In the next post I will share the post-migration procedure for all the developers needed to do to point their copy of repositories to Bitbucket.
I hope this guide will be useful to you if you decide to migrate to Bitbucket. Check the second part of this migration guide here: Automate git reposotory migration from local git server to bitbucket – Part 2