Git workflow - Setting up a build process

Disclaimer: I've been using Git for a while but still find it confusing.

I'm setting a build process for a project and am running into a problem updating the git repository.

  1. I created the repository on the server, lets say C:\MyProject.
  2. I connected my computer to the repository with SourceTree. Do a pull and get all my code, everything is great so far.
  3. I go to make a commit but get an error saying: "refusing to update checked out branch... By default, updating the current branch in a non-bare repository is denied"
  4. Use Google a bit, and I realize that the git repository on the server shouldn't have master checked out so I use Git GUI checkout using the option to "Detach from Local Branch". This sets the branch on the server to HEAD.

Now is the part where I'm confused (let me know if I was actually confused earlier and just don't know it). What is the recommended way to go about updating the code on the server? If I open the server repository in Git GUI there is no option to do a fetch. What seems to work is if I checkout master, but is that actually what's recommended? And if it is, then that would mean I have to checkout a different branch after each update so that I can continue doing pushes on my work computer.


Solution 1:

It is a best practice to push to a bare repo: see "concept of bare shared repository in git" and all about "bare" repos -- what, why, and how to fix a non-bare push.

Which means on your server, you need to:

  • create a Git repo (you already did)
  • clone that repo a bare repo (git clone --bare yourProjectFolder yourProjectFolder.git)
    (the .git extention is a naming convention for bare repo root folder)
  • push to that bare repo instead:
    Go to your local repo, and type:

    git remote set-url origin /url/repo/repo/yourProjectFolder.git
    
  • add a hook in the bare repo (on the server, yourProjectFolder.git/hooks/post-receive), in which you

    • change directory (to your non-bare repo folder yourProjectFolder)
    • unset GIT_DIR
    • git pull ../yourProjectFolder.git

See more at "Remote nodejs server deployment with forever".