Monday, September 22, 2014

Shell script for updating project in server from local using git

Hi,

This is a trick for lazy people like me. Say, you have a simple project that is being hosted by github or bitbucket. That same project is being hosted in a server too. Then, there is an easy way for porting local changes to server through github or bitbucket.

First you need to add the same remote, either github or bitbucket in your local copy and in your server copy. So that, pushing from local and pulling from server both occurs keeping github or bitbucket in the middle.

Now, the steps involved in syncing the server with local copy after changes is like this:

1. Go to your project folder.
2. Add your changes.
3. Commit these changes.
4. Pull from github or bitbucket.
5. Push to those.
6. Login to your server by using SSH.
6. Go to the project folder in the server.
7. Pull from github or bitbucket.

Quite a few commands need to be executed. Let's add them in a simple script that can do all these.

For that, you need sshpass to insert password while logging into the server with ssh. Now, the script would look like these:
#!/bin/sh
cd "/path/to/your/local/project/" && 
git add . && 
git commit -am"lazy commit message" && 
git pull origin master && 
git push origin master && 
sshpass -p 'your_server_password' ssh username@server_address 'cd /path/to/project/ && git pull origin master'

:)

No comments:

Post a Comment