Creating A Shared “Central” Git Repo For A Local Project
I am a Git noob with VSS and a bit of SVN background, and so far have been unable to resist falling in love with Git. Setting up local repos for all my little projects is a breeze, but today I needed to track a larger project on which I work both on the office workstation and on the home PC. (The latter is capable of connecting to the office network over a VPN link, so I did not need to use any Git-aware transport or fiddle with permissions, and this post therefore does not touch those topics.) Now putting instructions for myself together below. They should also work for a “workstation+laptop” scenario and the like.
Basically, I’ve set up a local repo at each workplace, and, on one of them, a “central” bare repo holding only the master branch, to which I push changes that go into production.
Option A: Clone
The easiest is to clone the local repo into a new bare repo, have the former track the latter as origin
, and then clone the new bare repo to other workplaces as needed:
-
Clone the existing local repo into the directory that will hold the “central” repo:
git clone --bare local-repo-url central-repo-directory
Note: if you are working under *ix and both repos are on the same filesystem, you may also wish to specify
--no-hardlinks
so as to create a proper copy. Even though NTFS supports hard links, that option seems to make no difference under Windows as of msysgit 1.7.6. -
From within the existing local repo, run:
git remote add origin central-repo-url
-
At each other workplace (home PC, laptop, etc.), simply clone the central repo:
git clone central-repo-url [ local-working-directory ]
If
local-working-directory
is omitted, a directory matching the “humanish” part ofcentral-repo-url
will be created in the current directory:> git clone //server/share/path/project Cloning into project... done.
It will already have the remote “origin” set:
local-working-directory> git remote -v origin bare-repo-url (fetch) origin bare-repo-url (push)
Note: If you are on a Windows LAN/VPN, both repo-urls
and repo-directories
may be UNC pathnames, but you would need to use forward slashes in the former:
git clone --bare . \\server\share\path\project
Cloning into bare repository \\server\share\path\project...
done.
git remote add origin \\server\share\path\project
git push
fatal: '\server\share\path\project' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
git remote rm origin
git remote add origin //server/share/path/project
Everything up-to-date
Option B: Selective Push
The above approach has one disadvantage: the central repo will contain all branches that you had in the original local repo. If this is undesirable, e.g. because there are many (leftover) experimental branches, you may set up the central repo the other way round:
-
Create an empty bare repository that will serve as central for your project:
git --bare init central-repo-dir
Note:
central-repo-dir
need not exist, but if it exists, it better be empty, otherwise Git would make it a mess without issuing any warnings. -
Go to the local repo and add the newly created central repo as remote
origin
:git remote add origin central-repo-url
-
Push the desired branch(es) to the central repo, e.g.:
git push origin master git push origin release
That's it. Now you may clone the central repo to the other workplaces as needed.
Tags: git