Pathogenic bacteria such as Escherichia coli assemble surface structures termed pili, or fimbriae, to mediate binding to host-cell receptors1. Type 1 pili are assembled via the conserved chaperone-usher pathway2-5. The outer-membrane usher FimD recruits pilus subunits bound by the chaperone FimC via the periplasmic N-terminal domain of the usher. Subunit translocation through the β-barrel channel of the usher occurs at the two C-terminal domains (which we label CTD1 and CTD2) of this protein. How the chaperone-subunit complex bound to the N-terminal domain is handed over to the C-terminal domains, as well as the timing of subunit polymerization into the growing pilus, have previously been unclear. Here we use cryo-electron microscopy to capture a pilus assembly intermediate (FimD-FimC-FimF-FimG-FimH) in a conformation in which FimD is in the process of handing over the chaperone-bound end of the growing pilus to the C-terminal domains. In this structure, FimF has already polymerized with FimG, and the N-terminal domain of FimD swings over to bind CTD2; the N-terminal domain maintains contact with FimC-FimF, while at the same time permitting access to the C-terminal domains. FimD has an intrinsically disordered N-terminal tail that precedes the N-terminal domain. This N-terminal tail folds into a helical motif upon recruiting the FimC-subunit complex, but reorganizes into a loop to bind CTD2 during handover. Because both the N-terminal and C-terminal domains of FimD are bound to the end of the growing pilus, the structure further suggests a mechanism for stabilizing the assembly intermediate to prevent the pilus fibre diffusing away during the incorporation of thousands of subunits.