Git push to live server
We 开发者_Python百科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 admin@webserver.example
webserver: password: ********
webserver: cd ~/domain.example/
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: ********
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/
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.example/~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://admin@webserver.example/~admin/domain.example/
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
- Add remote
$ git remote add server ssh://server_hostname:/path/to/git/repo
- Checkout temp branch on server
$ git checkout -b temp
- Push changes
$ git push server
- 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
精彩评论