Common Git commands for a typical website’s development workflow
In this post we summarize a set of common Git commands (and steps) typically used in our website development workflow.
Configuring a Git server
Creating a new shared repository
A shared repository is a repository shared between members of a group. Each member can work with the repository (cloning, push and pull).
Create a new bare shared repository on the server:
$ git init --bare --shared [project].git
Configuring a post-receive hook for automatic deploy
In a production server, or in a staging server, we can configure a post-receive hook such that when the project is pushed in the Git repository on the server it will be automatically copied in the website root folder.
In this way we can deploy our website in the production server, from our local PC, just with:
$ git push production master
To create the hook, first enter inside the Git bare repository (in the server):
$ cd [project].git
Create the post-receive
hook file:
$ vim hooks/post-receive
And put a content like this inside it:
#!/bin/sh
# -----------------------------------------------------------------------------
# First we define some variables containing our paths.
# You should replace these values with your custom paths.
# -----------------------------------------------------------------------------
# Website root folder
APP_WEB_DIR="/home/netgloo/public_html/"
# Git repository path
APP_GIT_DIR="/home/netgloo/git/netgloo_web.git/"
# -----------------------------------------------------------------------------
# Here we copy the repository content inside the website root folder.
# This action is triggered when a git push occurs on this server.
# -----------------------------------------------------------------------------
# Copy the repo content to the website root directory
git --work-tree=${APP_WEB_DIR} --git-dir=${APP_GIT_DIR} checkout -f
# Clean the website directory (use --exclude=[pattern] to exclude some file)
git --work-tree=${APP_WEB_DIR} clean -fd
# Do other things here, for example run scripts, optimizations or automatically
# load database changes
# ...
Finally, make the hook executable:
$ chmod +x hooks/post-receive
Now, inside the project folder on your local PC, you should add the production remote url (or the staging remote url). You can see below how to do that.
Working on your local PC
Create a repo and push it to the server
If you created a new bare repo on the server, now you can create the local repository in your project folder and push the project on the server:
$ cd [project-folder]
$
$ # Create a local repo
$ git init
$
$ # Add the server's URL
$ git remote add origin ssh://[user]@[server-addr]/path/to/[project].git
$
$ # Make the first commit and push the project on the server
$ git add --all
$ git commit -a -m "First commit"
$ git push -u origin master
Cloning a Git repo
Using the clone
you can get a repo from the server, copying it locally.
Once the repo is copied you can start to work on the project.
$ git clone ssh://[git-user]@[server-addr]/path/to/[project].git
$ cd [project]
Note: will be created the folder [project].
Commit, push, pull
Commit
Commit all last changes on your local repo:
$ git add --all
$ git commit -am "here the commit message..."
Push
Send local commits to the Git server:
$ git push origin master
Pull
Get the last changes from the Git server:
$ git pull
Note: this is equals to git fetch
+ git merge
.
Check the status
Check what files you changed and will be committed:
$ git status
Check changes on a file:
$ git diff path/to/file
Revert changes
This command will revert the file to the last commited version, resetting last changes:
$ git checkout -- path/to/file
This command will revert all uncommitted changes in the working directory (use it with care):
$ git reset --hard
Change the remote server’s URL
You can change the Git server’s URL:
$ # View existing remotes
$ git remote -v
$
$ # Change the 'origin' remote's URL
$ git remote set-url origin ssh://[user]@[new-server-addr]/path/to/[project].git
Alternative: using the git protocol (instead of ssh) you should use this URL:
git@[new-server-addr]:path/to/[project].git
Add production remote’s URL
You can add another remote Git server for pushing your project in the production environment (i.e. pushing it live online) when you finish to modify it:
$ # Add the 'production' server's URL
$ git remote add production ssh://[user]@[production-server-addr]/path/to/[project].git
Push the project on production (i.e. on the new remote named ‘production’):
$ git push production master
The production server should be configured with a Git repo and a script, triggered when the repo is updated, that will copy the repo content in the online directory. You can see an example of such configuration here.
References
https://www.atlassian.com/git/tutorials
https://git-scm.com/doc
https://confluence.atlassian.com/display/STASH/Basic+Git+commands
http://rogerdudler.github.io/git-guide/
http://thelucid.com/2008/12/02/git-setting-up-a-remote-repository-and-doing-an-initial-push/
http://www.dennisplucinik.com/blog/2012/03/17/common-git-commands/
http://www.norbauer.com/rails-consulting/notes/git-revert-reset-a-single-file.html
Git workflows
https://www.atlassian.com/git/tutorials/comparing-workflows/