Drupal: Create and Manage GitHub Branches for Different Environments

When you are ready to move a site in the University's Drupal Enterprise environment from DEV to STG and PRD, we recommend you use different branches for different environments.  Branches are different copies of your code that can be edited separately and merged together. Having different branches in the DEV / STG / PRD environments ensures that development code doesn't accidentally get pushed to production websites.

Create a New Branch

The easiest way to create a new branch is through the GitHub web interface.

  1. Go to github.umn.edu and sign in.
  2. Select your repository.
  3. Select the branch you want to branch from (most likely the master).
  4. Select the branch dropdown menu.
  5. Type the name of your new branch in the "Find or create a branch..." field, then press the enter key.

screenshot of the branch menu Find or create a branch... field

Manage Branches

If you haven't already, install GitHub for Mac or GitHub for Windows. Download the repository to your machine by doing git clone URL of the repository directory location.

Below are the commands you probably will use most when working with the branches.

git pull

Pulls the current branch from the server to your local machine. If you create a new branch using the method I listed here, you'll need to do a git pull so your machine knows about the new branch. If you have multiple people working on a repository, you need to do git pull to make sure you have the latest code on your machine.

git checkout branch name

This will make the branch name the active branch. For example, if you are in the master branch, and you want to start working in the develop branch, you would type git checkout develop.

git push

Pushes the current branch from your machine up to the server. For example, if you've made changes to the develop branch that you want to have deployed, you'd checkout the develop branch (if necessary) then type git push.

git diff branch name

Use if you want to know the detailed differences between two branches on your repository. For example, if you are in your develop branch and want to know what the differences are between this and your master branch, you would use git diff master.

git diff --name-only branch name

This is the same as git diff branch name except instead of giving a detailed list, it only shows the names of the files that have changed.

git merge branch name

Use this to merge the contents of [branch name] into your current branch. Git will try to merge based on the last modified date for the files. If there are conflicts (i.e., a file has been modified in both branches since the last merge), Git will list the files so you can reconcile the difference. We recommend you do either a git diff or git diff --name-only before you merge so you know what is going to happen.