<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="gitworkflows(7)">
<articleinfo>
    <title>gitworkflows(7)</title>
        <indexterm>
                <primary>gitworkflows(7)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>gitworkflows - An overview of recommended workflows with git</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara>git *</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>This document attempts to write down and motivate some of the workflow
elements used for <literal>git.git</literal> itself.  Many ideas apply in general,
though the full workflow is rarely required for smaller projects with
fewer people involved.</simpara>
<simpara>We formulate a set of <emphasis>rules</emphasis> for quick reference, while the prose
tries to motivate each of them.  Do not always take them literally;
you should value good reasons for your actions higher than manpages
such as this one.</simpara>
</simplesect>
<simplesect id="_separate_changes">
<title>SEPARATE CHANGES</title>
<simpara>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 test suite, etc.
This makes the review process much easier, and the history much more
useful for later inspection and analysis, for example with
<xref linkend="git-blame(1)"/> and <xref linkend="git-bisect(1)"/>.</simpara>
<simpara>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&#8217;t be afraid of making too
small or imperfect steps along the way. You can always go back later
and edit the commits with <literal>git rebase --interactive</literal> before you
publish them.  You can use <literal>git stash save --keep-index</literal> to run the
test suite independent of other uncommitted changes; see the EXAMPLES
section of <xref linkend="git-stash(1)"/>.</simpara>
</simplesect>
<simplesect id="_managing_branches">
<title>MANAGING BRANCHES</title>
<simpara>There are two main tools that can be used to include changes from one
branch on another: <xref linkend="git-merge(1)"/> and
<xref linkend="git-cherry-pick(1)"/>.</simpara>
<simpara>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.</simpara>
<simpara>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.</simpara>
<simpara>There is a tradeoff of course: merges require a more careful branch
management.  The following subsections discuss the important points.</simpara>
<simplesect id="_graduation">
<title>Graduation</title>
<simpara>As a given feature goes from experimental to stable, it also
"graduates" between the corresponding branches of the software.
<literal>git.git</literal> uses the following <emphasis>integration branches</emphasis>:</simpara>
<itemizedlist>
<listitem>
<simpara>
<emphasis>maint</emphasis> tracks the commits that should go into the next "maintenance
  release", i.e., update of the last released stable version;
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>master</emphasis> tracks the commits that should go into the next release;
</simpara>
</listitem>
<listitem>
<simpara>
<emphasis>next</emphasis> is intended as a testing branch for topics being tested for
  stability for master.
</simpara>
</listitem>
</itemizedlist>
<simpara>There is a fourth official branch that is used slightly differently:</simpara>
<itemizedlist>
<listitem>
<simpara>
<emphasis>pu</emphasis> (proposed updates) is an integration branch for things that are
  not quite ready for inclusion yet (see "Integration Branches"
  below).
</simpara>
</listitem>
</itemizedlist>
<simpara>Each of the four branches is usually a direct descendant of the one
above it.</simpara>
<simpara>Conceptually, the feature enters at an unstable branch (usually <emphasis>next</emphasis>
or <emphasis>pu</emphasis>), and "graduates" to <emphasis>master</emphasis> for the next release once it is
considered stable enough.</simpara>
</simplesect>
<simplesect id="_merging_upwards">
<title>Merging upwards</title>
<simpara>The "downwards graduation" discussed above cannot be done by actually
merging downwards, however, since that would merge <emphasis>all</emphasis> changes on
the unstable branch into the stable one.  Hence the following:</simpara>
<example>
<title>Merge upwards</title>
<simpara>Always commit your fixes to the oldest supported branch that require
them.  Then (periodically) merge the integration branches upwards into each
other.</simpara>
</example>
<simpara>This gives a very controlled flow of fixes.  If you notice that you
have applied a fix to e.g. <emphasis>master</emphasis> that is also required in <emphasis>maint</emphasis>,
you will need to cherry-pick it (using <xref linkend="git-cherry-pick(1)"/>)
downwards.  This will happen a few times and is nothing to worry about
unless you do it very frequently.</simpara>
</simplesect>
<simplesect id="_topic_branches">
<title>Topic branches</title>
<simpara>Any nontrivial feature will require several patches to implement, and
may get extra bugfixes or improvements during its lifetime.</simpara>
<simpara>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.</simpara>
<simpara>Use of "topic branches" solves these problems.  The name is pretty
self explanatory, with a caveat that comes from the "merge upwards"
rule above:</simpara>
<example>
<title>Topic branches</title>
<simpara>Make a side branch for every topic (feature, bugfix, &#8230;). Fork it off
at the oldest integration branch that you will eventually want to merge it
into.</simpara>
</example>
<simpara>Many things can then be done very naturally:</simpara>
<itemizedlist>
<listitem>
<simpara>
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 <emphasis>next</emphasis>, give it some testing time, and merge to <emphasis>maint</emphasis> when you
  know it is stable.)
</simpara>
</listitem>
<listitem>
<simpara>
If you find you need new features from the branch <emphasis>other</emphasis> to continue
  working on your topic, merge <emphasis>other</emphasis> to <emphasis>topic</emphasis>.  (However, do not
  do this "just habitually", see below.)
</simpara>
</listitem>
<listitem>
<simpara>
If you find you forked off the wrong branch and want to move it
  "back in time", use <xref linkend="git-rebase(1)"/>.
</simpara>
</listitem>
</itemizedlist>
<simpara>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 <xref linkend="git-rebase(1)"/>.</simpara>
<simpara>We should point out that "habitually" (regularly for no real reason)
merging an integration branch into your topics&#8201;&#8212;&#8201;and by extension,
merging anything upstream into anything downstream on a regular basis&#8201;&#8212;&#8201;is frowned upon:</simpara>
<example>
<title>Merge to downstream only at well-defined points</title>
<simpara>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.</simpara>
</example>
<simpara>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.</simpara>
</simplesect>
<simplesect id="_throw_away_integration">
<title>Throw-away integration</title>
<simpara>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.</simpara>
<simpara>The solution, of course, is to make a merge that we can undo: merge
into a throw-away branch.</simpara>
<example>
<title>Throw-away integration branches</title>
<simpara>To test the interaction of several topics, merge them into a
throw-away branch.  You must never base any work on such a branch!</simpara>
</example>
<simpara>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.  <literal>git.git</literal>
has such an official throw-away integration branch called <emphasis>pu</emphasis>.</simpara>
</simplesect>
</simplesect>
<simplesect id="_distributed_workflows">
<title>DISTRIBUTED WORKFLOWS</title>
<simpara>After the last section, you should know how to manage topics.  In
general, you will not be the only person working on the project, so
you will have to share your work.</simpara>
<simpara>Roughly speaking, there are two important workflows: merge and patch.
The important difference is that the merge workflow can propagate full
history, including merges, while patches cannot.  Both workflows can
be used in parallel: in <literal>git.git</literal>, only subsystem maintainers use
the merge workflow, while everyone else sends patches.</simpara>
<simpara>Note that the maintainer(s) may impose restrictions, such as
"Signed-off-by" requirements, that all commits/patches submitted for
inclusion must adhere to.  Consult your project&#8217;s documentation for
more information.</simpara>
<simplesect id="_merge_workflow">
<title>Merge workflow</title>
<simpara>The merge workflow works by copying branches between upstream and
downstream.  Upstream can merge contributions into the official
history; downstream base their work on the official history.</simpara>
<simpara>There are three main tools that can be used for this:</simpara>
<itemizedlist>
<listitem>
<simpara>
<xref linkend="git-push(1)"/> copies your branches to a remote repository,
  usually to one that can be read by all involved parties;
</simpara>
</listitem>
<listitem>
<simpara>
<xref linkend="git-fetch(1)"/> that copies remote branches to your repository;
  and
</simpara>
</listitem>
<listitem>
<simpara>
<xref linkend="git-pull(1)"/> that does fetch and merge in one go.
</simpara>
</listitem>
</itemizedlist>
<simpara>Note the last point.  Do <emphasis>not</emphasis> use <emphasis>git-pull</emphasis> unless you actually want
to merge the remote branch.</simpara>
<simpara>Getting changes out is easy:</simpara>
<example>
<title>Push/pull: Publishing branches/topics</title>
<simpara><literal>git push &lt;remote&gt; &lt;branch&gt;</literal> and tell everyone where they can fetch
from.</simpara>
</example>
<simpara>You will still have to tell people by other means, such as mail.  (Git
provides the <xref linkend="git-request-pull(1)"/> to send preformatted pull
requests to upstream maintainers to simplify this task.)</simpara>
<simpara>If you just want to get the newest copies of the integration branches,
staying up to date is easy too:</simpara>
<example>
<title>Push/pull: Staying up to date</title>
<simpara>Use <literal>git fetch &lt;remote&gt;</literal> or <literal>git remote update</literal> to stay up to date.</simpara>
</example>
<simpara>Then simply fork your topic branches from the stable remotes as
explained earlier.</simpara>
<simpara>If you are a maintainer and would like to merge other people&#8217;s topic
branches to the integration branches, they will typically send a
request to do so by mail.  Such a request looks like</simpara>
<literallayout>Please pull from
    &lt;url&gt; &lt;branch&gt;</literallayout>
<simpara>In that case, <emphasis>git-pull</emphasis> can do the fetch and merge in one go, as
follows.</simpara>
<example>
<title>Push/pull: Merging remote topics</title>
<simpara><literal>git pull &lt;url&gt; &lt;branch&gt;</literal></simpara>
</example>
<simpara>Occasionally, the maintainer may get merge conflicts when he tries to
pull changes from downstream.  In this case, he can ask downstream to
do the merge and resolve the conflicts themselves (perhaps they will
know better how to resolve them).  It is one of the rare cases where
downstream <emphasis>should</emphasis> merge from upstream.</simpara>
</simplesect>
<simplesect id="_patch_workflow">
<title>Patch workflow</title>
<simpara>If you are a contributor that sends changes upstream in the form of
emails, you should use topic branches as usual (see above).  Then use
<xref linkend="git-format-patch(1)"/> to generate the corresponding emails
(highly recommended over manually formatting them because it makes the
maintainer&#8217;s life easier).</simpara>
<example>
<title>format-patch/am: Publishing branches/topics</title>
<itemizedlist>
<listitem>
<simpara>
<literal>git format-patch -M upstream..topic</literal> to turn them into preformatted
  patch files
</simpara>
</listitem>
<listitem>
<simpara>
<literal>git send-email --to=&lt;recipient&gt; &lt;patches&gt;</literal>
</simpara>
</listitem>
</itemizedlist>
</example>
<simpara>See the <xref linkend="git-format-patch(1)"/> and <xref linkend="git-send-email(1)"/>
manpages for further usage notes.</simpara>
<simpara>If the maintainer tells you that your patch no longer applies to the
current upstream, you will have to rebase your topic (you cannot use a
merge because you cannot format-patch merges):</simpara>
<example>
<title>format-patch/am: Keeping topics up to date</title>
<simpara><literal>git pull --rebase &lt;url&gt; &lt;branch&gt;</literal></simpara>
</example>
<simpara>You can then fix the conflicts during the rebase.  Presumably you have
not published your topic other than by mail, so rebasing it is not a
problem.</simpara>
<simpara>If you receive such a patch series (as maintainer, or perhaps as a
reader of the mailing list it was sent to), save the mails to files,
create a new topic branch and use <emphasis>git-am</emphasis> to import the commits:</simpara>
<example>
<title>format-patch/am: Importing patches</title>
<simpara><literal>git am &lt; patch</literal></simpara>
</example>
<simpara>One feature worth pointing out is the three-way merge, which can help
if you get conflicts: <literal>git am -3</literal> will use index information contained
in patches to figure out the merge base.  See <xref linkend="git-am(1)"/> for
other options.</simpara>
</simplesect>
</simplesect>
<simplesect id="_see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="gittutorial(7)"/>,
<xref linkend="git-push(1)"/>,
<xref linkend="git-pull(1)"/>,
<xref linkend="git-merge(1)"/>,
<xref linkend="git-rebase(1)"/>,
<xref linkend="git-format-patch(1)"/>,
<xref linkend="git-send-email(1)"/>,
<xref linkend="git-am(1)"/></simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite.</simpara>
</simplesect>
</article>
