Continuous Deployment: Git Merging

I created a continuous deployment to the Microsoft Azure Web App Service in my last post. In that post, I suggested that you create a new branch on GitHub to house “the deployed code” – I called that branch “azure”. However, that begs the question – how do I get the code I am working on into the azure branch?

Let’s take a simple scenario. I’ve got my two branches – the master branch where I do all my work, and the azure branch which is the deployed code. I’ve got my web site working perfectly fine, but now I’ve changed some files in the master branch and I want to publish. How do I do that? Fortunately, git has given us all the tools we need to do this. Here is my handy dandy quick reference for dealing with git in the master branch:

# Clone a repository
git clone<user>/<repository>

# Check which files are checked out
git status

# Stage files that are not in the repository yet
git add <file>

# Commit all changed files plus those files that were added
git commit -a -m "<message>"

# Push the changes up to GitHub
git push

I’ve gotten along very nicely with those, but now there are some new ones:

# Check out an old branch
git checkout <branch>

# Copy the current branch to a new branch
git checkout -b <new-branch>

So, now I’ve got two branches. I want to merge the changes from one branch to the other. Here is my cheat sheet:

# Which files are changed between source and destination branches
git diff --name-status <destination>..<source>

# Patch between source and destination
git diff <destination>..<source> > patchfile

# Merge from source to destination
git checkout <destination>
git merge <source>

# Delete a temporary branch (push removes it from GitHub)
git branch -d <branch>
git push origin :<branch>

Let’s take a look at my situation now. I have a destination branch of azure and a source branch of master. I want to merge my changes. The first thing I do is check out the azure branch and see what has changed between the two branches:


I can see I have two new files (the A next to the file) and four modified files. Now, let’s do a merge:


If it is just me editing the files, then the merges should be relatively clean. However, you may get into a situation where two people have changed the same file. That’s a merge conflict and those are messy to deal with. Check out the Git Branching and Merging tutorial to understand merge conflicts as those can be complex to unwind.

Now test the azure branch. I wipe out the node_modules directory and run npm install followed by gulp to ensure that the service can be built, then I run the server locally and test out the functionality by hand that I am adding. Once I’m ready, I can push the azure branch up with a simple git push command while it is checked out.

I also do feature branches. I’ve got my master branch with the clean copy of the code. Create a branch for a feature and get it working, then merge that feature branch back into master before deleting the feature branch. Finally, merge the master changes from all the new features into the azure branch for publication.

This way I can use GitHub for my remote repository and deal with all the publication workflow easily.