
In this post, I will explain:
- how to configure a repository on github for collaboration
- a workflow using a master, QA, and PROD branches for different stages of your development cycle
- how to commit a change
- how to promote a change through the workflow
How to configure a repository on github for collaboration
We’ll assume that a remote repository has already been created by a github user named “stevo”. Now, suppose another github user named “john” would like to collaborate on this remote repository. The first thing to do is allow john access to the remote repository.
Here are the steps to add john as a collaborator:
1. john creates a private and public SSH key, if he/she doesn’t have one ([username]@[hostname] is my preferred naming convention for a key, where username is your local host user name and hostname is your local host name)
ssh-keygen -t rsa -C "[username]@[hostname]"
2. john copies public SSH key and sends it to stevo
cat ~/.ssh/id_rsa.pub | xclip -sel clip
3. stevo adds john’s public SSH key to github repository for access privileges (I name it [username]@[hostname])
github > Account Settings > Account Overview > SSH Public Keys > Click on "Add another public key" link > Copy public SSH key to value textbox
4. stevo adds john to github repository
github > Dashboard > Click on repository URL > Click on "Admin" button > Click on "Add another collaborator" link > Enter collaborator github account name
The workflow
Before we begin, I’ll define some git concepts that we need to understand:
Working tree: Files that are ‘checked out’ for editing.
The Index: Git stages changed files in “the index” before they are committed to the local repository. This allows individual files (or even individual diff blocks) to be committed even when other changes exist in the working tree.
Local repository: Git maintains a complete copy of all files, branches and tags of the repository in the .git directory at the top of a working tree.
Remote repositories: Git can also track repositories maintained outside the .git directory, either elsewhere on the filesystem or on a different host.
The development lifecycle contains these stages: DEV -> QA -> PROD
We’ll represent these stages in our repository using the master branch as our DEV stage, QA branch as our QA stage, and PROD branch as our PROD stage.
Clone the remote repository onto your localhost. By default, git will make this the master branch (this command creates a folder in your current path called [repository_name]).
git clone git@github.com:[stevo]/[repository_name].git
Create 2 remote branches: QA and PROD
git branch -b QA
git branch -b PROD
To see all the branches available, run this command:
git branch -a
This command should output the following (The star denotes the current branch you’re in; HEAD is a pointer to the current branch):
* master
origin/HEAD
origin/PROD
origin/QA
origin/master
A ‘tracking branch’ in Git is a local branch that is connected to a remote branch. When you push and pull on that branch, it automatically pushes and pulls to the remote branch that it is connected with.
We want to create a tracking branch for both QA and PROD by running these commands on your localhost:
git branch --track QA origin/QA
git branch --track PROD origin/PROD
You should now have all these branches available:
PROD
QA
* master
origin/HEAD
origin/PROD
origin/QA
origin/master
Committing a change
Step 1: Switch to the master branch
git checkout master
Step 2: Update the master branch from the remote master branch (origin/master):
git pull
Step 3a: If you’ve created a new file and you’d like to check it in:
Add new file to local repository
git add
Push changed file from local repository to remote repository
git push origin master
Step 3b: If you’ve updated an existing file in the local project and you’d like to commit the changes:
Commit changed file to local repository
git commit [path/to/filename] -m "Insert comments here"
Push changed file from local repository to remote repository
git push origin master
Step 3c: If you’ve deleted an existing file in the local project and you’d like to commit the changes:
Delete the file in local repository
git rm [filename]
Commit changes from local repository to remote repository
git commit [path/to/filename] -m "Deleted file"
Push changes from local repository to remote repository
git push
Promoting a change through the workflow
Step 0: Sync the local master branch with the remote master branch
git pull origin master
Step 0: Diff the QA branch with the master branch to verify your changes
git diff QA master
Step 1: Switch to QA branch (Note: after switching, you’ll see that your changes are not there anymore because you’re now in the QA branch! Cool!)
git checkout QA
Step 2: Promote code from master branch to QA branch (Note: this will promote everything)
git merge master
Step 1: Switch to PROD branch
git checkout PROD
Step 2: Promote code from QA branch to PROD branch (Note: this will promote everything)
git merge QA

