10

Situation:

  • I have a local copy of a website
  • I have a server that I have SSH access to

What do I want to do?

  • Commit locally until I'm happy with my code
  • Make branches locally
  • Have one master branch that is the one that should be pushed to the server
  • Update the website using a single command (git push origin master)

If I set up a git repo locally using git init, and then push to a folder on the server, it doesn't work. When I FTP to the server to check the files, they're actually there. When I SSH into the server and do git status, it's not clean, even though it should be since I just pushed to the server.


Steps I'm doing:

  1. Make a new folder on my computer (mkdir folder_x)
  2. Go into that folder (cd folder_x)
  3. Set up a new git repository there (git init)
  4. (git repository sets up successfully)
  5. Push the repository to the server using git push origin master (where origin is set up as user:pass@server.tld)
Jon Seigel
  • 11,819
  • 8
  • 53
  • 90
Wolfr
  • 4,965
  • 2
  • 21
  • 31
  • duplicate http://stackoverflow.com/questions/3728054/git-push-to-live-server – cmcginty Oct 25 '10 at 22:00
  • possible duplicate of [Deploy a project using Git push](http://stackoverflow.com/questions/279169/deploy-a-project-using-git-push). [git config receive.denyCurrentBranch updateInstead](http://stackoverflow.com/a/28381235/895245) on the remote is a good possibility. – Ciro Santilli新疆棉花TRUMP BAN BAD Feb 07 '15 at 11:11

4 Answers4

12

You need to write a post-receive hook that runs git checkout -f after the remote git repository receives a push. See Using Git to manage a web site for details.

joeforker
  • 36,731
  • 34
  • 138
  • 231
  • 1
    this seems to assume I don't actually do work on my webserver. So any work done there is lost – gman Jan 24 '12 at 04:26
  • With Git 2.3 you don't need this hook anymore you can do `git config receive.denyCurrentBranch updateInstead`. See https://github.com/blog/1957-git-2-3-has-been-released . – Aurelien Mar 15 '15 at 20:29
  • 1
    I think @Aurelien s comment should be the accepted answer! Very nice solution! – Josef Eisl Feb 22 '16 at 09:24
1

When you push to a git repository, it doesn't update that repository's working files. Generally when you push, you should be pushing to a bare repository--- that seems to be how they intended git push to work.

For one of my (local ) projects, I wrote a script to automatically check out the latest "ui" subdirectory when I push my work to a deployment repo. It's embedded in a previous answer here: Developing Django projects using Git

You could also simply have the post-update hook on the server do a "git reset --hard master" if someone pushes an update to master, and use a non-bare repo rather than having a separate area for checked-out files.

Community
  • 1
  • 1
araqnid
  • 108,587
  • 20
  • 147
  • 127
0

Git Version 1.9.1
Ubuntu Server 14.04 LTS
LAMP Server

I set my LAMP server to update my working directory of my Git repo whenever one of my web developers pushes a change to the server. I noticed that the log would note the new commits, but would not update the working directory. Instead of doing this manually (git checkout -f) for every update, this can be set automatically to do so after a push has been received.

  1. In your ".git" directory, go into the "hooks" folder.
  2. Create a file named "post-receive" within the "hooks" folder with this content:

    #!/bin/sh

    # Update working directory after receiving a push from remote clients.
    # This should be directed at the git working directory.

    GIT_WORK_TREE=/var/www/dev_site git checkout -f

  3. Enable permissions to execute the file by typing "chmod +x post-receive" in the "hooks" folder.

It will now update the working directory when commits are pushed to the Git repo. My site now shows the changes when I visit it in a browser.

In this example, my working directory is "/var/www/dev_site". Change this accordingly to where your Repo is.

Please note, this Repo is not a bare Repo. A bare Repo does not have a working directory a web server can read from.

0

I use the post-update hook as detailed in this FAQ entry: http://git.or.cz/gitwiki/GitFaq#non-bare

It will update your working tree if you have configured the remote repo as "bare".