Difference between revisions of "Developer Guide - Git Workflows"

From bering-uClibc
Jump to: navigation, search
(Managing Branches paragraph)
m (Usefull commands correct typo)
 
(38 intermediate revisions by one other user not shown)
Line 15: Line 15:
  
 
There is a tradeoff of course: merges require a more careful branch management. The following subsections discuss the important points.
 
There is a tradeoff of course: merges require a more careful branch management. The following subsections discuss the important points.
 +
 +
=== Graduation ===
 +
As a given feature goes from experimental to stable, it also "graduates" between the corresponding branches of the software. Bering-uClibc uses the following integration branches:
 +
* <span style="color:#8B4513">'''''maint'''''</span> tracks the commits that should go into the next "maintenance release", i.e., update of the last released stable version;
 +
* <span style="color:#8B4513">'''''master'''''</span> tracks the commits that should go into the next release;
 +
* <span style="color:#8B4513">'''''next'''''</span> is intended as a testing branch for topics being tested for stability for master.
 +
 +
There is a fourth official branch that is used slightly differently:
 +
* <span style="color:#8B4513">'''''pu'''''</span> (proposed updates) is an throw-away integration branch for things that are not quite ready for inclusion yet (see [[#Rule: Throw-away integration branches]] below).
 +
 +
Each of the four branches is usually a direct descendant of the one above it.
 +
 +
Conceptually, the feature enters at an unstable branch (usually <span style="color:#8B4513">'''''next'''''</span> or <span style="color:#8B4513">'''''pu'''''</span>), and "graduates" to <span style="color:#8B4513">'''''master'''''</span> for the next release once it is considered stable enough.
 +
 +
=== Merging upwards ===
 +
 +
The "downwards graduation" (for example from <span style="color:#8B4513">'''''next'''''</span> to <span style="color:#8B4513">'''''master'''''</span>) discussed above  <span style="color:#D00000">cannot be done by actually '''merging downwards'''</span>, however, since that would <span style="color:#D00000">merge '''all''' changes on the unstable branch into the stable one</span>. Hence the following:
 +
 +
==== <span style="color:green">Rule: '''Merge upwards'''</span> ====
 +
 +
Always commit your fixes to the '''oldest supported branch that require them'''. Then (periodically) merge the integration branches upwards into each other.
 +
 +
This gives a very controlled flow of fixes. If you notice that you have applied a fix to e.g. <span style="color:#8B4513">'''''master'''''</span> that is also required in <span style="color:#8B4513">'''''maint'''''</span>, you will need to cherry-pick it (using [http://www.kernel.org/pub/software/scm/git/docs/git-cherry-pick.html git-cherry-pick(1)]) downwards. This will happen a few times and is nothing to worry about unless you do it very frequently.
 +
 +
=== Topic branches ===
 +
Any nontrivial feature will require several patches to implement, and may get extra bugfixes or improvements during its lifetime.
 +
 +
<span style="color:#D00000">Committing everything directly on the integration branches leads to many problems</span>: Bad commits cannot be undone, so they must be reverted one by one, which creates confusing histories and further error potential when you forget to revert part of a group of changes. Working in parallel mixes up the changes, creating further confusion.
 +
 +
Use of "topic branches" solves these problems. The name is pretty self explanatory, with a caveat that comes from the "merge upwards" rule above:
 +
 +
==== <span style="color:green">Rule: '''Topic branches'''</span> ====
 +
* Make a side branch for every topic (feature, bugfix, …). Fork it off at the '''oldest integration branch''' that you will eventually want to merge it into. For example for a bugfix in <span style="color:#8B4513">'''''maint'''''</span> fork off your topic branch from <span style="color:#8B4513">'''''maint'''''</span>. For a new feature fork it off from the <span style="color:#8B4513">'''''master'''''</span> (<span style="color:#D00000">never from the <span style="color:#8B4513">'''''next'''''</span> that is an experimental branch that can be rewind</span>).
 +
 +
Many things can then be done very naturally:
 +
 +
* To get the feature/bugfix into an integration branch, simply merge it. If the topic has evolved further in the meantime, merge again. (Note that you do not necessarily have to merge it to the oldest integration branch first. For example, you can first merge a bugfix to <span style="color:#8B4513">'''''next'''''</span>, give it some testing time, and merge to <span style="color:#8B4513">'''''maint'''''</span> when you know it is stable.)
 +
* If you find you need new features from the branch <span style="color:#8B4513">'''''other'''''</span> to continue working on your topic, merge <span style="color:#8B4513">'''''other'''''</span> to <span style="color:#8B4513">'''''topic'''''</span>. (However, do not do this "just habitually", see below.)
 +
* If you find you forked off the wrong branch and want to move it "back in time", use [http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html git-rebase(1)].
 +
 +
Note that the last point clashes with the other two: <span style="color:#D00000">a topic that has been merged elsewhere should not be rebased</span>. See the section on RECOVERING FROM UPSTREAM REBASE in [http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html git-rebase(1)].
 +
 +
We should point out that "habitually" (regularly for no real reason) <span style="color:#D00000">merging an integration branch into your topics — and by extension, merging anything upstream into anything downstream on a regular basis — is frowned upon because:</span>
 +
 +
==== <span style="color:green">Rule: '''Merge to downstream only at well-defined points'''</span> ====
 +
* <span style="color:#D00000">Do not merge to '''downstream''' except with a good reason</span>: upstream API changes affect your branch; your branch no longer merges to upstream cleanly; etc.
 +
 +
Otherwise, <span style="color:#D00000">the topic that was merged to suddenly contains more than a single (well-separated) change</span>. The many resulting small merges will greatly clutter up history. Anyone who later investigates the history of a file will have to find out whether that merge affected the topic in development. An upstream might even inadvertently be merged into a "more stable" branch. And so on.
 +
 +
So for resuming, when you have fork off a new topic branch (from <span style="color:#8B4513">'''''master'''''</span> for example) '''do not merge''' your topic branch from the branch that was used to fork off ("habitually" users merge theirs topic branches from the branch that was used to fork off to be in sync with the integration branch: DON'T DO THAT IT'S REALLY A BAD IDEA !). The only merge that was good for a topic branch is to merge into an integration branch.
 +
 +
=== Throw-away integration ===
 +
If you followed the last paragraph, you will now have many small topic branches, and occasionally wonder how they interact. Perhaps the result of merging them does not even work? But on the other hand, we want to avoid merging them anywhere "stable" because such merges cannot easily be undone.
 +
 +
The solution, of course, is to make a merge that we can undo: merge into a throw-away branch.
 +
 +
==== <span style="color:green">Rule: Throw-away integration branches</span> ====
 +
To test the interaction of several topics, merge them into a throw-away branch. '''You must never base any work on such a branch!'''
 +
 +
If you make it (very) clear that this branch is going to be deleted right after the testing, you can even publish this branch, for example to give the testers a chance to work with it, or other developers a chance to see if their in-progress work will be compatible. Bering-uClibc has such an official throw-away integration branch called <span style="color:#8B4513">'''''pu'''''</span>.
 +
 +
=== Branch management to commit bugfix into maintenance branch ===
 +
==== <span style="color:green">Recipe: Merge the bugfix into maint</span> ====
 +
To commit the bugfix into <span style="color:#8B4513">'''''maint'''''</span> branch you only have to merge you bugfix topic branch into maint:
 +
* Sync your <span style="color:#8B4513">'''''maint'''''</span> branch with the remote repository and check that you don't have uncommited changes:
 +
git checkout maint
 +
git pull --ff-only
 +
If the last command failed because the merge can't be resolved as a fast-forward, you have uncommited changes (it's bad because you don't have to commit directly on the integration branches (see [[#Topic branches]]). So you need to commit them or stash them.
 +
 +
* Merge you bugfix topic branch into <span style="color:#8B4513">'''''maint'''''</span> and push to the public remote repository. For example:
 +
git merge my-bugfix
 +
# Check that all is correct (test a build for example), then push to the remote repository
 +
git push
 +
 +
==== <span style="color:green">Recipe: Update master branch from maint</span> ====
 +
Now as the <span style="color:#8B4513">'''''master'''''</span> is supposed to be '''a superset''' of <span style="color:#8B4513">'''''maint'''''</span> you need to checkout the <span style="color:#8B4513">'''''master'''''</span> branch and merge the <span style="color:#8B4513">'''''maint'''''</span> into it:
 +
git checkout master
 +
git pull --ff-only
 +
git merge --no-commit maint
 +
 +
Resolve the conflicts if some exists. If the bugfix commit in <span style="color:#8B4513">'''''maint'''''</span> branch should not be applied (completely or not) into the <span style="color:#8B4513">'''''master'''''</span> you can use commands like <tt>git add -p</tt> and <tt>git reset -p</tt> to choose parts that you want to stage/unstage into the <span style="color:#8B4513">'''''master'''''</span> branch. After that you can commit the merge:
 +
 +
* Commit the merge into <span style="color:#8B4513">'''''master'''''</span>:
 +
git commit
 +
If you have remove some parts during the merge, put a notice in the commit message to tell why the bugfix was not apply completely.
 +
 +
* Push the new commit to the remote repository:
 +
git push
 +
 +
* Clean up your worktree (removing parts that was not commited):
 +
git checkout :/
 +
 +
=== Branch management for a release ===
 +
Assuming you are using the merge approach discussed above, when you are releasing your project you will need to do some additional branch management work.
 +
 +
A feature release is created from the <span style="color:#8B4513">'''''master'''''</span> branch, since <span style="color:#8B4513">'''''master'''''</span> tracks the commits that should go into the next feature release.
 +
 +
The <span style="color:#8B4513">'''''master'''''</span> branch is supposed to be a superset of <span style="color:#8B4513">'''''maint'''''</span>. If this condition does not hold, then <span style="color:#8B4513">'''''maint'''''</span> contains some commits that are not included on <span style="color:#8B4513">'''''master'''''</span>. The fixes represented by those commits will therefore not be included in your feature release.
 +
 +
To verify that <span style="color:#8B4513">'''''master'''''</span> is indeed a superset of <span style="color:#8B4513">'''''maint'''''</span>, use git log:
 +
==== <span style="color:green">Recipe: Verify master is a superset of maint</span> ====
 +
git log master..maint
 +
 +
This command should not list any commits. Otherwise, check out <span style="color:#8B4513">'''''master'''''</span> and merge <span style="color:#8B4513">'''''maint'''''</span> into it.
 +
 +
Now you can proceed with the creation of the feature release.
 +
 +
==== <span style="color:green">Recipe: Creation of a feature release</span> ====
 +
* Sync your <span style="color:#8B4513">'''''master'''''</span> branch with the remote repository and check that you don't have uncommited changes:
 +
git checkout master
 +
git pull --ff-only
 +
If the last command failed because the merge can't be resolved as a fast-forward, you have uncommited changes (it's bad because you don't have to commit directly on the integration branches (see [[#Topic branches]]). So you need commit them or stash them.
 +
 +
* Create a new commit to tell that is a new release, then tag it. For example:
 +
# bump the version number then create a commit for the new release
 +
git commit -a -m "Bering-uClibc ''5.0-rc0''"
 +
git tag -a -m "Release of LEAF Bering-uClibc ''5.0-rc0''" ''v5.0-rc0''
 +
 +
* Push the new commit and tag to the public git server:
 +
git push
 +
git push origin ''v5.0-rc0''
 +
 +
This makes the tag available to others tracking your project. The push could also trigger a post-update hook to perform release-related items such as building release tarballs and preformatted documentation pages.
 +
 +
==== Maintenance branch management after a feature release ====
 +
After a feature release, you perhaps need to manage your maintenance branches.
 +
First, if you wish to continue to release maintenance fixes for the feature release made before the recent one, then you must create another branch to track commits for that previous release.
 +
 +
To do this, the current maintenance branch is copied to another branch named with the previous release version number (e.g. maint-X.(Y-1) where X.Y is the current release).
 +
==== <span style="color:green">Recipe: Copy maint</span> ====
 +
git branch maint-X.(Y-1) maint
 +
 +
The maint branch should now be fast-forwarded to the newly released code so that maintenance fixes can be tracked for the current release:
 +
==== <span style="color:green">Recipe: Update maint to new release</span> ====
 +
    git checkout maint
 +
    git merge --ff-only master
 +
 +
If the merge fails because it is not a fast-forward, then it is possible some fixes on <span style="color:#8B4513">'''''maint'''''</span> were missed in the feature release. This will not happen if the <span style="color:#8B4513">'''''master'''''</span> is a superset of <span style="color:#8B4513">'''''maint'''''</span> (see the previous section)
 +
 +
== Useful commands ==
 +
=== Show the commits that aren't in the upstream ===
 +
To show the commits that are in <span style="color:#8B4513">'''''next'''''</span> waiting to be validate to be merge into <span style="color:#8B4513">'''''master'''''</span> do:
 +
git cherry -v master
 +
or
 +
git log master..next --no-merges
 +
 +
== Further Reading ==
 +
* Original [http://www.kernel.org/pub/software/scm/git/docs/gitworkflows.html GitWorkflows]
 +
* [http://gitster.livejournal.com/42247.html Fun with merges and purposes of branches]
 +
* [http://dan.bravender.us/2011/10/20/Why_cherry-picking_should_not_be_part_of_a_normal_git_workflow.html Why cherry-picking should not be part of a normal git workflow]
 +
* [http://sethrobertson.github.com/GitBestPractices Git Best Practices]
 +
  
 
[[Category:Developer Guide]]
 
[[Category:Developer Guide]]

Latest revision as of 18:06, 6 January 2015

Description

This document attempts to write down and motivate some of the workflow elements used for managing the development of Bering-uClibc with git. This guide was an adapted version of the GitWorkflows document that is used for the git project.

Separate changes

As a general rule, you should try to split your changes into small logical steps, and commit each of them. They should be consistent, working independently of any later commits, pass the build, etc. This makes the review process much easier, and the history much more useful for later inspection and analysis, for example with git-blame(1) and git-bisect(1).

To achieve this, try to split your work into small steps from the very beginning. It is always easier to squash a few commits together than to split one big commit into several. Don’t be afraid of making too small or imperfect steps along the way. You can always go back later and edit the commits with git rebase --interactive before you publish them. You can use git stash save --keep-index to try a build independent of other uncommitted changes; see the EXAMPLES section of git-stash(1).

Managing Branches

There are two main tools that can be used to include changes from one branch on another: git-merge(1) and git-cherry-pick(1).

Merges have many advantages, so we try to solve as many problems as possible with merges alone. Cherry-picking is still occasionally useful; see "Merging upwards" below for an example.

Most importantly, merging works at the branch level, while cherry-picking works at the commit level. This means that a merge can carry over the changes from 1, 10, or 1000 commits with equal ease, which in turn means the workflow scales much better to a large number of contributors (and contributions). Merges are also easier to understand because a merge commit is a promise that all changes from all its parents are now included.

There is a tradeoff of course: merges require a more careful branch management. The following subsections discuss the important points.

Graduation

As a given feature goes from experimental to stable, it also "graduates" between the corresponding branches of the software. Bering-uClibc uses the following integration branches:

  • maint tracks the commits that should go into the next "maintenance release", i.e., update of the last released stable version;
  • master tracks the commits that should go into the next release;
  • next is intended as a testing branch for topics being tested for stability for master.

There is a fourth official branch that is used slightly differently:

Each of the four branches is usually a direct descendant of the one above it.

Conceptually, the feature enters at an unstable branch (usually next or pu), and "graduates" to master for the next release once it is considered stable enough.

Merging upwards

The "downwards graduation" (for example from next to master) discussed above cannot be done by actually merging downwards, however, since that would merge all changes on the unstable branch into the stable one. Hence the following:

Rule: Merge upwards

Always commit your fixes to the oldest supported branch that require them. Then (periodically) merge the integration branches upwards into each other.

This gives a very controlled flow of fixes. If you notice that you have applied a fix to e.g. master that is also required in maint, you will need to cherry-pick it (using git-cherry-pick(1)) downwards. This will happen a few times and is nothing to worry about unless you do it very frequently.

Topic branches

Any nontrivial feature will require several patches to implement, and may get extra bugfixes or improvements during its lifetime.

Committing everything directly on the integration branches leads to many problems: Bad commits cannot be undone, so they must be reverted one by one, which creates confusing histories and further error potential when you forget to revert part of a group of changes. Working in parallel mixes up the changes, creating further confusion.

Use of "topic branches" solves these problems. The name is pretty self explanatory, with a caveat that comes from the "merge upwards" rule above:

Rule: Topic branches

  • Make a side branch for every topic (feature, bugfix, …). Fork it off at the oldest integration branch that you will eventually want to merge it into. For example for a bugfix in maint fork off your topic branch from maint. For a new feature fork it off from the master (never from the next that is an experimental branch that can be rewind).

Many things can then be done very naturally:

  • To get the feature/bugfix into an integration branch, simply merge it. If the topic has evolved further in the meantime, merge again. (Note that you do not necessarily have to merge it to the oldest integration branch first. For example, you can first merge a bugfix to next, give it some testing time, and merge to maint when you know it is stable.)
  • If you find you need new features from the branch other to continue working on your topic, merge other to topic. (However, do not do this "just habitually", see below.)
  • If you find you forked off the wrong branch and want to move it "back in time", use git-rebase(1).

Note that the last point clashes with the other two: a topic that has been merged elsewhere should not be rebased. See the section on RECOVERING FROM UPSTREAM REBASE in git-rebase(1).

We should point out that "habitually" (regularly for no real reason) merging an integration branch into your topics — and by extension, merging anything upstream into anything downstream on a regular basis — is frowned upon because:

Rule: Merge to downstream only at well-defined points

  • Do not merge to downstream except with a good reason: upstream API changes affect your branch; your branch no longer merges to upstream cleanly; etc.

Otherwise, the topic that was merged to suddenly contains more than a single (well-separated) change. The many resulting small merges will greatly clutter up history. Anyone who later investigates the history of a file will have to find out whether that merge affected the topic in development. An upstream might even inadvertently be merged into a "more stable" branch. And so on.

So for resuming, when you have fork off a new topic branch (from master for example) do not merge your topic branch from the branch that was used to fork off ("habitually" users merge theirs topic branches from the branch that was used to fork off to be in sync with the integration branch: DON'T DO THAT IT'S REALLY A BAD IDEA !). The only merge that was good for a topic branch is to merge into an integration branch.

Throw-away integration

If you followed the last paragraph, you will now have many small topic branches, and occasionally wonder how they interact. Perhaps the result of merging them does not even work? But on the other hand, we want to avoid merging them anywhere "stable" because such merges cannot easily be undone.

The solution, of course, is to make a merge that we can undo: merge into a throw-away branch.

Rule: Throw-away integration branches

To test the interaction of several topics, merge them into a throw-away branch. You must never base any work on such a branch!

If you make it (very) clear that this branch is going to be deleted right after the testing, you can even publish this branch, for example to give the testers a chance to work with it, or other developers a chance to see if their in-progress work will be compatible. Bering-uClibc has such an official throw-away integration branch called pu.

Branch management to commit bugfix into maintenance branch

Recipe: Merge the bugfix into maint

To commit the bugfix into maint branch you only have to merge you bugfix topic branch into maint:

  • Sync your maint branch with the remote repository and check that you don't have uncommited changes:
git checkout maint
git pull --ff-only

If the last command failed because the merge can't be resolved as a fast-forward, you have uncommited changes (it's bad because you don't have to commit directly on the integration branches (see #Topic branches). So you need to commit them or stash them.

  • Merge you bugfix topic branch into maint and push to the public remote repository. For example:
git merge my-bugfix
# Check that all is correct (test a build for example), then push to the remote repository
git push

Recipe: Update master branch from maint

Now as the master is supposed to be a superset of maint you need to checkout the master branch and merge the maint into it:

git checkout master
git pull --ff-only
git merge --no-commit maint

Resolve the conflicts if some exists. If the bugfix commit in maint branch should not be applied (completely or not) into the master you can use commands like git add -p and git reset -p to choose parts that you want to stage/unstage into the master branch. After that you can commit the merge:

  • Commit the merge into master:
git commit

If you have remove some parts during the merge, put a notice in the commit message to tell why the bugfix was not apply completely.

  • Push the new commit to the remote repository:
git push
  • Clean up your worktree (removing parts that was not commited):
git checkout :/

Branch management for a release

Assuming you are using the merge approach discussed above, when you are releasing your project you will need to do some additional branch management work.

A feature release is created from the master branch, since master tracks the commits that should go into the next feature release.

The master branch is supposed to be a superset of maint. If this condition does not hold, then maint contains some commits that are not included on master. The fixes represented by those commits will therefore not be included in your feature release.

To verify that master is indeed a superset of maint, use git log:

Recipe: Verify master is a superset of maint

git log master..maint

This command should not list any commits. Otherwise, check out master and merge maint into it.

Now you can proceed with the creation of the feature release.

Recipe: Creation of a feature release

  • Sync your master branch with the remote repository and check that you don't have uncommited changes:
git checkout master
git pull --ff-only

If the last command failed because the merge can't be resolved as a fast-forward, you have uncommited changes (it's bad because you don't have to commit directly on the integration branches (see #Topic branches). So you need commit them or stash them.

  • Create a new commit to tell that is a new release, then tag it. For example:
# bump the version number then create a commit for the new release
git commit -a -m "Bering-uClibc 5.0-rc0"
git tag -a -m "Release of LEAF Bering-uClibc 5.0-rc0" v5.0-rc0
  • Push the new commit and tag to the public git server:
git push
git push origin v5.0-rc0

This makes the tag available to others tracking your project. The push could also trigger a post-update hook to perform release-related items such as building release tarballs and preformatted documentation pages.

Maintenance branch management after a feature release

After a feature release, you perhaps need to manage your maintenance branches. First, if you wish to continue to release maintenance fixes for the feature release made before the recent one, then you must create another branch to track commits for that previous release.

To do this, the current maintenance branch is copied to another branch named with the previous release version number (e.g. maint-X.(Y-1) where X.Y is the current release).

Recipe: Copy maint

git branch maint-X.(Y-1) maint

The maint branch should now be fast-forwarded to the newly released code so that maintenance fixes can be tracked for the current release:

Recipe: Update maint to new release

   git checkout maint
   git merge --ff-only master

If the merge fails because it is not a fast-forward, then it is possible some fixes on maint were missed in the feature release. This will not happen if the master is a superset of maint (see the previous section)

Useful commands

Show the commits that aren't in the upstream

To show the commits that are in next waiting to be validate to be merge into master do:

git cherry -v master

or

git log master..next --no-merges

Further Reading