gitgit-pushgit-non-bare-repository

How to push to a non-bare Git repository?


I usually work on a remote server via ssh (screen and vim), where I have a Git repository. Sometimes I'm not online, so I have a separate repository (cloned from my remote) on my laptop.

However, I can't pull from this repository on remote side because I'm usually behind a firewall or I don't have a public IP.

I've read that I should push just to a bare repository. How should I then push my changes to my remote repository?


Solution

  • receive.denyCurrentBranch updateInstead

    This options was added in Git 2.3, and it makes the server update its working tree if it is clean.

    So if you ensure that you always commit before you pull locally, and keep a clean working tree on the server (which you should do to avoid having merge conflicts), then this option is a good solution.

    Sample usage:

    git init server
    cd server
    touch a
    git add .
    git commit -m 0
    git config --local receive.denyCurrentBranch updateInstead
    
    cd ..
    git clone server local
    cd local
    touch b
    git add .
    git commit -m 1
    git push origin master:master
    
    cd ../server
    ls
    

    Output:

    a
    b