Create a Bare, Centralized Git Repo

Review of steps to create a local Git repo and clone it into a bare, centralized Git repo (git clone --bare)

This brief article shows a method for setting up a shared Git repo on a shared drive and then pushing and pulling from it with various clients. Having a shared repo / depot works equally well with Linux, Mac OS, and Windows clients, and it's a great way to create a single centralized resource that's simple & straight forward to back up.

The first thing you need to do is create a shared folder. Do yourself a favor and do this on either a Linux box or Mac. We're running Ubuntu 16.04 Linux and create our depot folder under our /build partition: /build/depot. Since we make use of Windows clients for some of our work (e.g., CAE), we share our /build/depot drive with both NFS and Samba. On Windows, we map the drive using Samba (e.g., O:\)

$ mkdir -p /build/depot
$ cd /build/depot
depot$

Next, let's set up a local directory (git repo) on a Linux box or a Mac where we'll keep our Bash and Python scripts. Create this new directory and run git init

$ cd  ~  # home folder
$ mkdir -p scripts
$ cd scripts

scripts$ git init
Initialized empty Git repository in /home/ex_user/scripts/.git/

For our example, create a simple script that mounts our shared depot drive using NFS. Using your editor, open "mntdepot.sh":

file: mntdepot.sh
#!/bin/sh
# file: mntdepot.sh

sudo mkdir -p /mnt/depot
sudo mount -o nolock myserver:/build/depot /mnt/depot

Ideally, we should have two terminals now open side-by-side: 1) /build/depot on our server; 2) ~/scripts on our local machine.

Create our first local commit, and we'll clone this on our server:

scripts$ git add .
scripts$ git status  # to see what we're going to commit
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached ..." to unstage)

	new file:   mntdepot.sh
	
scripts$ git commit -m 'initial commit: mntdepot.sh'
[master (root-commit)...

Now go back to the terminal for the shared repo on our server. For this, we assume the local drive is mapped from the server and is at /mnt/ex_user. We're now ready to clone the scripts repo as a bare repo.

Note that it is a useful convention to name our bare repo using a ".git" suffix.

depot$ git clone --bare /mnt/ex_user/scripts  scripts.git
Cloning into bare repository 'scripts.git'...
done.

Since this is our centralized repo, remove the reference to the existing remote origin:

depot$ cd scripts.git
scripts.git$ git remote rm origin
scripts.git$ ls
branches  config  description  HEAD  hooks  info  objects  packed-refs  refs

That's basically it for the server. Notice that when we peform an ls in our bare repo, we just see the configuration and database files of our repo, which has the same structure under .git of our local repo.

The last thing we need to do is configure our local scripts repo to reference our new centralized, bare repo as the origin, so we can push and pull from it.

scripts$ git remote add origin /mnt/depot/scripts.git/
scripts$ git remote update origin
Fetching origin
From /mnt/depot/scripts
 * [new branch]      master     -> origin/master

Finally, edit .git/config in our local scripts folder (repo) and add the following to the bottom of the file:

file: .git/config
[branch "master"]
	remote = origin
	merge = refs/heads/master

You should now be able to push and pull from the local scripts repo to the bare repo on the server:

scripts$ git pull
Already up-to-date.
scripts$ git push
Everything up-to-date

Help us improve this article by adding your comment or question:

email addresses are neither displayed nor shared