Skip to content

Instantly share code, notes, and snippets.

@davidraedev
Forked from taldanzig/gitpushpull.md
Created April 18, 2020 09:11
Show Gist options
  • Save davidraedev/5774fde62820d40f509bd769b959b02c to your computer and use it in GitHub Desktop.
Save davidraedev/5774fde62820d40f509bd769b959b02c to your computer and use it in GitHub Desktop.
Make push/pull work with a remote non-bare repository

Make push/pull work with a remote non-bare repository

Sometimes it is necessary (and desireable) to work on a git repository on multiple development machines. We want to be able to push and pull between repositories without having to use an intermediary bare repository, and for this to work symetrically in both repositories.

First clone we clone an existing repository:

git clone ssh://user@hostname:/path/to/repo

By default this will name the remote as origin, but let's assume we want to reserve that name for a master repository that commits will eventually get pushed to:

git remote rename origin otherdev
git remote add origin masterhostname:/path/to/master/repo.git

Finally we need to edit our .git/config. We'll have a section that looks like this:

[remote "otherdev"]
    url = ssh://user@hostname:/path/to/repo
    fetch = +refs/heads/*:refs/remotes/otherdev/*

Add the following line:

    push = +refs/heads/*:refs/remotes/otherdev/*

Now in the original repository add a section as follows:

[remote "otherdev"]
    url = ssh://user@otherhost:/path/to/clonedrepo
    fetch = +refs/heads/*:refs/remotes/otherdev/*
    push = +refs/heads/*:refs/remotes/otherdev/*

Now either repository should be able to push and pull from the other.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment