October 22, 2018

The GIT version control system - remote repositories

If you obtained your local git repository and working files by doing a "git clone", you can ignore much of what follows. The git alias "origin" will be set up to track wherever you cloned from and if you just want to build software (which is often the case), you either are done entirely or you may want to pull updates should they appear in the remote repository you cloned from.

Setting up an empty remote repository

Suppose you have a project with a local git repository and want to start keeping a copy of it in some remote repository somewhere. This could be on the same machine, but in general this will be on another machine simply to achieve redundancy, if not to make it possible to share files with other people. The most common case here is to use Github, or a service like it, and I have a special section dedicated to dealing with that. But if you want to set up a remote repository on a machine you administer, that is what this section is all about.

Go to the other machine (the server that will host your remote repository) and do this:

ssh my_server-machine
cd /big_disk/my_git_repos
mkdir project.git
cd project.git
git --bare init
Using a name like "project.git" is entirely up to you, but this is a convention I find useful. Having done this, the next question is how to specify this remote repository from your project on some other machine.

How to specify a remote repository

On the machine with your project files, you will want to do this (once you figure out what "URL" should be).
cd my_project
git remote add origin  URL
The simplest case is where the respository is not remote at all, but is on the same machine. Here you just need to specify the path to the files, as in:
cd my_project
git remote add origin  /big_disk/my_git_repos/project.git
This is not as stupid as it may sound. I often have a working copy of my files on the same machine that hosts the repository (and I also reference the repository from remote machines). This also works if the machine you are on uses a network mount (NFS or something of the sort) to access the files on some server.

Note that you only do the "remote add" command once and for all. Git remembers it and you simply use "origin" and before long you forget what the URL was to begin with.

Suppose you have ssh keys set up to access the remote machine holding your files, then you use the following:

Note that there is no explicit mention of ssh.

Pushing your local repository to the remote

Once everthing is set up, you simply do this:
git push origin master
In fact, the usual thing to do whenever you are working on a project and it compiles cleanly and seems like something you are willing to share is to do:
git add .
git commit
git push origin master
If you have made a lot of changes, and just want to keep your local repository up to date (a good idea), you omit the last line and do:
git add .
git commit
But what is this "origin" and "master" all about? The word "master" is the name of the git branch you want to push. Many projects will only have a master branch, and this is the default if you don't do any branching. The word "origin" is the short name for the URL to the remote repository. This is set up automatically for you if you do a git clone, but if you are starting from scratch with a project that has never been under git control, you set it up via a command like:
git remote add origin  URL

Feedback? Questions? Drop me a line!

Tom's Computer Info / tom@mmto.org