Search code examples
gitgithubdeploymentversion-controlweb-deployment

Complete control via Git over deployment with Github private repo as main


I'm learning and adjusting my workflow integrating Git. My wish is to be able to push my development branch to my test server from my local terminal. My current setup is giving me an error once I branch of. I have the feeling it's not an error but Git doing its work, in combination with me not understanding it completely. Are you able to explain why this is happening and how to avoid it?

The Git "error"

! [remote rejected] feature -> feature (branch is currently checked out)
error: failed to push some refs to (I hide the ssh server address)

How I came to this situation:

  • Create repo on github
  • Include deployment keys generated via cpanel from my server
  • Git clone on local machine
  • Git clone on server via SSH
  • Add server as remote to the repo as test

Flow

  • Adjusting master locally, pushing it to github and server all works fine. But my understanding is that the power of Git is create a branch for a 'feature' > test it > merge it to the master.

  • I make a branch called feature on my local machine

git checkout -b feature

  • Make some changes to the repo and push it to origin and test

git add . git commit -m 'added index.html' git push origin feature (for backup/sync purposes) git push test feature (to be able so see my code working on the test server

  • I have to 'checkout' the new branch in order to show the files on the server, so I SSH to my server and checkout the new branch:

    git checkout feature

My thinking is, that from this point on, I can work on feature branch locally > commit adjustments and with a simple push command git push test feature I can test the code on my test server.

Broke the flow

But now my flow is broken. After I check out the feature branch on my server, I'm not able to push my adjusted branch to my remote test. Git returns the message shown above.


Solution

  • Git clone on server via SSH

    You would need, still on that server through your SSH session, to add:

    cd /path/to/cloned/repo
    git config --local receive.denyCurrentBranch updateInstead
    

    Meaning, using Git 2.4 or more, using a push-to-deploy, in order to allow a git push to directly update a checked out working tree.
    Usually, you would rather push to a bare repository: see "What is a bare repository and why would I need one?", and use a post-receive hook with a checkout.
    That is: an executable (chmod 775) file named post-receive, in a "myrepo.git/hooks" folder, as detailed in the discussion.
    myrepo.git is a git clone --bare of your repo on the server: you can push to it.