1GITWORKFLOWS(7) Git Manual GITWORKFLOWS(7)
2
3
4
6 gitworkflows - An overview of recommended workflows with Git
7
9 git *
10
11
13 This document attempts to write down and motivate some of the workflow
14 elements used for git.git itself. Many ideas apply in general, though
15 the full workflow is rarely required for smaller projects with fewer
16 people involved.
17
18 We formulate a set of rules for quick reference, while the prose tries
19 to motivate each of them. Do not always take them literally; you should
20 value good reasons for your actions higher than manpages such as this
21 one.
22
24 As a general rule, you should try to split your changes into small
25 logical steps, and commit each of them. They should be consistent,
26 working independently of any later commits, pass the test suite, etc.
27 This makes the review process much easier, and the history much more
28 useful for later inspection and analysis, for example with git-blame(1)
29 and git-bisect(1).
30
31 To achieve this, try to split your work into small steps from the very
32 beginning. It is always easier to squash a few commits together than to
33 split one big commit into several. Don’t be afraid of making too small
34 or imperfect steps along the way. You can always go back later and edit
35 the commits with git rebase --interactive before you publish them. You
36 can use git stash push --keep-index to run the test suite independent
37 of other uncommitted changes; see the EXAMPLES section of git-stash(1).
38
40 There are two main tools that can be used to include changes from one
41 branch on another: git-merge(1) and git-cherry-pick(1).
42
43 Merges have many advantages, so we try to solve as many problems as
44 possible with merges alone. Cherry-picking is still occasionally
45 useful; see "Merging upwards" below for an example.
46
47 Most importantly, merging works at the branch level, while
48 cherry-picking works at the commit level. This means that a merge can
49 carry over the changes from 1, 10, or 1000 commits with equal ease,
50 which in turn means the workflow scales much better to a large number
51 of contributors (and contributions). Merges are also easier to
52 understand because a merge commit is a "promise" that all changes from
53 all its parents are now included.
54
55 There is a tradeoff of course: merges require a more careful branch
56 management. The following subsections discuss the important points.
57
58 Graduation
59 As a given feature goes from experimental to stable, it also
60 "graduates" between the corresponding branches of the software. git.git
61 uses the following integration branches:
62
63 · maint tracks the commits that should go into the next "maintenance
64 release", i.e., update of the last released stable version;
65
66 · master tracks the commits that should go into the next release;
67
68 · next is intended as a testing branch for topics being tested for
69 stability for master.
70
71 There is a fourth official branch that is used slightly differently:
72
73 · pu (proposed updates) is an integration branch for things that are
74 not quite ready for inclusion yet (see "Integration Branches"
75 below).
76
77 Each of the four branches is usually a direct descendant of the one
78 above it.
79
80 Conceptually, the feature enters at an unstable branch (usually next or
81 pu), and "graduates" to master for the next release once it is
82 considered stable enough.
83
84 Merging upwards
85 The "downwards graduation" discussed above cannot be done by actually
86 merging downwards, however, since that would merge all changes on the
87 unstable branch into the stable one. Hence the following:
88
89 Example 1. Merge upwards
90
91 Always commit your fixes to the oldest supported branch that requires
92 them. Then (periodically) merge the integration branches upwards into
93 each other.
94
95 This gives a very controlled flow of fixes. If you notice that you have
96 applied a fix to e.g. master that is also required in maint, you will
97 need to cherry-pick it (using git-cherry-pick(1)) downwards. This will
98 happen a few times and is nothing to worry about unless you do it very
99 frequently.
100
101 Topic branches
102 Any nontrivial feature will require several patches to implement, and
103 may get extra bugfixes or improvements during its lifetime.
104
105 Committing everything directly on the integration branches leads to
106 many problems: Bad commits cannot be undone, so they must be reverted
107 one by one, which creates confusing histories and further error
108 potential when you forget to revert part of a group of changes. Working
109 in parallel mixes up the changes, creating further confusion.
110
111 Use of "topic branches" solves these problems. The name is pretty self
112 explanatory, with a caveat that comes from the "merge upwards" rule
113 above:
114
115 Example 2. Topic branches
116
117 Make a side branch for every topic (feature, bugfix, ...). Fork it off
118 at the oldest integration branch that you will eventually want to merge
119 it into.
120
121 Many things can then be done very naturally:
122
123 · To get the feature/bugfix into an integration branch, simply merge
124 it. If the topic has evolved further in the meantime, merge again.
125 (Note that you do not necessarily have to merge it to the oldest
126 integration branch first. For example, you can first merge a bugfix
127 to next, give it some testing time, and merge to maint when you
128 know it is stable.)
129
130 · If you find you need new features from the branch other to continue
131 working on your topic, merge other to topic. (However, do not do
132 this "just habitually", see below.)
133
134 · If you find you forked off the wrong branch and want to move it
135 "back in time", use git-rebase(1).
136
137 Note that the last point clashes with the other two: a topic that has
138 been merged elsewhere should not be rebased. See the section on
139 RECOVERING FROM UPSTREAM REBASE in git-rebase(1).
140
141 We should point out that "habitually" (regularly for no real reason)
142 merging an integration branch into your topics — and by extension,
143 merging anything upstream into anything downstream on a regular basis —
144 is frowned upon:
145
146 Example 3. Merge to downstream only at well-defined points
147
148 Do not merge to downstream except with a good reason: upstream API
149 changes affect your branch; your branch no longer merges to upstream
150 cleanly; etc.
151
152 Otherwise, the topic that was merged to suddenly contains more than a
153 single (well-separated) change. The many resulting small merges will
154 greatly clutter up history. Anyone who later investigates the history
155 of a file will have to find out whether that merge affected the topic
156 in development. An upstream might even inadvertently be merged into a
157 "more stable" branch. And so on.
158
159 Throw-away integration
160 If you followed the last paragraph, you will now have many small topic
161 branches, and occasionally wonder how they interact. Perhaps the result
162 of merging them does not even work? But on the other hand, we want to
163 avoid merging them anywhere "stable" because such merges cannot easily
164 be undone.
165
166 The solution, of course, is to make a merge that we can undo: merge
167 into a throw-away branch.
168
169 Example 4. Throw-away integration branches
170
171 To test the interaction of several topics, merge them into a throw-away
172 branch. You must never base any work on such a branch!
173
174 If you make it (very) clear that this branch is going to be deleted
175 right after the testing, you can even publish this branch, for example
176 to give the testers a chance to work with it, or other developers a
177 chance to see if their in-progress work will be compatible. git.git has
178 such an official throw-away integration branch called pu.
179
180 Branch management for a release
181 Assuming you are using the merge approach discussed above, when you are
182 releasing your project you will need to do some additional branch
183 management work.
184
185 A feature release is created from the master branch, since master
186 tracks the commits that should go into the next feature release.
187
188 The master branch is supposed to be a superset of maint. If this
189 condition does not hold, then maint contains some commits that are not
190 included on master. The fixes represented by those commits will
191 therefore not be included in your feature release.
192
193 To verify that master is indeed a superset of maint, use git log:
194
195 Example 5. Verify master is a superset of maint
196
197 git log master..maint
198
199 This command should not list any commits. Otherwise, check out master
200 and merge maint into it.
201
202 Now you can proceed with the creation of the feature release. Apply a
203 tag to the tip of master indicating the release version:
204
205 Example 6. Release tagging
206
207 git tag -s -m "Git X.Y.Z" vX.Y.Z master
208
209 You need to push the new tag to a public Git server (see "DISTRIBUTED
210 WORKFLOWS" below). This makes the tag available to others tracking your
211 project. The push could also trigger a post-update hook to perform
212 release-related items such as building release tarballs and
213 preformatted documentation pages.
214
215 Similarly, for a maintenance release, maint is tracking the commits to
216 be released. Therefore, in the steps above simply tag and push maint
217 rather than master.
218
219 Maintenance branch management after a feature release
220 After a feature release, you need to manage your maintenance branches.
221
222 First, if you wish to continue to release maintenance fixes for the
223 feature release made before the recent one, then you must create
224 another branch to track commits for that previous release.
225
226 To do this, the current maintenance branch is copied to another branch
227 named with the previous release version number (e.g. maint-X.Y.(Z-1)
228 where X.Y.Z is the current release).
229
230 Example 7. Copy maint
231
232 git branch maint-X.Y.(Z-1) maint
233
234 The maint branch should now be fast-forwarded to the newly released
235 code so that maintenance fixes can be tracked for the current release:
236
237 Example 8. Update maint to new release
238
239 · git checkout maint
240
241 · git merge --ff-only master
242
243 If the merge fails because it is not a fast-forward, then it is
244 possible some fixes on maint were missed in the feature release. This
245 will not happen if the content of the branches was verified as
246 described in the previous section.
247
248 Branch management for next and pu after a feature release
249 After a feature release, the integration branch next may optionally be
250 rewound and rebuilt from the tip of master using the surviving topics
251 on next:
252
253 Example 9. Rewind and rebuild next
254
255 · git checkout next
256
257 · git reset --hard master
258
259 · git merge ai/topic_in_next1
260
261 · git merge ai/topic_in_next2
262
263 · ...
264
265 The advantage of doing this is that the history of next will be clean.
266 For example, some topics merged into next may have initially looked
267 promising, but were later found to be undesirable or premature. In such
268 a case, the topic is reverted out of next but the fact remains in the
269 history that it was once merged and reverted. By recreating next, you
270 give another incarnation of such topics a clean slate to retry, and a
271 feature release is a good point in history to do so.
272
273 If you do this, then you should make a public announcement indicating
274 that next was rewound and rebuilt.
275
276 The same rewind and rebuild process may be followed for pu. A public
277 announcement is not necessary since pu is a throw-away branch, as
278 described above.
279
281 After the last section, you should know how to manage topics. In
282 general, you will not be the only person working on the project, so you
283 will have to share your work.
284
285 Roughly speaking, there are two important workflows: merge and patch.
286 The important difference is that the merge workflow can propagate full
287 history, including merges, while patches cannot. Both workflows can be
288 used in parallel: in git.git, only subsystem maintainers use the merge
289 workflow, while everyone else sends patches.
290
291 Note that the maintainer(s) may impose restrictions, such as
292 "Signed-off-by" requirements, that all commits/patches submitted for
293 inclusion must adhere to. Consult your project’s documentation for more
294 information.
295
296 Merge workflow
297 The merge workflow works by copying branches between upstream and
298 downstream. Upstream can merge contributions into the official history;
299 downstream base their work on the official history.
300
301 There are three main tools that can be used for this:
302
303 · git-push(1) copies your branches to a remote repository, usually to
304 one that can be read by all involved parties;
305
306 · git-fetch(1) that copies remote branches to your repository; and
307
308 · git-pull(1) that does fetch and merge in one go.
309
310 Note the last point. Do not use git pull unless you actually want to
311 merge the remote branch.
312
313 Getting changes out is easy:
314
315 Example 10. Push/pull: Publishing branches/topics
316
317 git push <remote> <branch> and tell everyone where they can fetch from.
318
319 You will still have to tell people by other means, such as mail. (Git
320 provides the git-request-pull(1) to send preformatted pull requests to
321 upstream maintainers to simplify this task.)
322
323 If you just want to get the newest copies of the integration branches,
324 staying up to date is easy too:
325
326 Example 11. Push/pull: Staying up to date
327
328 Use git fetch <remote> or git remote update to stay up to date.
329
330 Then simply fork your topic branches from the stable remotes as
331 explained earlier.
332
333 If you are a maintainer and would like to merge other people’s topic
334 branches to the integration branches, they will typically send a
335 request to do so by mail. Such a request looks like
336
337 Please pull from
338 <url> <branch>
339
340
341 In that case, git pull can do the fetch and merge in one go, as
342 follows.
343
344 Example 12. Push/pull: Merging remote topics
345
346 git pull <url> <branch>
347
348 Occasionally, the maintainer may get merge conflicts when they try to
349 pull changes from downstream. In this case, they can ask downstream to
350 do the merge and resolve the conflicts themselves (perhaps they will
351 know better how to resolve them). It is one of the rare cases where
352 downstream should merge from upstream.
353
354 Patch workflow
355 If you are a contributor that sends changes upstream in the form of
356 emails, you should use topic branches as usual (see above). Then use
357 git-format-patch(1) to generate the corresponding emails (highly
358 recommended over manually formatting them because it makes the
359 maintainer’s life easier).
360
361 Example 13. format-patch/am: Publishing branches/topics
362
363 · git format-patch -M upstream..topic to turn them into preformatted
364 patch files
365
366 · git send-email --to=<recipient> <patches>
367
368 See the git-format-patch(1) and git-send-email(1) manpages for further
369 usage notes.
370
371 If the maintainer tells you that your patch no longer applies to the
372 current upstream, you will have to rebase your topic (you cannot use a
373 merge because you cannot format-patch merges):
374
375 Example 14. format-patch/am: Keeping topics up to date
376
377 git pull --rebase <url> <branch>
378
379 You can then fix the conflicts during the rebase. Presumably you have
380 not published your topic other than by mail, so rebasing it is not a
381 problem.
382
383 If you receive such a patch series (as maintainer, or perhaps as a
384 reader of the mailing list it was sent to), save the mails to files,
385 create a new topic branch and use git am to import the commits:
386
387 Example 15. format-patch/am: Importing patches
388
389 git am < patch
390
391 One feature worth pointing out is the three-way merge, which can help
392 if you get conflicts: git am -3 will use index information contained in
393 patches to figure out the merge base. See git-am(1) for other options.
394
396 gittutorial(7), git-push(1), git-pull(1), git-merge(1), git-rebase(1),
397 git-format-patch(1), git-send-email(1), git-am(1)
398
400 Part of the git(1) suite
401
402
403
404Git 2.21.0 02/24/2019 GITWORKFLOWS(7)