Git push to live server

GitDeploymentPushGit PushGit Remote

Git Problem Overview


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: ********

Git Solutions


Solution 1 - Git

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 ~/domain.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/

Hope this helps!

Solution 2 - Git

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 - Git

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://admin@webserver.com/~admin/domain.com/ 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.com/

that allows you to run:

   git push origin master
   git push webserver master

Solution 4 - Git

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 - Git

  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

Solution 6 - Git

Before deploying the local changes, check whether anything has changed on target server.

Add to deployment script to ensure nothing has changed on server:

$ git ls-files -dmo --exclude-standard

Will be empty if there are unchanged files, easier than parsing git status

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPetahView Question on Stackoverflow
Solution 1 - GitralphtheninjaView Answer on Stackoverflow
Solution 2 - GitaleembView Answer on Stackoverflow
Solution 3 - GitTim HoolihanView Answer on Stackoverflow
Solution 4 - GitcmcgintyView Answer on Stackoverflow
Solution 5 - Git2upmediaView Answer on Stackoverflow
Solution 6 - GitRohan KhudeView Answer on Stackoverflow