Using Git for server-side code deployment

Source: Internet
Author: User
Tags using git windows remote desktop

Traditional deployment Scenarios
Windows Remote Desktop
Log on to the server pull GitHub code
Phing (PHP Professional Deployment tool)

Git automatic deployment flowchart

Server-side readiness:
0. This work is done under root or under the administrative authority account, the following root for the user, switch to other users will be prompted
1. Make sure you have Git installed
2. For security purposes, create a new unprivileged user dedicated to code deployment
Useradd-m Deployuser
passwd Deployuser #设置该用户的密码, can also be configured as a password-free login
3. Create a new directory as the root directory where you want to deploy the code, such as:
4. Change the owner group of this directory to the newly created user Deployuser
Chown Deployuser:deployuser Deploy
5. Switch to a dedicated user for the deployment code
Su Deployuser
6. Go to the project root directory and initialize to git repository
CD Deploy
Git init
7. "Important" Let the warehouse accept code submission
git config receive.denycurrentbranch ignore
[optional] git config core.worktree ~/www
[optional] git config--bool receive.denynonfastforwards false #禁止强制推送
At this point, an empty Git repository is built on the server and the address of the warehouse is:
Ssh://[email Protected]/var/www/html/deploy/.git

Local Warehouse Preparation:
1. Get code locally from the GitHub repository via git clone or git pull
2. Add the server to the remote warehouse list, using the name to distinguish between different servers, such as the test server can be called testing
Git remote add testing ssh://[email protected]/var/www/html/deploy/.git
3. Submit the local code to the test server
git Push testing Master

Back to server side:
1. Update the server-side Git repository status and check out files
Git update-server-info

Git checkout-f
git checkout Branch_name # If the push is not a master branch

2. Check if the file is updated
3. Set server-side update hooks post-update
CD. Git/hooks
Create a new post-receive or rename post-receive.sample to Post-receive
Touch post-receive
MV Post-receive.sample post-receive
Vim post-receive
Copy the following to the file
Unset Git_dir
Git checkout-f
Note: The 3rd step will replace post-receive with Post-update, but you will need to delete the exec git update-server-info line in post-update first.

Update for subsequent code:
1. GitHub has an update when pull updates the local deployment warehouse
2. Then local push to test server for testing
3. The test is then push to the official server to go live
4. Rollback of the code:
* Server-side rollback: recommended git reset--hard head^
Local warehouse rollback: Code rollback is done without logging in to the server, git reset head^ retains code rollback, and then uses git push remote_name local_branch_name-f to force push

Issues to be aware of during use:
1. Need to agree on how git can not update the operation of how to deal with, such as New database fields, new installation necessary expansion ...

Using Git for server-side code deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.