Search code examples
gitgit-push

git force push current working directory


I need to git push to remote, and that push need to be current working copy. Because, pushed source is a web host. So, pushed files need to be currently in use. I am sure no one will edit files at the host :)

This question is follow up of these 2 questions, Is it possible to have a git repo inside another git repo and what difference does --bare switch make when initing a git repo?

Edit: bare repo is not what I want, since files need to be directly used on remote machine, and need to be found on the locations where I put them.

Edit2: It is my understanding that bare repo doesn't keep the file hieararchy, and keep repo data in root directory


Solution

  • Here is a step-by-step on how to create a git repo on your web host that will allow direct pushes, set it up as a remote for your local repo, and push changes to it. Note that you need to have ssh access to your web host, and have git installed.

    On your web host (ssh'd in):

    # create an empty repo on  your web host; note its path
    mkdir site
    cd site
    git init
    
    # configure it to allow pushes to the current checked-out branch
    # without complaining (direct push)
    git config receive.denyCurrentBranch ignore
    

    Now with your local repo (run these git commands inside the repo):

    # create the reference to the web host remote for pushing
    # /path/to/the/repo is the path to the repo you made above on the web host:
    # it will be an absolute path OR relative to your ssh chroot (depends on the host)
    git remote add deploy ssh://your_user@your_host/path/to/the/repo
    
    # finally, push your changes!
    git push deploy master
    

    Now, your changes have been pushed to the checked-out repo on the web host. However, they will not be reflected in the working dir (that is, your changes will not take effect immediately). This is because you have pushed directly to the active branch. To finish up, you need to either

    • manually perform a git checkout -f to update the working dir
    • make a git post-receive hook to do it automatically.

    To have instant, automatic deployment with a post-receive hook check out this git site deployment howto.