2 .\" Title: gitworkflows
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
7 .\" Source: Git 2.47.0.rc1.33.g90fe3800b9
10 .TH "GITWORKFLOWS" "7" "2024-10-04" "Git 2\&.47\&.0\&.rc1\&.33\&.g9" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 gitworkflows \- An overview of recommended workflows with Git
39 This document attempts to write down and motivate some of the workflow elements used for \fBgit\&.git\fR itself\&. Many ideas apply in general, though the full workflow is rarely required for smaller projects with fewer people involved\&.
41 We formulate a set of \fIrules\fR 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\&.
42 .SH "SEPARATE CHANGES"
44 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 \fBgit-blame\fR(1) and \fBgit-bisect\fR(1)\&.
46 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\(cqt be afraid of making too small or imperfect steps along the way\&. You can always go back later and edit the commits with \fBgit rebase \-\-interactive\fR before you publish them\&. You can use \fBgit stash push \-\-keep\-index\fR to run the test suite independent of other uncommitted changes; see the EXAMPLES section of \fBgit-stash\fR(1)\&.
47 .SH "MANAGING BRANCHES"
49 There are two main tools that can be used to include changes from one branch on another: \fBgit-merge\fR(1) and \fBgit-cherry-pick\fR(1)\&.
51 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\&.
53 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\&.
55 There is a tradeoff of course: merges require a more careful branch management\&. The following subsections discuss the important points\&.
58 As a given feature goes from experimental to stable, it also "graduates" between the corresponding branches of the software\&. \fBgit\&.git\fR uses the following \fIintegration branches\fR:
69 tracks the commits that should go into the next "maintenance release", i\&.e\&., update of the last released stable version;
81 tracks the commits that should go into the next release;
93 is intended as a testing branch for topics being tested for stability for master\&.
96 There is a fourth official branch that is used slightly differently:
107 (patches seen by the maintainer) is an integration branch for things that are not quite ready for inclusion yet (see "Integration Branches" below)\&.
110 Each of the four branches is usually a direct descendant of the one above it\&.
112 Conceptually, the feature enters at an unstable branch (usually \fInext\fR or \fIseen\fR), and "graduates" to \fImaster\fR for the next release once it is considered stable enough\&.
113 .SS "Merging upwards"
115 The "downwards graduation" discussed above cannot be done by actually merging downwards, however, since that would merge \fIall\fR changes on the unstable branch into the stable one\&. Hence the following:
117 \fBExample\ \&1.\ \&Merge upwards\fR
119 Always commit your fixes to the oldest supported branch that requires them\&. Then (periodically) merge the integration branches upwards into each other\&.
121 This gives a very controlled flow of fixes\&. If you notice that you have applied a fix to e\&.g\&. \fImaster\fR that is also required in \fImaint\fR, you will need to cherry\-pick it (using \fBgit-cherry-pick\fR(1)) downwards\&. This will happen a few times and is nothing to worry about unless you do it very frequently\&.
124 Any nontrivial feature will require several patches to implement, and may get extra bugfixes or improvements during its lifetime\&.
126 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\&.
128 Use of "topic branches" solves these problems\&. The name is pretty self explanatory, with a caveat that comes from the "merge upwards" rule above:
130 \fBExample\ \&2.\ \&Topic branches\fR
132 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\&.
134 Many things can then be done very naturally:
144 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
145 \fInext\fR, give it some testing time, and merge to
147 when you know it is stable\&.)
158 If you find you need new features from the branch
160 to continue working on your topic, merge
163 \fItopic\fR\&. (However, do not do this "just habitually", see below\&.)
174 If you find you forked off the wrong branch and want to move it "back in time", use
175 \fBgit-rebase\fR(1)\&.
178 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 \fBgit-rebase\fR(1)\&.
180 We should point out that "habitually" (regularly for no real reason) merging an integration branch into your topics \(em and by extension, merging anything upstream into anything downstream on a regular basis \(em is frowned upon:
182 \fBExample\ \&3.\ \&Merge to downstream only at well\-defined points\fR
184 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\&.
186 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\&.
187 .SS "Throw\-away integration"
189 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\&.
191 The solution, of course, is to make a merge that we can undo: merge into a throw\-away branch\&.
193 \fBExample\ \&4.\ \&Throw\-away integration branches\fR
195 To test the interaction of several topics, merge them into a throw\-away branch\&. You must never base any work on such a branch!
197 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\&. \fBgit\&.git\fR has such an official throw\-away integration branch called \fIseen\fR\&.
198 .SS "Branch management for a release"
200 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\&.
202 A feature release is created from the \fImaster\fR branch, since \fImaster\fR tracks the commits that should go into the next feature release\&.
204 The \fImaster\fR branch is supposed to be a superset of \fImaint\fR\&. If this condition does not hold, then \fImaint\fR contains some commits that are not included on \fImaster\fR\&. The fixes represented by those commits will therefore not be included in your feature release\&.
206 To verify that \fImaster\fR is indeed a superset of \fImaint\fR, use git log:
208 \fBExample\ \&5.\ \&Verify \fImaster\fR is a superset of \fImaint\fR\fR
210 \fBgit log master\&.\&.maint\fR
212 This command should not list any commits\&. Otherwise, check out \fImaster\fR and merge \fImaint\fR into it\&.
214 Now you can proceed with the creation of the feature release\&. Apply a tag to the tip of \fImaster\fR indicating the release version:
216 \fBExample\ \&6.\ \&Release tagging\fR
218 \fBgit tag \-s \-m "Git X\&.Y\&.Z" vX\&.Y\&.Z master\fR
220 You need to push the new tag to a public Git server (see "DISTRIBUTED WORKFLOWS" below)\&. 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\&.
222 Similarly, for a maintenance release, \fImaint\fR is tracking the commits to be released\&. Therefore, in the steps above simply tag and push \fImaint\fR rather than \fImaster\fR\&.
223 .SS "Maintenance branch management after a feature release"
225 After a feature release, you need to manage your maintenance branches\&.
227 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\&.
229 To do this, the current maintenance branch is copied to another branch named with the previous release version number (e\&.g\&. maint\-X\&.Y\&.(Z\-1) where X\&.Y\&.Z is the current release)\&.
231 \fBExample\ \&7.\ \&Copy maint\fR
233 \fBgit branch maint\-X\&.Y\&.(Z\-1) maint\fR
235 The \fImaint\fR branch should now be fast\-forwarded to the newly released code so that maintenance fixes can be tracked for the current release:
237 \fBExample\ \&8.\ \&Update maint to new release\fR
247 \fBgit checkout maint\fR
258 \fBgit merge \-\-ff\-only master\fR
261 If the merge fails because it is not a fast\-forward, then it is possible some fixes on \fImaint\fR were missed in the feature release\&. This will not happen if the content of the branches was verified as described in the previous section\&.
262 .SS "Branch management for next and seen after a feature release"
264 After a feature release, the integration branch \fInext\fR may optionally be rewound and rebuilt from the tip of \fImaster\fR using the surviving topics on \fInext\fR:
266 \fBExample\ \&9.\ \&Rewind and rebuild next\fR
276 \fBgit switch \-C next master\fR
287 \fBgit merge ai/topic_in_next1\fR
298 \fBgit merge ai/topic_in_next2\fR
312 The advantage of doing this is that the history of \fInext\fR will be clean\&. For example, some topics merged into \fInext\fR may have initially looked promising, but were later found to be undesirable or premature\&. In such a case, the topic is reverted out of \fInext\fR but the fact remains in the history that it was once merged and reverted\&. By recreating \fInext\fR, you give another incarnation of such topics a clean slate to retry, and a feature release is a good point in history to do so\&.
314 If you do this, then you should make a public announcement indicating that \fInext\fR was rewound and rebuilt\&.
316 The same rewind and rebuild process may be followed for \fIseen\fR\&. A public announcement is not necessary since \fIseen\fR is a throw\-away branch, as described above\&.
317 .SH "DISTRIBUTED WORKFLOWS"
319 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\&.
321 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 \fBgit\&.git\fR, only subsystem maintainers use the merge workflow, while everyone else sends patches\&.
323 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\(cqs documentation for more information\&.
326 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\&.
328 There are three main tools that can be used for this:
339 copies your branches to a remote repository, usually to one that can be read by all involved parties;
351 that copies remote branches to your repository; and
363 that does fetch and merge in one go\&.
366 Note the last point\&. Do \fInot\fR use \fIgit pull\fR unless you actually want to merge the remote branch\&.
368 Getting changes out is easy:
370 \fBExample\ \&10.\ \&Push/pull: Publishing branches/topics\fR
372 \fBgit push <remote> <branch>\fR and tell everyone where they can fetch from\&.
374 You will still have to tell people by other means, such as mail\&. (Git provides the \fBgit-request-pull\fR(1) to send preformatted pull requests to upstream maintainers to simplify this task\&.)
376 If you just want to get the newest copies of the integration branches, staying up to date is easy too:
378 \fBExample\ \&11.\ \&Push/pull: Staying up to date\fR
380 Use \fBgit fetch <remote>\fR or \fBgit remote update\fR to stay up to date\&.
382 Then simply fork your topic branches from the stable remotes as explained earlier\&.
384 If you are a maintainer and would like to merge other people\(cqs topic branches to the integration branches, they will typically send a request to do so by mail\&. Such a request looks like
397 In that case, \fIgit pull\fR can do the fetch and merge in one go, as follows\&.
399 \fBExample\ \&12.\ \&Push/pull: Merging remote topics\fR
401 \fBgit pull <URL> <branch>\fR
403 Occasionally, the maintainer may get merge conflicts when they try to pull changes from downstream\&. In this case, they 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 \fIshould\fR merge from upstream\&.
406 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 \fBgit-format-patch\fR(1) to generate the corresponding emails (highly recommended over manually formatting them because it makes the maintainer\(cqs life easier)\&.
408 \fBExample\ \&13.\ \&format\-patch/am: Publishing branches/topics\fR
418 \fBgit format\-patch \-M upstream\&.\&.topic\fR
419 to turn them into preformatted patch files
430 \fBgit send\-email \-\-to=<recipient> <patches>\fR
433 See the \fBgit-format-patch\fR(1) and \fBgit-send-email\fR(1) manpages for further usage notes\&.
435 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):
437 \fBExample\ \&14.\ \&format\-patch/am: Keeping topics up to date\fR
439 \fBgit pull \-\-rebase <URL> <branch>\fR
441 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\&.
443 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 \fIgit am\fR to import the commits:
445 \fBExample\ \&15.\ \&format\-patch/am: Importing patches\fR
449 One feature worth pointing out is the three\-way merge, which can help if you get conflicts: \fBgit am \-3\fR will use index information contained in patches to figure out the merge base\&. See \fBgit-am\fR(1) for other options\&.
452 \fBgittutorial\fR(7), \fBgit-push\fR(1), \fBgit-pull\fR(1), \fBgit-merge\fR(1), \fBgit-rebase\fR(1), \fBgit-format-patch\fR(1), \fBgit-send-email\fR(1), \fBgit-am\fR(1)
455 Part of the \fBgit\fR(1) suite