Smart branching with SourceTree and Git-flow
By Steve on August 1, 2012Note: for brevity this article refers to Git and git-flow, but SourceTree supports exactly the same concepts in Mercurial via Hg Flow too.
In early 2010, Vincent Driessen wrote an article called “A successful Git branching model” which recommended an approach called git-flow to use git branches in your development cycle. The idea was to standardise branching and merging when developing features, handling releases and managing hot fixes, in order to be consistent and gain the advantages of git’s ‘branchy’ development model. Using many separate branches in Git gives you lots of flexibility, but it can get complex. Adopting a standardised approach has many advantages:
- Keep your repository tidier
- Keep your procedures clearer
- Move between projects more easily with familiar branch structures
- Get new developers up to speed more quickly
SourceTree 1.5 now integrates with git-flow and presents it to you in a friendly and intuitive way.
Summary of the concept
The general idea of git-flow is to use the following branch structure in your repository:
- Development branch (usually called ‘develop’)
This is your main development branch where all the changes destined for the next release are placed, either by directly committing small changes or by merging other branches (e.g. feature branches) into this branch. - Production branch (usually called ‘master’)
This branch represents the latest released / deployed codebase. Only updated by merging other branches into it. - Feature branches (usually prefixed with ‘feature/’)
When you start work on anything non-trivial, you create a feature branch. When finished, you’ll merge this branch back into the development branch to queue it for the next release. - Release branches (usually prefixed with ‘release/’)
When you’re about to package a new release, you create a release branch from the development branch. You can commit to it during your preparation for a release, and when it’s ready to be deployed, you merge it into both the development branch and the master branch (to indicate that the release has been deployed). - Hotfix branches (usually prefixed with ‘hotfix/’)
If you need to patch the latest release without picking up new features from the development branch, you can create a hotfix branch from the latest deployed code in master. Once you’ve made your changes, the hotfix branch is then merged back into both the master branch (to update the released version) and the development branch (to make sure the fixes go into the next release too)
SourceTree helps you utilise these branches via git-flow actions which we will describe below.
Getting started with git-flow
There’s a handy new addition to the toolbar in SourceTree 1.5 (keyboard shortcut Cmd-Alt-F):
Based on the current state of the repository, the Git-flow button will bring up a dialog with the most likely action you’d want to perform next. So if you haven’t set up git-flow on this repo yet, it’ll help you do that by default. If you’re on the development branch, it will default to starting a new feature. If you’re already on a feature branch, it will offer to finish your current feature and merge it back into the development branch, and so on. You can always get to all the other git-flow actions via this button as well, but most of the time the default option will be the action you’ll want SourceTree to perform.
If you haven’t used git-flow already on this repository, the first thing SourceTree will do is initialise your repository to use it. You’ll probably just want to use the defaults in the initialisation window so that’s not covered here. For more details please see the Help section included in SourceTree. Next up, we’ll concentrate on the actions we can perform with Git-flow and SourceTree.
Starting a feature
You can commit trivial changes directly to the development branch (‘develop’) if you like, but any time you start on something non-trivial you should explicitly start a new feature. This ‘Start Feature’ action will be the default action when you click the Git-flow button if you are currently on the dev branch.
The first thing to note is the ‘Preview’ window, which is present on all of the action dialogs and shows you what will actually happen when you confirm the dialog. In this case, a new feature branch called ‘feature/my-new-feature’ will be created (I used the default prefix when I initialised this repository). You commit your feature implementations to this branch. If you want, you can push the feature branch to your remote while you work on it (your team can decide on whether that’s normal practice or not).
If at any time you want to switch branches, either to another feature branch or to somewhere else, just use the normal mechanisms in SourceTree to do that, such as double-clicking a log entry or a branch in the sidebar. Git-flow determines your context simply from the branch you currently have checked out, so it’s fine to jump around if you like.
Finishing a feature
Eventually when you’re done implementing a feature, use the ‘Finish Feature’ action (again, this will be the default action from the toolbar button if you’re on a feature branch):
Again, the Preview shows you what will happen — the feature branch will merge into the main development branch, essentially queueing it for inclusion in the next release. Feature branches are deleted by default but you can opt to retain them if you like.
Starting a release
You start a release branch when you want to start preparing a new release, which probably coincides with a feature freeze. Accessing the Start Release option –either from the menu or from the toolbar action selection window — brings up the following dialog:
The preview shows that a new release branch will be created. Most of the time, you want to start the release from the latest commit in the development branch, but you can choose to base it on another commit if you wish. This essentially freezes the release, so it’s not affected by subsequent development. You can also perform preparatory tasks for the release process on this branch, such as updating the version number in source files, updating changelogs, or committing other tweaks. Once these tweaks are done, you can finish the release as described below.
Finishing a release
Once all the adjustments required for the release are done and committed, you can conclude the release process.If you’re on the release branch already, the git-flow button will show you the following dialog:
The preview illustrates that the release branch will be merged into the production branch (‘master’ or ‘default’ normally) to indicate an update to the deployed version. Sourcetree will also create a tag here for the release. The release branch changes will also merge back into the development branch to make sure the develop branch is kept up to date.
Starting a hotfix
What if you need to just fix a bug on the latest release? You don’t want to create a new release, because that will pick up the recent changes from the development branch. So instead, you can start a hot fix:
Hot fixes always start from the latest production code from the ‘master’ or ‘default’ branch. Other than that, they’re basically the same as release branches. Moreover, when you’re finished with the hot-fix, they behave the same way as finishing a release branch. The changes are merged back into both the production branch and the development branch, and a tag is created on the production branch for the hot fix release.
Wrapping up
Git-flow is a great way to automate your handling of branch-based development in Git, and SourceTree now provides a simple and clear way to use it with an easy-to-use and intuitive GUI. Big thanks to Vincent Driessen for coming up with git-flow in the first place!
Try the Git-flow workflow yourself with Sourcetree 1.5!
52 Comments
A real pity that you only offer Git-flow for Mac OS X and neither for Windows nor for Linux.
an interesting reversal of the common practice of releasing for windows first and mac later (maybe)…
No real developers use Windows anymore. This is why. There’s no “GitHub for Linux” or “GitHub for Windblows” either.
You are wrong, there is GitHub for Windows http://windows.github.com/
Your hideous neckbeard is showing.
Obvious fanboy. No serious programmer uses OSX, a designer maybe.
So what about iOS developers out there? Your ignorance and narrow minded attitude amazes me.
I do, and have made over £2m doing so. I’d say that was pretty serious.
Does it really matter what OS or platform you’re on?
Seems like pointless arguing to me.
All that matters are your developer skills and mindset. Carry on!
You are narrow minded!
Elitist twat – get off your high horse. Real developers use whatever suits them best.
git itself wasn’t available for windows for _years_ after it was introduced. Nobody cared. Windows is for old people and big slow stagnant corporations.
Like stackoverflow?
I will pay money for this on Linux.
How much money?
This is already made for linux and windows and osx.. the git flow is set of scripts that you can get and install.. then use via command line like git flow feature start alabala.. sourcetree is a git gui that will issue those command for you.
For Hg, the main branch for development is typically “default.” Is there a reason the GUI is setting production to “default” or “master” when initializing the flow?
When using hg-flow, ‘default’ is the production branch, not the development branch, just like the production branch is ‘master’ on git-flow. In both the recommended style is to use a new branch called ‘develop’ for development. SourceTree defaults to these conventions because git-flow and hg-flow use them.
Git Flow and Subversion
Any chance SourceTree might be able to support a git-flow workflow for Git clones of Subversion repositories?
Git-flow uses the `–no-ff` flag to ensure that a merge commit is always created to preserve the information about the historical existence of a feature branch, but this approach conflicts with the recommendation for git-svn to avoid such merge commits by rebasing.
Could SourceTree include some logic that would enforce the use of the `–no-ff` flag when starting and finishing features, yet still default to rebasing whenever a feature or release branch is pushed upstream to the `git-svn` remote?
You could make this work for features by selecting the ‘rebase’ option when finishing a feature, but in general git-flow and git-svn are just not very compatible, because hotfixes always need to use merging, and as you know git merges can’t be sent to Subversion.
Maybe you could track your git-svn branch in a completely different local branch, called ‘svn-upstream- or similar, then work with git-flow in the usual branches, then when you want to send things upstream do a git merge –squash (not exposed in SourceTree yet though) into the svn-upstream branch, replicating the changes without needing the merge metadata.
Generally though, I think I’d have to consider git-svn to be fundamentally incompatible with git-flow. Another reason to encourage the masters of the upstream repo to switch to DVCS 😉
spent two day cramming hg/hg flow and sourcetree… so far so good. one problem I have though is when I finish a release the TAG WITH THIS MESSAGE is greyed out. in the screenshot above theyre enabled. I thought one of the tenet so Hg/git Flow was to tag the default/master when you finish a release or do I finish then tag default. I presume its the tagged default branch is where I ultimately package up my code for the App Store say?
I’ve tested on two separate repos.
This is actually because hg-flow doesn’t support messages on tags right now. The same UI is used for git-flow and hg-flow but some options are disabled where they’re not available.
ok cheers. One other thing I noticed while using it is that it would be nice to assign a color to branches ourselves. the light blue and light pink swap when you switch branch. Not a problem when you are on the main screen as I only switch branches using the toolbar on the left. But when you click on merge youre only shown the revisions table and asked to pick a branch to merge in. The graph lines on the left also move around so can be confusing. Is there a bug site/wishlist site? cheers. Also adding support for DeltaWalker for diff would be good too.
Yes, this has been requested once before, you can add your 2c and/or upvote the feature request here (backlog priorities are very much influenced by the number of user requests): https://jira.atlassian.com/browse/SRCTREE-1191
I did try to support Deltawalker once before but found it to be very unreliable and buggy, such that I didn’t want to officially stand behind it as fully supported. This was a while ago now though. You can actually add it yourself by using the ‘Other’ option and providing the settings yourself – any diff/merge tool can be configured that way.
You can actually add it yourself by using the ‘Other’ option and providing the settings yourself – any diff/merge tool can be configured that way.
a very good thought, +100500
more
has a small issue. am using 1.0.5.0 on windows.
when finishing a feature (merging back to develop) it always goes for fast-forward if possible even if you have disabled it in the preferences, this has the adverse affect of losing branch history. may be it’s just me. am doing Git Flow manually for now.
Yeah, it’s in the git-flow code to fast-forward on feature finish if there have been no changes in the destination branch, there’s no option to turn that off in the git-flow script hence why the ST option doesn’t affect it.
Thank you for reply Steve, that explains it. so should I file this as an issue with gitflow script on GitHub? no idea why Vincent said “no fast forwards” in the blog post and didn’t do it in the script 🙂
turns out this is a known issue: https://github.com/nvie/gitflow/issues/292
there was a pull request for it about 5 months ago which hasn’t been merged yet.
some say this fork (https://github.com/petervanderdoes/gitflow) is better.
Thank you for the great tool, I always used command line git (on Mac) before Source Tree and now I use it on windows as well.
Is it possible to finish a feature/release/hot-fix with a pull request instead of directly merging it?
Yes, we would need this too, because how are we able to do code reviews if the code is merged on develop with no pull request from the feature branch to develop?
You can do this with SourceTree for Mac 1.7 and SourceTree for Windows 1.2. Just submit a pull request from within SourceTree and when the pull request is accepted, your fetch will bring that merge in to your local copy, effectively finishing the feature.
Thanks for the tip!
Sorry to resurrect this thread from the dead — quick question on this flow though. If I create a pull request from a feature, GitHub is always trying to merge into “master”, instead of into “develop”. It’s easy enough to change on the GitHub interface, but I just wanted to make sure I wasn’t missing something obvious on the SourceTree side.
This is the default GitHub behaviour, if the target project is git-flow based then you want to change the default branch to ‘develop’ in the Settings page on GitHub so that the default merge target for pull requests is develop instead of master.
Holy cow! Thanks so much for the quick response. Much appreciated. Ahhh… that makes sense. We’re just contemplating switching to git-flow, so I’m still getting a handle on the ins and outs. I sent you a tweet as well, please feel free to ignore that. Really really awesome support! Thanks Steve!
I have an existing HG repository with several feature branches and the ‘default’ branch for production and a ‘Test’ branch which contains several features that are in test but are not ready to go to production yet.
If I want to start using HG Flow on this repo, how do I deal with the existing branches? I do not want to loose the features included in my ‘Test’ branch.
You won’t lose any existing branches, it would only be an issue if one of your existing branches had the same name as one of the hgflow ‘special’ branches like develop. Any other branches will remain as normal and you’re free to manually manage those if you want.
Well I took the plunge…
and not all went well. The ‘develop’ branch was not created and another branch had many changes, as if SourceTree tried to make that branch the ‘develop’ branch. I could not discard these changes, so I had to commit them.
After that I was able to create the ‘development’ branch manually from the ‘default’ branch
Hello all,
i installed Source tree setup and clone my project but now master barch selected in source tree. but i want to change branch. so how to change branch can any help me please
Please use https://answers.atlassian.com for community questions in future, but the quickest way to switch branch is just double-click on the branch in the sidebar or in the log.
Really enjoying using SourceTree.app in lieu of the command line and assorted IDE plugins as it really has simplified the SCM workflow which is significantly enhanced with the addition of the Git-flow workflow too.
I started a feature sometime with git Flow but did not finish it as suppose. Instead I mistakenly right click on the feature and delete it using sourceTree GUI. Now if I try creating new feature, it automatically append the name of that feature I deleted and I’m guessing its because I didn’t finish the feature with git Flow. Please how do I get rid of that feature give that I don’t have it anymore.. as I want my new feature to have it own given name not the name of the previous feature I forgot to finish. Thanks
Just deleting the branch should have freed up the name for use again. The only problem would be if you’d pushed the branch to your remote, in which case you should delete the remote branch via the interface on your host, e.g. in Bitbucket you can do it in the branches view:
an old blog post… but still useful. Thank you
“If you want, you can push the feature branch to your remote while you work on it (your team can decide on whether that’s normal practice or not).” Isn’t not pushing to remote extremely dangerous? What if you’ve been working on a feature for a week and then your computer crashes. If you never pushed to remote you just lost a week’s worth of development. Is there an equally bad possibility of pushing to remote?
An issue came up in StackOverflow
http://stackoverflow.com/questions/27215383/why-doesnt-the-graph-display-a-new-branch-branching-off-from-the-master-branch/31252046#31252046
that if e.g. you do an initial commit to [master], followed by a branch to [develop] and perhaps another branch to [feature1] (as suggested in Vincent Dreissen’s A Successful Git-Branching Model) GitFlow shows this as a linear trail, all in blue color, whereas it should actually depict a branching trail that changes color between the nodes. I realize that after you do the merges (merge develop & feature, merge master & develop) the graph looks OK in the end, but it sure is confusing for noobies to branch and branch and to see no new branches on GitFlow.
Cancelling a hotfix?
Especially when there is a merge conflict when finishing a hotfix and sourcetree glitches out and you can “finish” the fix over and over and it still won’t disappear locally so you have to manually delete the local hotfix branch…
Sometimes when you code. You run into coding problems. Also sometimes it makes you turn into a unicorn. This happens alot. Any help appreciate. thank you
WHERE DID MY COMMENT GOOOOO >:((
using sourcetree app in IOS ,wehen clone a GIT advance option is not working..
Any one please help..urgent
I have tried to use this and bumped into the following situation:
when trying to finish a Release-branch or a hotfix-branch, it will only work if there was meanwhile no change in the develop branch. but in reality, this is almost always the case and Sourctree GitFlow fails. I resolved this by manually merge my Release-branch (or hotfix-branch) to the master first, after that I pull the latest development branch into it, then I marge it to the develop branch (and then I manually delete my Release or hotfix branch)
Am I doing something wrong here?
13 Trackbacks
[…] Above that, the people who created Sourcetree, have implemented it in their app and have provided a nice write-up of all the branches used in the model. […]
[…] Git-flow with a GUI (sourcetreeapp.com) Rate this:Share this:TwitterPrintFacebookTumblrEmailPinterestDiggRedditStumbleUponLinkedInLike this:LikeBe the first to like this. […]
[…] this example, you are the only person working on that feature branch. This approach is common with git-flow and hg-flow for example. This feature branch may take a while to complete, and you’ll only want to […]
[…] Jeff Kreeftmeijer. Though I prefer using the terminal when working with git my favorite Mac Client Sourcetree supports git-flow since the version 1.5. There is still much to learn about Git and I’m […]
[…] And: http://blog.sourcetreeapp.com/2012/08/01/smart-branching-with-sourcetree-and-git-flow/, http://yakiloo.com/getting-started-git-flow/ […]
[…] like some more detail on what Git Flow is about and how SourceTree helps you use it, the blog post Smart branching with SourceTree and Git-flow is a good place to start. We wrote it for the Mac version, but the Windows version operates in […]
[…] GitFlow is such a branching model that we’ll be taking a look at now and how to use it with SourceTree and Visual Studio (at least how I use […]
[…] If you’d like some more detail on what Git Flow is, check out Atlassian’s Git Flow tutorial or our blog Smart branching with SourceTree and Git Flow […]
[…] 在sourcetree的官方博客上,有一篇文章讲述了怎么使用这个功能。链接:Smart branching with SourceTree and Git-flow […]
[…] Another very strong feature is that SourceTree supports both HG Flow and Git Flow out of the box: no need to use the flow command-line tools here. There is a good Git Flow guide here: Smart branching with SourceTree and Git-flow | SourceTree Blog. […]
[…] Additional handy links: https://www.atlassian.com/git/workflows#!workflow-gitflow http://nvie.com/posts/a-successful-git-branching-model/ http://blog.sourcetreeapp.com/2012/08/01/smart-branching-with-sourcetree-and-git-flow/ […]
[…] Use SourceTree with GitFlow – SourceTree is an awesome graphical way to manage your source control through GitHub, BitBucket, etc. GitFlow is a set of conventions for better streamlining how you manager your code. Put the two of these together and you really do have a match made in heaven. You can read more about SourceTree and GitFlow here. […]
[…] Smart branching with SourceTree and Git-flow http://blog.sourcetreeapp.com/2012/08/01/smart-branching-with-sourcetree-and-git-flow/ […]