I’ve been learning a lot about Git over the last few months, & I wanted to start sharing a bit of what I’ve learned. We’ve been using GitHub at work, & it’s great, really great. But I wanted to be able to centrally host a git repo on our servers that acts as dedicated storage. I’m not going to use the git repo on the server as an actual development environment, just as a place to store the files that everyone else will use. In other words, I wanted to duplicate the GitHub experience on our servers. Here’s how.

Note that this setup works for us. It may not work for you. Notably, we’re all signing in as the same user. We all sign our commits, however. For a larger team, that wouldn’t fly. Test!

Set up the git repo on the server (crashaw.foobar.com)

Say you have a server named crashaw.foobar.com. Connect to that server & create the directory for the repo:

# cd /var
# git init --bare server-repo.git
# ls
server-repo.git/

The --bare tells git that this a storage directory for a repo, not an actual development environment.

CLI: Set up the git repo on your local machine A

You now have a bare repo on the server. Basically, you now move to your local computer, the real development environment (well, one of them, anyway), put the files you want to work on into your local repo, & push them to the server where they can be shared collaboratively.

$ cd /Applications/Git
$ mkdir local-repo
$ cd local-repo/
$ git init
$ touch README.md
$ git add README.md 
$ git commit -m "Created README.md"
$ git remote add origin root@crashaw.foobar.com:/var/server-repo.git
$ git push -u origin master

CLI: Set up the git repo on a different local machine B

Now someone else copies the repo to their computer so they can work on it too.

$ cd /home/alice
$ git clone -v root@crashaw.foobar.com:/var/server-repo.git
$ ls
server-repo/
$ cd server-repo/
$ echo test > README.md 
$ cat README.md 
$ git add README.md 
$ git commit -m "Added test to README.md"
$ git push

CLI: git pull & git push

Now you can use git pull & git push as normal on either machine. Here’s an example.

On machine A:

$ cd /Applications/Git/local-repo
$ cat README.md 
$ git pull
  remote: Counting objects: 5, done.
  remote: Total 3 (delta 0), reused 0 (delta 0)
  Unpacking objects: 100% (3/3), done.
  From crashaw.foobar.com:/var/server-repo
 6f0c364..7f747a7  master -> origin/master
  Updating 6f0c364..7f747a7
  Fast-forward
   README.md |1 +
   1 files changed, 1 insertions(+), 0 deletions(-)
$ cat README.md 
  test
$ echo testtest >> README.md 
$ cat README.md 
  test
  testtest
$ git add README.md 
$ git commit -m "Added testtest"
  [master 2ce5031] Added testtest
   1 files changed, 1 insertions(+), 0 deletions(-)
$ git push
  Counting objects: 5, done.
  Writing objects: 100% (3/3), 256 bytes, done.
  Total 3 (delta 0), reused 0 (delta 0)
  To root@crashaw.foobar.com:/var/server-repo.git
 7f747a7..2ce5031  master -> master

On machine B:

$ cd /home/alice/server-repo
$ cat README.md 
  test
$ git pull
  remote: Counting objects: 5, done.
  remote: Total 3 (delta 0), reused 0 (delta 0)
  Unpacking objects: 100% (3/3), done.
  From crashaw.foobar.com:/var/server-repo
 7f747a7..2ce5031  master -> origin/master
  Updating 7f747a7..2ce5031
  Fast-forward
   README.md |1 +
   1 files changed, 1 insertions(+), 0 deletions(-)
$ cat README.md 
  test
  testtest

GUI: Set up the git repo on your local machine A using Tower

I like the command line, but my business partner prefers to use GUI tools (although he’s quite proficient on the CLI). We both use Tower on our Macs, a very nice GUI for git (& other source code management tools). Here’s how to configure Tower to work with the self-hosted repo I detailed above.

Open Tower.

Go to Manage Repositories.

Press Clone Remote Repository.

Enter the following information:

  • Repository URL: root@crashaw.foobar.com:/var/server-repo.git
  • Bookmark Title: [Autofills with server-repo; you can change if you’d like]
  • Authentication: Private Key
  • Username: root
  • Private Key: [Select appropriate key]
  • Key Password: [Leave empty]
  • Clone To: /Applications/Git/server-repo

Press Clone.

You can now proceed as usual, as you’re used to with GitHub, but now the repo is on our server.