If you’re like me (and condolences if you are), you’ve often started a local Git repository for a new project, and then later wanted to create a shared upstream repository on another computer. (GitHub walks you through this, but let’s assume you want to manage the shared repository yourself.) Before I knew Git very well, I did this clumsily by copying the .git directory and tweaking its config file and permissions (which was especially difficult when moving from a Windows desktop to a Linux server). But that is very un-Git; here’s a much better method:
On the upstream server, go to where you want the repository to be located and create a bare, shared repository:
$ cd /var/git
$ git init --bare --shared myrepo.git
Then on your local machine, simply add a remote for the new shared repository and push the local repository up to it with the
$ git remote add origin ssh://myuser@myserver/var/git/myrepo.git
$ git push -u origin master
-u option is short for
--set-upstream. From the git-push man page:
For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.
Now with just two Git commands, your local repository acts just like you’d cloned it from your new shared repository.
Note: If you have a group of users that you want to be able to modify the repository, ensure that it is owned by that group and is group writable. For example:
$ chgrp -R git /var/git
$ chmod -R g+rwx /var/git
$ ls -ld /var/git
drwxrwx--- 22 git git 4.0K Jul 16 12:00 /var/git