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:

git merge

it can be useful to do a:

 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

git checkout -b fancyfeature

# change some files, etc

git commit -a -m "made some changes"

# more fixes, etc

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

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

 git push origin fancyfeature

# more changes

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

# i’m done! switch back to master

git checkout master

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

git merge --squash fancyfeature

# commit those changes as a single commit

git commit -a -m "Added fancyfeature"

# tag the branch just in case we need it later

git tag archive/fancyfeature fancyfeature

# delete the topic branch in my local repo

git branch -D fancyfeature

# delete it on github too

 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 *

Share this post