Collaborating Using Bare Git Repos in Corporate Environments

Using Git in corporate environments is like a breath of fresh air compared to the usual enterprise options. The normal tools available include Subversion, TFS, VSS, or nothing if you are really unlucky.

Once you start using Git for your source code control you get a bucket load of potential benefits, including but not limited to:

  • Speed
    • Git is fast
  • Works Offline
    • Git works offline, or where the network is slow. You have the entire repo on your local machine so most of your operations are very quick, even on 'classic' hardware. Working on the train or on secure offline environments is possible.
  • Backups
    • As long as you have more than one copy of the repo (see below) you get built in backups. Never again wonder whether the corporate server is backed up just after the shared central server goes belly up.
  • Branching and Merging Work
    • Amazingly branching and merging just works, so easily, that you end up using them all the time.
  • Easy Collaboration
    • Git is built for collaboration and you can easily work well with others using a shared bare repo on a network share, without needing an actual central source code server (a la VSS, TFS, SVN). In fact you can even use a usb chip as the central repo if needed.

To gain some of the distributed benefits (Collaboration and Backups) a good pattern to use is a shared bare repo.

Assuming two developers and an application name of WidgetBlock here's a possible working layout:

Dev1 (local repo) - c:\dev\WidgetBlock  
Dev2 (local repo) - d:\mycode\WidgetBlock  
Network Drive (shared bare repo) \\server\share\repos\WidgetBlock.git  

So Dev1 keeps their local git repo in the c:\dev\WidgetBlock directory, Dev2 does similar but under their d:\mycode\WidgetBlock directory. They both push and pull code to the central repo which is located on a server at \\server\share\repos\WidgetBlock.git

The key to the collaborating is setting up of the bare repo on the server.

Pre-existing Single Repo

If you are starting out with an existing git repo and need to share via a network share here is what you do. e.g. Assume Dev1 has a repo located on their dev machine at c:\dev\WidgetBlock and we are using Git Bash on Windows.

Make sure the local repo is up to date

git status then git add, git commit

Create bare repo on central share

Create a mapped drive to your network share repo parent folder, e.g. h: maps to \\server\share\repos\

Open the Git Bash prompt.
Navigate to the mapped repo parent folder and create the folder and bare repo. Note a bare repo is a git repo without a version of the actual directory structure, it just has the blobs and file manifests of the git structure.

This enables the repo to be pushed to and pulled from by multiple parties, and is generally used as a central location. Note the .git folder name is a convention that indicates a bare repo.

cd h:  
mkdir WidgetBlock.git  
cd WidgetBlock.git  
git init --bare  

You should now have a bare repo located in your h:/WidgetBlock.git directory.

Now we need to push the full repo from Dev1's machine to the network hosted bare repo.

Navigate back to the local version:

cd c:/dev/WidgetBlock  

To push to our new shared repo we need to add a remote location to the local repo.

git remote add origin h:/WidgetBlock.git  

Check what is setup by:

git remote -v  

Which should show:

origin h:/WidgetBlock.git (fetch)  
origin h:/WidgetBlock.git (push)  

We are now ready to push our repo to the central bare one.

git push -u origin --all  

Push pushes you local repo to the origin version, -u sets the remote as our upstream server, and finally --all uploads all branches.

You're all set now to push and pull which you would do on the master branch thus:

git pull origin master  
git push origin master  

Dev2 can now clone from the central repo to their local machine. Assuming they map the remote repo parent folder to h: and using Bash Prompt:

cd d:/mycode  
git clone h:/WidgetBlock.git  

This should clone the remote repo down to a new folder d:\mycode\WidgetBlock

So now we have 2 devs, able to collaborate using the central bare repo, with 3 versions of the repo distributed across the machines. As more devs join the team simply get them to clone from the central repo.

If you find you would benefit from a centralised git server with commenting, pull requests etc similar to github then there are a number of great options available including:

  • Github - the daddy of git social coding, offers public, private hosted and on premise solutions. Used for almost all of the top open source projects it generally leads the field for git based collaboration. Private repos cost money, but not a lot for what you get.
  • Gitlab - the open source option, very much like Github, quite 'entertaining' to install so good if you are a wizard linux admin. Great price once you are up and running and fully under your control so good for hosting behind the firewall, or on the internet on your own servers.
  • Atlassian Stash - like all Atlassian products very nice indeed. Great pricing for small teams, then gets quite pricey for larger teams. If you are a Jira or Confluence fan then this could go well with your existing setup.
  • Visual Studio Online - Impressively Microsoft are now fully embracing the git path alongside their TFS offering. Visual Studio Online offers a basic 5 user, unlimited project, private repo hosting option which is very good for lots of small private repos. This is also handy for clients who have firewall rules in place for Github.