So You Want Your Team To Start Using Git? - Part 4: Team Workflows
Join the DZone community and get the full member experience.Join For Free
so you’ve got your own personal git use dialled in, you’ve got a server environment configured with commits flying into your project like crazy. now you’re ready to start leveraging the power of git with others – new workflows exist for distributed version control systems that offer great gains in terms of developer workflow, interaction, quality assurance and overall delivery process. let’s take a look at one of them – gitflow, so we can turn your team’s git usage up to 11.
one of the problems that using source control in a team with any more than just a few developers is a common one on nearly any platform. developers editing the same files and working on the same projects and treading on each others toes – it’s a recipe for disaster.
every source control tooling i've used over the years has had a different community approach to how this issue is dealt with; an informal “gold standard” for branching and merging – the git community’s most common answer to this is an approach/workflow named “gitflow”.
in essence gitflow is a branching strategy that steals from a number of very successful strategies from cvs communities (svn/tfs) and mashes them together.
a really good early write up of using gitflow can be found here.
at it’s core gitflow uses the speed, simplicity and power of git’s branching and merging features to take full advantage of this for your teams betterment.
it operates on a few key principles and assumptions.
- commits are cheap.
- branches are cheap.
- because merging in git is so super pain free, it’s a great place to place for code reviews/peer review.
- teams need an approach to building features away from production code.
- teams need an approach to rolling out hotfixes without mixing in any untested development code.
- having a way to record previous releases is an important way of story workflow progression (tagging).
at a high level, gitflow is the use of git’s branching strategy to take care of a need for hotfixes, releases, development and team feature branches.
this translates directly into the following:
your current latest production ready codebase at any time resides in the “master” branch. this is only merged into once a release has successfully gone out and is tagged for easy rollback.
your “develop” branch is your currently integrated work in progress branch. if you’re deploying your staging or pre-prod integration codebase from here, you’re probably doing it right :-)
feature branches for everything
branching in git is cheap in terms of speed and disk space - mostly because it’s not how you’d normally think of branching . merging is super easy and mostly merge conflict free. so every time you pick up a new user story or backlog item, create a new branch.
release branching is used for pre-release tweaks/integration
it’s not rare for you to work towards a release and have to make some last minute tweaks against your highly integrated “develop” codebase. for this we create a branch of “develop” and use it to pre-merge “master” into it resolving any last minute kinks while allowing your team to carry on.
support to work on hotfixes as a first class citizen
someone’s reported a sev 1/critical/end-of-world-drop-everything bug, and you need to just tweak something quickly to resolve it. branching off master for a hotfix before a quick reintegration and double merge back into “master” and “develop” makes this easy.
tagging to remember all of your teams greatest hits
tagging releases with semantic versioning conventions make rolling back a piece of cake.
down to business
whether you’re in the command line, or using the sourcetree gui implementing gitflow is easy. sourcetree actually makes this even easier by supporting gitflow natively with an easy to use wrapper over the top. want a new feature? simply use the button just for that!
working on code away from the current release
the first thing any team wants to ensure is that you’ve got a stable working copy of your source code to deploy at the drop of a hat while you continue working.
the way gitflow approaches this is with two standard branches.
the “ master ” branch is where we keep our stable source codebase. our current releases.
we then create a branch called “ develop ” to store your current development integration branch. if you like to run continuous integration and deployment for your team’s current stable development work then this might be where you’d do it.
to set this up in the command prompt or with your git bash:
$ git checkout master $ git checkout -b develop
this checks out your current master branch and then branches it into a new branch named “develop” for you to continue your work in.
to get this some setup in source tree while also letting source tree know that you want to use gitflow:
open source tree with your current repository open.
click on the “gitflow” icon:
this will then bring up a dialog asking you what defaults you’d like to use for your branch naming conventions. i suggest just going with the defaults.
press ok, and source tree will create you a new “develop” branch and check it out.
if you open up your .git folder and look closely you’ll notice that sourcetree has also edited your “.git\config” file to add your gitflow configuration (** starts the new bits **).
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true hidedotfiles = dotgitonly ** [gitflow "branch"] master = master develop = develop [gitflow "prefix"] feature = feature/ release = release/ hotfix = hotfix/ support = support/ versiontag =
with this completed you’re now ready to get going using gitflow with sourcetree.
you then want to make sure you push this new “develop” branch up to your remote so your team can see it. by default this won’t occur, so we need to make sure your remote knows about the branch.
on the command line:
$ git push origin feature/user-story-123
in source tree you simply press the “push” icon, and make sure you’ve checked your new develop branch.
separate the different pieces of work you and your team are developing
i mentioned above that git was great because commits and branches are so cheap. this makes working in parallel really easy as everyone can work with their own branch.
gitflow gives you a framework for taking this even further, and guiding you to use a new branch for every new piece of work – by creating “feature” branches.
to follow this convention, every time you want to work on something new, checkout your develop branch, and then branch from it.
$ git checkout develop
$ git checkout -b feature/user-story-123
protip: if you’re big into using gitflow on the commandline, there is actually a set of bash extensions that make this more implicit. give them a go to save yourself the keystrokes.
if you’re using source tree you do this by checking out your “develop” branch (right click on it and choose “checkout”).
then press the gitflow icon and select “start new feature”
then give your feature a name, hit ok and you’ve created a new feature branch and checked it out.
after you’ve done some work you need to make sure you push this new branch to your remote so your team can see it and potentially work on it also.
on the command line:
$ git push origin feature/user-story-123
in source tree you do this the same way you did with your develop branch, press the “push” icon and select your new feature branch.
many commits later you’ve finished your work.
you’re ready to close out your feature and merge it back into “develop”.
we need to merge your feature branch into “develop”
on the command line:
$ git checkout develop $ git merge feature/user-story-123 $ git branch –d feature/user-story-123
this merges“develop” into your feature, then checks out “develop” and merges your feature into develop, then deletes develop.
if you suffer from a merge conflict on the command line, i recommend this guide to help walk you through an interactive command line merge.
in source tree:
click on the gitflow icon.
then press “finish feature”.
you can then select the feature branch you’d like to finalise and click ok.
both of the above use cases will take the git default of conducting a “ fast forward ” merge if possible, which can be a little unsettling if you’ve never used git before. this is because depending on what’s gone on in the “develop” since you branched, any visibility over what went on in your feature branch may have disappeared on the merge. in the case that nothing has changed since you branched, your feature branch has now become the past for the “develop” branch. you can change this behaviour, but have a read so you understand whether this matters to you or not.
in the case that you encounter a merge conflict because your team has made changes to “develop” while you were working, you can follow this guide to help you work through a merge conflict with source tree and git (hint; it’s quite easy).
you’ve now completed your first feature branch using gitflow. this allows each of your team to work on separate pieces of functionality without affecting each other while at the same time keeping a clean development integration copy of your team’s work in progress and your current production release of code for a redeployment or rollback at anytime.
starting a new release and segregating it so work can carry on
another important team productivity boost from gitflow is it’s ability for you to “work on a release” while normal development work continues in “develop” and your feature branches. it does this by taking a branch of your current “develop” branch named “releases/0.1” (following semantic versioning conventions). this allow your team to continue working while some of your team or devops guys finalise your release.
to accomplish this what we’re going to do:
- checkout “develop”
- branch “develop” into “release/0.1”
- make some last minute changes.
- merge “release/0.1” into master and “develop”.
- delete our release branch.
to do this on the command line:
$ git checkout develop $ git checkout -b release/v0.1
now make any commits you need to finalise your release.
when you’re done, it’s time to merge into “master” and “develop”, and tag your release on the command line.
$ git checkout develop $ git merge release/v0.1 $ git checkout master $ git merge release/v0.1 $ git tag -a v0.1 -m 'our release of version v0.1' $ git branch –d release/v0.1
to do this in source tree:
checkout your “develop” branch by right clicking on “develop” and selecting “checkout “develop branch”.
now click on the “gitflow” icon.
now press the “start new release” button.
give your release a name. i’m naming mine “v0.1” as it’s my first release.
you are now checked out into your new release branch.
you can then make any final changes and commits, while your team keeps working on the next one.
when you are done with your release changes, you finish your release by again pressing the gitflow icon.
press the “finish release” button.
enter a comment for your release finalisation message and press ok.
and with that you’ve finished your first release!
fixing production issues without stopping the music
the last thing that most teams need from a great source control workflow is the ability to take into account out-of-band hotfixes for bugs found in production.
gitflow has you covered here, as it includes a branching strategy for hotfixes. this is incredibly important, because a hotfix to production is usually a small tweak, and you want to make it only to your current production codebase while keeping it very segregated from unstable changes happening elsewhere in your repository . by using gitflow to create a branch of “master” specifically to work on this fix, you allow your team to continue on it’s work in “develop” or feature branches without slowing them down at all – all while being incredibly safe.
what this means in reality:
- your team finds a bug in production.
- you create a branch of “master” named “hotfix/v0.1.1” or similar.
- your team completes a fix by committing to this branch.
- you merge this hotfix branch back into “master” and “develop” and delete the release branch.
- you tag this merged commit for later rollback.
doing this on the command line:
$ git checkout master $ git checkout -b hotfix/v0.1.1
now make any commits you need to finalise your hotfix.
these commits will be kept separate from everything else going on in your repo.
when completed with your hotfix, it’s time to merge it into “master” and “develop” and delete it.
$ git checkout master $ git merge hotfix/v0.1.1 $ git checkout developer $ git merge hotfix/v0.1.1 $ git tag -a v0.1.1 -m 'fix a cross browser compatibility issue on homepage' $ git branch -d hotfix/v0.1.1
doing this in source tree:
checkout your “master” branch by right clicking on it on the left and opting the “checkout master”
now press the “gitflow” icon to open the gitflow menu.
select the option for “start new hotfix”
give your hotfix a name.
you’ll notice that source tree gives your branch the name “hotfix/v0.1.1” to follow the gitflow naming convention.
then work with and commit your fixes to this branch.
when you’re done we’ll finish the hotfix, by pressing the gitflow icon again and then selecting “finish hotfix”
now enter a description for your closing merge, and press ok.
when you action this merge you’ll notice that source tree has also tagged your hotfix release with the name of your hotfix.
and with that you’ve also taken care of fixing a bug in production while your team continues to work.
i hope the last 4 parts of this blog series helped you get a start using git with other people in your team – like a lot of new development team approaches i wouldn’t expect your team to be git gurus on day one, but i hope these posts are enough to get you and your team started.
checkout the rest of the posts in this series:
- part 1: getting started
- part 2: pushing it up somewhere
- part 3: more than just committing
- part 4: team workflows
Published at DZone with permission of Douglas Rathbone, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.