How to use local filesystem remotes with git

If you develop with git and use multiple computers, you may find you want to keep your repos updated across these different computers. One way to do this is to use a git repo hosting provider like Github and push and fetch from remote branches.

Though there could be reasons why you might not want to do this: privacy, setup time, code you’re not ready to push to the rest of the world, … Another way you could do it is to copy your repo onto a USB disk or cloud drive, either zipped or uncompressed, then copy it onto the second computer and continue work. This works, but it is easy to get files out of sync.

Another option is to host your own local filesystem git remote repo. To do this, you need to create a bare repo:

cd repos # change to whichever directory you like
git init --bare myrepo.git

This will create a repo called myrepo.git that you can use as a remote. (Bare repos are normal repos but without a working copy of the code.) You can setup your bare repo wherever is convenient for you. In order to share it across multiple computers, it could be on a networked drive, a usb disk, a cloud drive such as Dropbox, …

Then you need to hook the repo where you’ve been working up to the remote. So, from that directory, run:

git remote add origin /path/to/repos/myrepo.git

The identifier origin is commonly used for remotes, but this can be what you like. Then push master or any other branches you want to have available to the remote.

git push origin master

Now to create a new working copy, you can clone the repo in a new directory:

git clone /path/to/repos/myrepo.git

To update a copy you can use standard git fetch, merge, pull, rebase commands with your branches as normal. For example:

git fetch origin
git merge origin/master

Or to link an existing copy of the repo to the remote and pull in remote branches:

git remote add origin /path/to/repos/myrepo.git
git fetch origin

Then merge changes as needed.

Other useful commands

To show information about a remote, for example if tracked branches are up-to-date:

git remote show origin

To show log messages of the changes between branches:

git log origin/master ^master

To show differences between branches:

git diff origin/master master

Tags: