interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / gitworkflows.adoc

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