Skip to content

Synchronising Gitbare and Bitbucket

If a project is using both Gitbare and Bitbucket at the same time then we can sync the commits and changes between each repository. We do this by using a post receive hook on Gitbare to fetch commits down and push changes up.

Initial Setup and Sync

There is a script in the home directory that does all of the heavy lifting and this needs calling from the post receive hook. To create the hook, create a new file in the hooks directory of the bare repository you want to sync called post-receive.

Use chmod to add executable permission chmod u+x post-receieve. This will allow git to execute it. Inside the file, paste the following snippet.

#!/usr/bin/env bash

bitbucketSlug="git@bitbucket.org:user/my-client-repo.git";

bash /home/ec/bitbucket-sync.bash "$bitbucketSlug"

Replace the contents of bitbucketSlug with the remote value you would use with git remote in your normal non-bare repository. Now when you push, you should see the commits being received by Bitbucket.

If you do not want your pushes to be delayed, you can background the bitbucket sync like this:

#!/usr/bin/env bash

echo "Kicking off backgrounded bitbucket mirror"

bitbucketSlug="git@bitbucket.org:user/my-client-repo.git";

nohup bash /home/ec/bitbucket-sync.bash "$bitbucketSlug"  &> /dev/null &

Initial Sync

Warning

When doing the initial synchronisation, ensure that any Slack notifications are disabled as there will be a lot of noise generated from the initial sync

Warning

If you haven't used our gitBare previously, or stopped using it at some point, make sure to make it up to date by executing these commands in your project root: git push REMOTE '*:*' git push REMOTE --all. Where REMOTE is your remote repository name.

In the gitBare repository root, run the following to do the initial synchronisation.

git fetch <bitbucketSlug> '*:*';
bash hooks/post-receieve

Replace the bitbucket slug with the actual remote for your repository, this will pull in all the branches on Bitbucket but not on gitbare and synchronise every thing up.