Git push to live server

你。 提交于 2019-11-29 18:57:05

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!

aleemb

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.

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

that allows you to run:

   git push origin master
   git push webserver master

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!