Git best practices: Commands to create and merge main and supporting branches

I recently published an article about Git standards to prevent code conflicts.

Down below are all the Git commands you need to know to follow those standards. This is highly recommended when you collaborate with other teams that are not in your company, or when your team is not always collocated.

We use those standards daily, and they have been really helpful in US Forest Service projects, like RMRS and CCRC, where we are not the only team/organization performing changes to those websites, and we rarely find ourselves resolving code conflicts caused by our work and/or the work of other teams.

Create a Git feature branch

This branch is created from develop, and it’s used to implement new features. Once developers are done with their changes, this branch is merged back to develop.

Steps:

1. Make sure everything is up to date.

(develop)$ git pull origin develop

2. Create feature branch, myfeature, which is branched from develop.

(develop)$ git checkout –b myfeature develop

3. Make code changes and commit.

(myfeature)$ git commit –a –m “Details about changes made…”
(myfeature)$ git push origin myfeature

4. Perform peer code review, unit testing, etc. Stable code is then merged to the develop integration branch.

(myfeature)$ git checkout develop
(develop)$ git pull origin develop
(develop)$ git checkout myfeature
(myfeature)$ git pull origin myfeature
(myfeature)$ git merge develop
(myfeature)$ git checkout develop
(develop)$ git pull origin develop
(develop)$ git merge --no-ff myfeature
(develop)$ git push origin develop

5. Remove feature branch since it has been merged to develop.

(develop)$ git branch –d myfeature
(develop)$ git push origin --delete myfeature

Create a Git release branch

Release branches are created from the develop branch as well. They are used to isolate the changes merged in the develop branch and get them ready for a new production release. This branch allows the develop branch to receive more updates without affecting that release.

Steps:

1. Make sure everything is up to date.

(develop)$ git pull origin develop

2. Create release branch, RC-2.2.7, which is branched from develop.

(develop)$ git checkout –b RC-2.2.7 develop

3. Make changes for the release and commit. Changes should only contain minor bug fixes and updates to the release documentation.

(RC-2.2.7)$ git commit –a –m “Comment for changes made…”
(RC-2.2.7)$ git push origin RC-2.2.7

4. The release branch goes to QA for verification. Once QA passes, merge the release branch to master and develop. Tag the release from master.

(RC-2.2.7)$ git pull origin RC-2.2.7
(RC-2.2.7)$ git checkout develop
(develop)$ git pull origin develop
(develop)$ git merge --no-ff RC-2.2.7
(develop)$ git push origin develop
(develop)$ git checkout master
(master)$ git pull origin master
(master)$ git merge --no-ff RC-2.2.7
(master)$ git push origin master
(master)$ git tag –a 2.2.7 –m “Tagging 2.2.7 release.”
(master)$ git push --tags

5. Remove release branch since it has been merged to master and develop.

(develop)$ git branch –d RC-2.2.7
(develop)$ git push origin --delete RC-2.2.7

Create a Git hotfix branch

Hotfix branches are created from the master branch, they are used when changes on develop are not release-ready and a fix to production is required that cannot wait until the upcoming release is stable.

Steps:

1. Make sure everything is up to date.

(master)$ git pull origin master

2. Create hotfix branch, hotfix-2.2.8, which is branched from master.

(master)$ git checkout –b hotfix-2.2.8 master

3. Make changes for the hotfix and commit.

(hotfix-2.2.8)$ git commit –a –m “Comment for changes made…”
(hotfix-2.2.8)$ git push origin hotfix-2.2.8

4. The hotfix branch goes to QA for verification. Once QA passes, merge the hotfix branch to master and develop. Tag the release from master.

(hotfix-2.2.8)$ git pull origin hotfix-2.2.8
(hotfix-2.2.8)$ git checkout develop
(develop)$ git pull origin develop
(develop)$ git merge --no-ff hotfix-2.2.8
(develop)$ git push origin develop
(develop)$ git checkout master
(master)$ git pull origin master
(master)$ git merge --no-ff hotfix-2.2.8
(master)$ git push origin master
(master)$ git tag –a 2.2.8 –m “Tagging 2.2.8 release.”
(master)$ git push --tags

5. Remove hotfix branch since it has been merged to master and develop.

(hotfix-2.2.8)$ git branch –d hotfix-2.2.8
(hotfix-2.2.8)$ git push origin --delete hotfix-2.2.8