Creating and deleting topic branches with git and github

Topic branches are a useful convention when working with git, the version control system. A topic branch lets you work on a new feature or a bugfix in isolation from the master branch. It’s particularly useful when working on several bug-fixes or features simultaneously.

The branches are typically named after the new feature, e.g. “fancyfeature” and are short-lived. Once you’ve completed work, you can merge them back into master. Instead of using a standard:

  1. git merge

it can be useful to do a:

  1. git merge --squash

This allows you to squash all the commits on the topic branch (which may have included various dead ends, repeated tweaks) into a single commit on master.
This is the strategy shown below:

# make a new topic

  1. git checkout -b fancyfeature

# change some files, etc

  1. git commit -a -m "made some changes"

# more fixes, etc

  1. git commit -a -m "stupid fancyfeature. maybe this will work"

# push a copy to github so my colleagues can review my changes

  1. git push origin fancyfeature

# more changes

  1. git commit -a -m "woo, got the fancyfeature working"

# i'm done! switch back to master

  1. git checkout maste

# merge in all my changes, squash them into my working dir

  1. git merge --squash fancyfeature

# commit those changes as a single commit

  1. git commit -a -m "Added fancyfeature"

# tag the branch just in case we need it later

  1. git tag archive/fancyfeature fancyfeature

# delete the topic branch in my local repo

  1. git branch -D fancyfeature

# delete it on github too

  1. git push --tags origin :fancyfeature
Matt Mayer

Matt Mayer is a founder at ReignDesign. Matt is from the UK and was based in Shanghai for ten years. He is now living in Bangkok, Thailand.

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *