1GITWORKFLOWS(7)                   Git Manual                   GITWORKFLOWS(7)
2
3
4

NAME

6       gitworkflows - An overview of recommended workflows with git
7

SYNOPSIS

9       git *
10

DESCRIPTION

12       This document attempts to write down and motivate some of the workflow
13       elements used for git.git itself. Many ideas apply in general, though
14       the full workflow is rarely required for smaller projects with fewer
15       people involved.
16
17       We formulate a set of rules for quick reference, while the prose tries
18       to motivate each of them. Do not always take them literally; you should
19       value good reasons for your actions higher than manpages such as this
20       one.
21

SEPARATE CHANGES

23       As a general rule, you should try to split your changes into small
24       logical steps, and commit each of them. They should be consistent,
25       working independently of any later commits, pass the test suite, etc.
26       This makes the review process much easier, and the history much more
27       useful for later inspection and analysis, for example with git-blame(1)
28       and git-bisect(1).
29
30       To achieve this, try to split your work into small steps from the very
31       beginning. It is always easier to squash a few commits together than to
32       split one big commit into several. Don’t be afraid of making too small
33       or imperfect steps along the way. You can always go back later and edit
34       the commits with git rebase --interactive before you publish them. You
35       can use git stash save --keep-index to run the test suite independent
36       of other uncommitted changes; see the EXAMPLES section of git-stash(1).
37

MANAGING BRANCHES

39       There are two main tools that can be used to include changes from one
40       branch on another: git-merge(1) and git-cherry-pick(1).
41
42       Merges have many advantages, so we try to solve as many problems as
43       possible with merges alone. Cherry-picking is still occasionally
44       useful; see "Merging upwards" below for an example.
45
46       Most importantly, merging works at the branch level, while
47       cherry-picking works at the commit level. This means that a merge can
48       carry over the changes from 1, 10, or 1000 commits with equal ease,
49       which in turn means the workflow scales much better to a large number
50       of contributors (and contributions). Merges are also easier to
51       understand because a merge commit is a "promise" that all changes from
52       all its parents are now included.
53
54       There is a tradeoff of course: merges require a more careful branch
55       management. The following subsections discuss the important points.
56
57   Graduation
58       As a given feature goes from experimental to stable, it also
59       "graduates" between the corresponding branches of the software. git.git
60       uses the following integration branches:
61
62       ·    maint tracks the commits that should go into the next "maintenance
63           release", i.e., update of the last released stable version;
64
65       ·    master tracks the commits that should go into the next release;
66
67       ·    next is intended as a testing branch for topics being tested for
68           stability for master.
69
70       There is a fourth official branch that is used slightly differently:
71
72       ·    pu (proposed updates) is an integration branch for things that are
73           not quite ready for inclusion yet (see "Integration Branches"
74           below).
75
76       Each of the four branches is usually a direct descendant of the one
77       above it.
78
79       Conceptually, the feature enters at an unstable branch (usually next or
80       pu), and "graduates" to master for the next release once it is
81       considered stable enough.
82
83   Merging upwards
84       The "downwards graduation" discussed above cannot be done by actually
85       merging downwards, however, since that would merge all changes on the
86       unstable branch into the stable one. Hence the following:
87
88       Example 1. Merge upwards
89
90       Always commit your fixes to the oldest supported branch that require
91       them. Then (periodically) merge the integration branches upwards into
92       each other.
93
94       This gives a very controlled flow of fixes. If you notice that you have
95       applied a fix to e.g. master that is also required in maint, you will
96       need to cherry-pick it (using git-cherry-pick(1)) downwards. This will
97       happen a few times and is nothing to worry about unless you do it very
98       frequently.
99
100   Topic branches
101       Any nontrivial feature will require several patches to implement, and
102       may get extra bugfixes or improvements during its lifetime.
103
104       Committing everything directly on the integration branches leads to
105       many problems: Bad commits cannot be undone, so they must be reverted
106       one by one, which creates confusing histories and further error
107       potential when you forget to revert part of a group of changes. Working
108       in parallel mixes up the changes, creating further confusion.
109
110       Use of "topic branches" solves these problems. The name is pretty self
111       explanatory, with a caveat that comes from the "merge upwards" rule
112       above:
113
114       Example 2. Topic branches
115
116       Make a side branch for every topic (feature, bugfix, ...). Fork it off
117       at the oldest integration branch that you will eventually want to merge
118       it into.
119
120       Many things can then be done very naturally:
121
122       ·   To get the feature/bugfix into an integration branch, simply merge
123           it. If the topic has evolved further in the meantime, merge again.
124           (Note that you do not necessarily have to merge it to the oldest
125           integration branch first. For example, you can first merge a bugfix
126           to next, give it some testing time, and merge to maint when you
127           know it is stable.)
128
129       ·   If you find you need new features from the branch other to continue
130           working on your topic, merge other to topic. (However, do not do
131           this "just habitually", see below.)
132
133       ·   If you find you forked off the wrong branch and want to move it
134           "back in time", use git-rebase(1).
135
136       Note that the last point clashes with the other two: a topic that has
137       been merged elsewhere should not be rebased. See the section on
138       RECOVERING FROM UPSTREAM REBASE in git-rebase(1).
139
140       We should point out that "habitually" (regularly for no real reason)
141       merging an integration branch into your topics — and by extension,
142       merging anything upstream into anything downstream on a regular basis —
143       is frowned upon:
144
145       Example 3. Merge to downstream only at well-defined points
146
147       Do not merge to downstream except with a good reason: upstream API
148       changes affect your branch; your branch no longer merges to upstream
149       cleanly; etc.
150
151       Otherwise, the topic that was merged to suddenly contains more than a
152       single (well-separated) change. The many resulting small merges will
153       greatly clutter up history. Anyone who later investigates the history
154       of a file will have to find out whether that merge affected the topic
155       in development. An upstream might even inadvertently be merged into a
156       "more stable" branch. And so on.
157
158   Throw-away integration
159       If you followed the last paragraph, you will now have many small topic
160       branches, and occasionally wonder how they interact. Perhaps the result
161       of merging them does not even work? But on the other hand, we want to
162       avoid merging them anywhere "stable" because such merges cannot easily
163       be undone.
164
165       The solution, of course, is to make a merge that we can undo: merge
166       into a throw-away branch.
167
168       Example 4. Throw-away integration branches
169
170       To test the interaction of several topics, merge them into a throw-away
171       branch. You must never base any work on such a branch!
172
173       If you make it (very) clear that this branch is going to be deleted
174       right after the testing, you can even publish this branch, for example
175       to give the testers a chance to work with it, or other developers a
176       chance to see if their in-progress work will be compatible. git.git has
177       such an official throw-away integration branch called pu.
178
179   Branch management for a release
180       Assuming you are using the merge approach discussed above, when you are
181       releasing your project you will need to do some additional branch
182       management work.
183
184       A feature release is created from the master branch, since master
185       tracks the commits that should go into the next feature release.
186
187       The master branch is supposed to be a superset of maint. If this
188       condition does not hold, then maint contains some commits that are not
189       included on master. The fixes represented by those commits will
190       therefore not be included in your feature release.
191
192       To verify that master is indeed a superset of maint, use git log:
193
194       Example 5. Verify master is a superset of maint
195
196       git log master..maint
197
198       This command should not list any commits. Otherwise, check out master
199       and merge maint into it.
200
201       Now you can proceed with the creation of the feature release. Apply a
202       tag to the tip of master indicating the release version:
203
204       Example 6. Release tagging
205
206       git tag -s -m "GIT X.Y.Z" vX.Y.Z master
207
208       You need to push the new tag to a public git server (see "DISTRIBUTED
209       WORKFLOWS" below). This makes the tag available to others tracking your
210       project. The push could also trigger a post-update hook to perform
211       release-related items such as building release tarballs and
212       preformatted documentation pages.
213
214       Similarly, for a maintenance release, maint is tracking the commits to
215       be released. Therefore, in the steps above simply tag and push maint
216       rather than master.
217
218   Maintenance branch management after a feature release
219       After a feature release, you need to manage your maintenance branches.
220
221       First, if you wish to continue to release maintenance fixes for the
222       feature release made before the recent one, then you must create
223       another branch to track commits for that previous release.
224
225       To do this, the current maintenance branch is copied to another branch
226       named with the previous release version number (e.g. maint-X.Y.(Z-1)
227       where X.Y.Z is the current release).
228
229       Example 7. Copy maint
230
231       git branch maint-X.Y.(Z-1) maint
232
233       The maint branch should now be fast-forwarded to the newly released
234       code so that maintenance fixes can be tracked for the current release:
235
236       Example 8. Update maint to new release
237
238       ·    git checkout maint
239
240       ·    git merge --ff-only master
241
242       If the merge fails because it is not a fast-forward, then it is
243       possible some fixes on maint were missed in the feature release. This
244       will not happen if the content of the branches was verified as
245       described in the previous section.
246
247   Branch management for next and pu after a feature release
248       After a feature release, the integration branch next may optionally be
249       rewound and rebuilt from the tip of master using the surviving topics
250       on next:
251
252       Example 9. Rewind and rebuild next
253
254       ·    git checkout next
255
256       ·    git reset --hard master
257
258       ·    git merge ai/topic_in_next1
259
260       ·    git merge ai/topic_in_next2
261
262       ·   ...
263
264       The advantage of doing this is that the history of next will be clean.
265       For example, some topics merged into next may have initially looked
266       promising, but were later found to be undesirable or premature. In such
267       a case, the topic is reverted out of next but the fact remains in the
268       history that it was once merged and reverted. By recreating next, you
269       give another incarnation of such topics a clean slate to retry, and a
270       feature release is a good point in history to do so.
271
272       If you do this, then you should make a public announcement indicating
273       that next was rewound and rebuilt.
274
275       The same rewind and rebuild process may be followed for pu. A public
276       announcement is not necessary since pu is a throw-away branch, as
277       described above.
278

DISTRIBUTED WORKFLOWS

280       After the last section, you should know how to manage topics. In
281       general, you will not be the only person working on the project, so you
282       will have to share your work.
283
284       Roughly speaking, there are two important workflows: merge and patch.
285       The important difference is that the merge workflow can propagate full
286       history, including merges, while patches cannot. Both workflows can be
287       used in parallel: in git.git, only subsystem maintainers use the merge
288       workflow, while everyone else sends patches.
289
290       Note that the maintainer(s) may impose restrictions, such as
291       "Signed-off-by" requirements, that all commits/patches submitted for
292       inclusion must adhere to. Consult your project’s documentation for more
293       information.
294
295   Merge workflow
296       The merge workflow works by copying branches between upstream and
297       downstream. Upstream can merge contributions into the official history;
298       downstream base their work on the official history.
299
300       There are three main tools that can be used for this:
301
302       ·    git-push(1) copies your branches to a remote repository, usually
303           to one that can be read by all involved parties;
304
305       ·    git-fetch(1) that copies remote branches to your repository; and
306
307       ·    git-pull(1) that does fetch and merge in one go.
308
309       Note the last point. Do not use git pull unless you actually want to
310       merge the remote branch.
311
312       Getting changes out is easy:
313
314       Example 10. Push/pull: Publishing branches/topics
315
316       git push <remote> <branch> and tell everyone where they can fetch from.
317
318       You will still have to tell people by other means, such as mail. (Git
319       provides the git-request-pull(1) to send preformatted pull requests to
320       upstream maintainers to simplify this task.)
321
322       If you just want to get the newest copies of the integration branches,
323       staying up to date is easy too:
324
325       Example 11. Push/pull: Staying up to date
326
327       Use git fetch <remote> or git remote update to stay up to date.
328
329       Then simply fork your topic branches from the stable remotes as
330       explained earlier.
331
332       If you are a maintainer and would like to merge other people’s topic
333       branches to the integration branches, they will typically send a
334       request to do so by mail. Such a request looks like
335
336           Please pull from
337               <url> <branch>
338
339
340       In that case, git pull can do the fetch and merge in one go, as
341       follows.
342
343       Example 12. Push/pull: Merging remote topics
344
345       git pull <url> <branch>
346
347       Occasionally, the maintainer may get merge conflicts when he tries to
348       pull changes from downstream. In this case, he can ask downstream to do
349       the merge and resolve the conflicts themselves (perhaps they will know
350       better how to resolve them). It is one of the rare cases where
351       downstream should merge from upstream.
352
353   Patch workflow
354       If you are a contributor that sends changes upstream in the form of
355       emails, you should use topic branches as usual (see above). Then use
356       git-format-patch(1) to generate the corresponding emails (highly
357       recommended over manually formatting them because it makes the
358       maintainer’s life easier).
359
360       Example 13. format-patch/am: Publishing branches/topics
361
362       ·    git format-patch -M upstream..topic to turn them into preformatted
363           patch files
364
365       ·    git send-email --to=<recipient> <patches>
366
367       See the git-format-patch(1) and git-send-email(1) manpages for further
368       usage notes.
369
370       If the maintainer tells you that your patch no longer applies to the
371       current upstream, you will have to rebase your topic (you cannot use a
372       merge because you cannot format-patch merges):
373
374       Example 14. format-patch/am: Keeping topics up to date
375
376       git pull --rebase <url> <branch>
377
378       You can then fix the conflicts during the rebase. Presumably you have
379       not published your topic other than by mail, so rebasing it is not a
380       problem.
381
382       If you receive such a patch series (as maintainer, or perhaps as a
383       reader of the mailing list it was sent to), save the mails to files,
384       create a new topic branch and use git am to import the commits:
385
386       Example 15. format-patch/am: Importing patches
387
388       git am < patch
389
390       One feature worth pointing out is the three-way merge, which can help
391       if you get conflicts: git am -3 will use index information contained in
392       patches to figure out the merge base. See git-am(1) for other options.
393

SEE ALSO

395       gittutorial(7), git-push(1), git-pull(1), git-merge(1), git-rebase(1),
396       git-format-patch(1), git-send-email(1), git-am(1)
397

GIT

399       Part of the git(1) suite.
400
401
402
403Git 1.7.1                         08/16/2017                   GITWORKFLOWS(7)
Impressum