Skip to content

Instantly share code, notes, and snippets.

@markreid
Created January 17, 2017 04:30
Show Gist options
  • Star 76 You must be signed in to star a gist
  • Fork 30 You must be signed in to fork a gist
  • Save markreid/12e7c2203916b93d23c27a263f6091a0 to your computer and use it in GitHub Desktop.
Save markreid/12e7c2203916b93d23c27a263f6091a0 to your computer and use it in GitHub Desktop.
git flow with rebasing

Git Flow (with rebasing)

See http://nvie.com/posts/a-successful-git-branching-model/

Features

A feature is based off the develop branch and merged back into the develop branch. It will eventually get into master when we make a release.

Working Locally

# checkout develop, fetch the latest changes and pull them from remote into local
git checkout develop
git fetch
git pull origin develop

# create a feature branch that is based off develop
git checkout -b feature/XX-123/some-description

# do your work
git add something
git commit -m "first commit"
git add another
git commit -m "second commit"

# rebase against develop to pull in any changes that have been made
# since you started your feature branch.
git fetch
git rebase origin/develop

# push your local changes up to the remote
git push

# if you've already pushed changes and have rebased, your history has changed
# so you will need to force the push
git fetch
git rebase origin/develop
git push --force-with-lease

GitHub workflow

  • Open a Pull Request against develop
  • When the Pull Request has been approved, merge using squash and merge, adding the ticket number and a brief description: ie, MQ-330 enable users to order a pizza from the dashboard.
  • This squashes all your commits into a single clean commit.

If you are unable to squash merge because of conflicts, you need to rebase against develop again:

# in your feature branch
git fetch
git rebase origin/develop
git push --force-with-lease

Releases

A release takes the changes in develop and applies them to master.

Working locally

# create a release branch from develop
git checkout develop
git fetch
git pull origin develop
git checkout -b release/3.2.1

# finalise the change log, local build, etc
git add CHANGELOG.md
git commit -m "Changelog"

# rebase against master, which we're going to merge into
git fetch
git rebase origin/master
git push --force-with-lease

Usually at this point you will want to deploy the release branch to the staging server for final QA. If there are any issues, fixes should be committed to the release branch.

Github workflow

  • Open a Pull Request against master
  • When the PR is approved and the staging deploy has been verified by QA, merge using rebase and merge.
  • DO NOT SQUASH MERGE. We don't want a single commit for the release, we want to maintain the feature commits in the history.
  • Repeat the steps above against develop (may need to rebase first).
  • Tag a release on master. Use the version number and put the changelog in the description.

Hotfixes

A hotfix is a patch that needs to go directly into master without going through the regular release process. The most common use case is to patch a bug that's on production when develop contains code that isn't yet ready for release.

Working locally

# create a hotfix branch based on master, because master is what will be deployed to production
git checkout master
git fetch
git pull origin master
git checkout -b hotfix/describe-the-problem

git add patch.fix
git commit -m "fix the problem"
git push

Github workflow

  • Open a Pull Request against master
  • When the PR's approved and the code is tested, squash and merge to squash your commits into a single commit.
  • Open a Pull Request against develop (may need to rebase first).
  • Tag a release on master. Describe the issue in the name, feel free to put details in the description.
@khauser
Copy link

khauser commented Jun 24, 2022

@meenie: He is not rebasing master back into develop. Instead he creates a PR to merge his release also back into develop after he has done it for master. But that's only needed if the release contains changes made only there (maybe fixes, changelog, version-numbers). In case of hotfix-branches you always have to bring their changes also into develop.

But another problem we encountered is that the rebase and merge into master via PR always results in conflicts in the next PR (see: https://github.com/khauser/test_gitflow_with_rebase/pull/9). As far as I understand it rebase and merge also changes history. That's why develop and master get incompatible. Then indeed master needs to be merged into develop, or better develop needs to be reset to master. Merge might be worser than reset..
With normal merges from hotfix- or release-Branches into master the problem disappeared. And still squash and merge could be used when bringing features into develop.

Please tell me your thoughts about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment