Never Commit to Master – An Introduction to Git Flow



Never Commit to Master – An Introduction to Git Flow

0 0


never-commit-to-master


On Github opdavies / never-commit-to-master

Never Commit to Master

An Introduction to Git Flow

By Oliver Davies / @opdavies

Drupal for 8 years, Git since for 4 years, Git Flow for a year

Me

  • Oliver Davies
  • Developer and Systems Administrator
  • @opdavies (Twitter, D.O., IRC)
  • http://dgo.to/@opdavies
  • PHPer and Drupalista since 2007 (full time since 2010)
  • Git user since 2010
  • Git Flow user since 2013

Git Flow is:

"A collection of Git extensions to provide high-level repository operations for Vincent Driessen's branching model."

From https://github.com/nvie/gitflow

* Read quote from slide * Provides a wrapper around standard Git functionality Everything in Git Flow can be done manually

The Branching Model

From

Separates production code from development code Encourages feature driven development Allows for releasing and tagging of code Multiple opportunities for QA and review

Branches

  • master: production code
  • develop: development code
  • feature/: a specific task or ticket (multiple)
  • release/: temporary release branch for testing (single)
  • hotfix/: temporary branch for emergency fixes
  • suport/: experimental
master: stable production code, tagged at the end of each release (stable) develop: stores tested and verified development code, awaiting deployment (fairly stable) feature: unstable code relating to a specific task, bug or user story release: hotfix: Used to apply urgent fixes to production code

Why use Git Flow?

  • Separation of production and development code
  • Flexibility
  • Better code quality
  • Encourages collaboration
  • Encourages peer code reviews
Separates code by using different branches Being able to easily switch between features Continuous testing of feature and release branches Collaboration through sharing of standard branch names Code reviews

My rules of Git Flow

Never, ever commit code directly to master Only commit stable code to develop Try not to commit directly to develop One feature branch per user story/bug Commit early, commit often, push often

How do I use it?

  • CLI
    • ~ $ brew install git-flow
    • ~ $ apt-get install git-flow
    • ~ $ yum install gitflow
  • SourceTree (free, cross-platform GUI)
You can install and use Git Flow on the command line. There are packages for Ubuntu (apt), Red Hat and CentOS (yum) and OS X (Homebrew). You can also download SourceTree from Atlassian which is cross-platform

Need Help?

~ $ git flow help
# Shows the standard help menu

~ $ git flow {subcommand} help
# Shows the help menu for a specific subcommand

Initialise Git Flow

~ $ git flow init
This can be done at any time, with existing commits

Create your default branches

~ $ git flow init

No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

Configure branch prefixes

~ $ git flow init

No branches exist yet. Base branches must be created now.
Branch name for production releases: [master]
Branch name for "next release" development: [develop]

How to name your supporting branch prefixes?
Feature branches? [feature/]
Release branches? [release/]
Hotfix branches? [hotfix/]
Support branches? [support/]
Version tag prefix? []

Tip: Automatically accept the default branch names

~ $ git flow init -d
# Accepts the default branch names.

Adding -d accepts the default branch names.

Features

$ git flow feature

  • list: lists all features
  • checkout: checks out an existing feature
  • start: starts a new feature
  • finish: finishes a feature
  • publish: pushes a feature into a remote repo
  • pull: pulls a feature from a remote repo
Checkout can be a full feature name or a prefix

Start a new feature branch

~ $ git flow feature start {name}
~ $ git flow feature start foo

Switched to a new branch 'feature/foo'

Summary of actions:
- A new branch 'feature/foo' was created, based on 'develop'
- You are now on branch 'feature/foo'

Now, start committing on your feature. When done, use:

git flow feature finish foo
* Branches from develop

Add and commit changes

~ $ drush dl admin_menu
~ $ git add sites/all/modules/contrib/admin_menu
~ $ git commit -m "Added admin_menu"

Recommended: Rebase Your Feature

Ensure that your feature is up-to-date

~ $ git flow feature rebase

Will try to rebase 'foo'...
First, rewinding head to replay your work on top of it...
Applying: Added admin_menu
Rewinds the feature, updates from develop and re-applies the feature changes Ensures that the feature code is up to date and will merge correctly.

Finish a feature

~ $ git flow feature finish {name}
~ $ git flow feature finish foo

Switched to branch 'develop'
Updating 5c04d5a..6487134
Fast-forward
...
31 files changed, 5051 insertions(+)
Deleted branch feature/foo (was 6487134).

Summary of actions:
- The feature branch 'feature/foo' was merged into 'develop'
- Feature branch 'feature/foo' has been removed
- You are now on branch 'develop'

And repeat...

Releases

$ git flow release

  • list: lists existing releases
  • start: starts a new release
  • finish: finishes a release
Creates a new version of the prod code I tend to do this at the end of each sprint

Start a new release

~ $ git flow release start {version}
~ $ git flow release start 2014-03-02.0

Switched to a new branch 'release/2014-03-02.0'

Summary of actions:
- A new branch 'release/2014-03-02.0' was created, based on 'develop'
- You are now on branch 'release/2014-03-02.0'

Follow-up actions:
- Bump the version number now!
- Start committing last-minute fixes in preparing your release
- When done, run:

     git flow release finish '2014-03-02.0'
Final commits or bug fixes Database export

Finish a release

~ $ git flow release finish {version}
~ $ git flow release finish 2014-03-02.0

  ...

  Deleted branch release/2014-03-02.0 (was f2aee7d).

  Summary of actions:
  - Latest objects have been fetched from 'origin'
  - Release branch has been merged into 'master'
  - The release was tagged '2014-03-02.0'
  - Release branch has been back-merged into 'develop'
  - Release branch 'release/2014-03-02.0' has been deleted
Merges develop into master Enter a tag message Tags the release with the specified tag Advanced options for release tag singing with GPG

Pushing changes remotely

~ $ git push --all
# Push the changes to the remote branches.

~ $ git push --tags
# Push the tags.

Tip: Finish a release in one command

~ $ git flow release finish -pm {message} {version}
# Specify a commit message and automatically push the changes.
~ $ git flow release finish -pm 2014-03-02.0 2014-03-02.0
$ git flow release finish -pm "tag message" {branch}

finish-sprint.sh

~ $ ./finish-sprint.sh 2014-03-02.1

#!/bin/bash

DRUPAL_DIR="/path/to/drupal/docroot"
TAG=$1

if [ -z $TAG ]; then
  # A tag must be specified.
  echo 'You must specify a tag.'
fi

# Go into the Drupal directory
cd $DRUPAL_DIR

# Start a new Git Flow release.
git flow release start $TAG -F

# Flush the cache.
drush cc all

# Export the database
drush sql-dump --gzip --result-file=../db/$TAG.sql
git add ../db/$TAG.sql
git commit -m "Exported database for $TAG"

# Finish and push the release
git flow release finish -pm $TAG $TAG

Hotfixes

$ git flow hotfix

  • list: list all hotfixes
  • start: start a hotfix
  • finish: finish a hotfix
Applies an emergency fix to your production code Branches from master (by default) Merges back into master and develop

Create a new hot fix

~ $ git flow hotfix start {version}
~ $ git flow hotfix start 2014-03-02.2

Switched to a new branch 'hotfix/2014-03-02.2'

Summary of actions:
- A new branch 'hotfix/2014-03-02.2' was created, based on 'master'
- You are now on branch 'hotfix/2014-03-02.2'

Follow-up actions:
- Bump the version number now!
- Start committing your hot fixes
- When done, run:

     git flow hotfix finish '2014-03-02.2'

Commit your fixes

~ $ git ci -am 'Updated .htaccess'
[hotfix/2014-03-02.2 6d04738] Updated .htaccess
 1 file changed, 4 insertions(+), 4 deletions(-)

Finish the hot fix

~ $ git flow hotfix finish 2014-03-02.2
Switched to branch 'master'
Merge made by the 'recursive' strategy.
 .htaccess | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Switched to branch 'develop'
Merge made by the 'recursive' strategy.
 .htaccess | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Deleted branch hotfix/2014-03-02.2 (was 6d04738).

Summary of actions:
- Latest objects have been fetched from 'origin'
- Hotfix branch has been merged into 'master'
- The hotfix was tagged '2014-03-02.2'
- Hotfix branch has been back-merged into 'develop'
- Hotfix branch 'hotfix/2014-03-02.2' has been deleted

Resources

Demo

Questions?

Thanks

Feedback appreciated!