Moving Git

I’ve been spending some time over the last month or so migrating our git server at work. Our old environment was starting to show the signs of age and just was not suited to having multiple developers checking in on several large projects at a time meaning we frequently ran into file permissions problems needing one of the team to ssh to the server a manually chmod the directory.

There are a lot of alternatives out there for git hosting including GitLab, Bonobo, Stash and the 800 pound guerilla of GitHub enterprise. In the end Stash from Atlassian won out, on premise hosting, great Jira integration and finally low license cost for small teams (which is in fact a donation to Room to Read).

Now we have a shiny new server all set up and running how best to migrate all our existing repositories over without losing history, tags, branches or living in a half and half state while we do so? One of the nicest things about distributed version control systems like git is that this process is much simplier than with large centralised version control systems of old, below are a couple of scripts for making this easier still.

Moving to a New Remote

The sequence below will check out everything from your existing repository and check it into your new server of choice.

Checkout your existing repository:

git clone <your old repo>

Lets get all the branches (I can’t remember where I acquired this branch script from but whoever devised it I owe you a drink):

#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch –track ${branch#remotes/origin/} $branch
done

Remove existing remote origin so we have a fall back if all goes wrong:

git remote rm origin

Create your new remote repository on the server, then its time point your local copy to the remote:

git remote set-url origin <new repository>

Push that data to your remote:

git push origin –all

 

Splitting a Repository

Now we’ve moved our repositories around we decided it was time for a bit of house keeping. For a reason no-one could remember we had a repository with two codebases in the root. While these two projects are related this decision has caused us repeated problems requiring dubious workarounds in our build scripts to handle this double root. We needed a way to split this directory into two repositories but again without losing and of the history, tags or branches.

Checkout the existing repository:

git clone <your old repo>

Lets get all the branches by running the script again:

#!/bin/bash
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
git branch –track ${branch#remotes/origin/} $branch
done

Remove existing remote origin so we have a fall back if all goes wrong:

git remote rm origin

Now we want to split one directory out and make it the root of our new repository:

git filter-branch –subdirectory-filter <directory> –tag-name-filter cat — –all

Create a new remote repository for us to push into and add it to our local repository:

git remote add origin <new remote repository>

Finally push the repository up to the new origin:

git push origin –all

 

I hope these are useful to someone, I might get round to building these up into complete scripts at some point.

One thought on “Moving Git

  1. Sergey says:

    Although this approach works fine, it does mean that your web pages needs a git rsitepoory embedded within it.a0 A better approach, and one Ia0use now is to use rsync to copy (and synchorise) the files at the remote end.Here is an example, which detects a git merge, or a commit in one of two branches (site and test)a0which then rsyncs via ssh (to site mb) into the web locations for the production and test sites respectively.#!/bin/sh## Output a version file that we can include at the bottom of the pagebranch=$(git branch | sed -n s/^\*\ //p)cd "$(git rev-parse –show-cdup)"if [ "$branch" == "site" ]; then git clean -f rsync -axq –delete –exclude=scripts/calendar –exclude=htaccess –exclude=.git –exclude=test ./ mb:public_html/static/fiif [ "$branch" == "test" ]; then git clean -f rsync -axq –delete –exclude=scripts/calendar –exclude=htaccess –exclude=.git ./ mb:public_html/static/test/static/fi

Leave a Reply

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