"Synchronizing" files between local and remote server using Git

5,058

When you are using a Git repository as a central repo (as you seem to be), you generally should not have a working directory on that repo. The reason for this is listed in the error you mentioned: pushing into a branch which is checked out can cause the working tree to become out of sync.

If the people you are sharing with can use Git, then you instead can set up a bare repository on the server, then push to it.

# On the server
git init --bare ~/project.git

# On your computer
git remote add origin ssh://[email protected]/~/project.git
git push origin master

# On everyone else's computer
git remote add origin http://example.com/~david/project.git
git pull origin master
git checkout master

If they cannot use Git, then you'll need a slightly more complicated workflow:

# On the server, once
git init ~/project
git config receive.denyCurrentBranch ignore

# On your computer
git remote add origin ssh://[email protected]/~/project
git push origin master

# On the server after each push
cd ~/project
git reset --hard

After this step, then the files should be available under /project

Share:
5,058

Related videos on Youtube

ChrisF
Author by

ChrisF

Updated on September 18, 2022

Comments

  • ChrisF
    ChrisF over 1 year

    My intended goal:

    I maintain some files in my local computer, and I also share them with others by putting them on my website. In the past I did this by manually uploading all the files using FTP, every time I did some modifications etc. Now, I am wondering if I can use Git to help me achieve this (by "pushing" the local files to my website server). My server is hosted by Dreamhost.

    First Attempt:

    First, I try this tutorial. I first push my local files to my Github repo, and ssh into my Dreamhost server to git clone --bare from the Github repo. But I find that git does not transfer my files. So I ignore the tutorial.

    Second Attempt:

    I ssh into my Dreamhost server to clone directly from Github. My files are all transfered to the server. Then, on my local computer, I git remote add dreamhost ssh://[email protected]/~/my-project. Then I add some files, and commit, and git push dreamhost master. And a bunch of errors appear:

    Error
    (source: geotakucovi.com)

    As a newbie Git user, I must have missed something. Please help!

    • Admin
      Admin about 11 years
      Do these files need to be under revision control or do you just need to "sync them"?
  • ChrisF
    ChrisF over 12 years
    Thanks David. I'm wondering if there's a way to do this without Github.
  • ChrisF
    ChrisF over 12 years
    Thank you, Stephen! It works. So by design Git does not allow any simper solution? Though maybe make or a script can handle these tedious commands.
  • Stephen Jennings
    Stephen Jennings over 12 years
    @ConcreteVitamin: I don't believe so, because git won't check out a working directory from a remote repo, it wasn't designed for that. If my answer sufficiently answers your question, please mark it as accepted by clicking the checkmark to the left. Thanks!