Git push to live server

63,204

Solution 1

Yes you can push directly to your webserver, but I wouldn't recommend it since you should only push to repositories cloned with the --bare argument. I'd utilize the Git hook system to let the main repository automatically update the repo on the web server. Check out the post-update hook in:

http://git-scm.com/docs/githooks

This script could in turn login to the web server via SSH and do

cd ~/example.com/
git checkout master
git pull origin master

This way you only need to focus on pushing to the central server and don't have to care about the web server, it will always be updated once a push has been made. If you can automate something, then automate it :)

I even found a nice article for you about logging in via SSH in a script (if you must use password, this is trivial if a ssh-key has been setup):

http://bash.cyberciti.biz/security/expect-ssh-login-script/

Solution 2

I had the same query and not satisfied with the currently top-voted answer here, ended up following git-website-howto which outlines the process fairly well and is IMO a much cleaner and quicker approach.

TL;DR, git init --bare to create a fresh repo on your web server where you will push your changes from your dev machine. When the web repo receives your changes, it fires the post-receive hook which then copies the files to your web root.

I like this approach because the post-receive hook does the work on your server so your local machine can push much faster and free itself up. This also makes it very easy to setup remote tracking for a particular branch. So you could have a branch called production to update your web server, while your master continues to be for development and link to your git repo elsewhere.

Note: you'll need run git config receive.denycurrentbranch ignore on your web server repo to suppress a warning on your local dev box when pushing.

Solution 3

Look at the Git URLs portion of http://www.kernel.org/pub/software/scm/git/docs/v1.6.0.6/git-push.html

so you would try:

git push ssh://[email protected]/~admin/domain.example/ master

ADDED: I think part of what you are asking for is how to have multiple remote repositories.

git remote add webserver ssh://[email protected]/~admin/domain.example/

that allows you to run:

   git push origin master
   git push webserver master

Solution 4

I think the feature you are looking for is described here: http://debuggable.com/posts/git-tip-auto-update-working-tree-via-post-receive-hook:49551efe-6414-4e86-aec6-544f4834cda3

From local you can add the webserver as a remote, just like you would do any other:

git remote add webserver admin@webserver:/path/to/repo.git/
# push only master branch by default
git config remote.webserver.push master  

Now when your ready to push you can just do:

git push webserver

Solution 5

  1. Add remote $ git remote add server ssh://server_hostname:/path/to/git/repo
  2. Checkout temp branch on server $ git checkout -b temp
  3. Push changes $ git push server
  4. Checkout previous branch and delete the temporary one $ git checkout - # shorthand for previous branch, git checkout @{-1} $ git branch -d temp

I have more background information here: https://medium.com/@2upmedia/git-push-to-live-server-5100406a26

Share:
63,204
Petah
Author by

Petah

All your base are belong to us!

Updated on July 09, 2022

Comments

  • Petah
    Petah almost 2 years

    We have a website that has all its PHP/HTML/JS/CSS/etc files stored in a Git repository.

    We currently have 3 types of computers (or use cases) for the repository.

    • Local developer: pull latest changes, make changes, commit to local repo, push to master server
    • Master server: central repository, all changes get pushed to the master server
    • Web server: changes are pulled down from the master server when deploying the website

    So currently we:

    local: git push origin master
    local: password: ********
    local: ssh [email protected]
    webserver: password: ********
    webserver: cd ~/domain.com/
    webserver: git pull origin master
    

    So my question is: is there a way that from my local computer I can push straight to the web server?

    ie.

    local: git push origin master
    local: password: ********
    local: git push webserver master
    local: password: ********
    
  • cmcginty
    cmcginty over 13 years
    The problem is that push and pull are not interchangeable. Pushing to a repo will not modify the working branch.
  • Tim Hoolihan
    Tim Hoolihan over 13 years
    @Casey, if you read the question, he is just asking how to push, he doesn't mention trying to update the working tree. You're probably right about what he's intending to do, but the down vote seems a bit harsh for taking his question at face value.
  • cmcginty
    cmcginty over 13 years
    In the question his current workflow is "git pull origin master"
  • Tim Hoolihan
    Tim Hoolihan over 13 years
    "So my question is: is there a way that from my local computer I can push straight to the web server?" -and- "local: git push webserver master"
  • Samuel Cook
    Samuel Cook almost 7 years
    the howto worked great for me. because i was working in one directory of the server to another i needed to sudo a user in the post-receive file. sudo -u <username> -H sh -c "GIT_WORK_TREE=/path/to/live git checkout -f"
  • cloudsurfin
    cloudsurfin about 4 years
    This has a benefit of not needing to store private keys on a production web server (to to pull from the primary repo).
  • Hashim Aziz
    Hashim Aziz about 2 years
    I like this approach better than the other approach of pushing to a bare repo on the server, but your script code doesn't make much sense in this context - that code seems to be the "traditional" way of doing things and seems to run contrary to the "automation" that the rest of the answer advocates for.