In this chapter, you’ll learn all about the Forking Workflow. You use the Forking Workflow to contribute to a project to which you only have read-only access. It’s mainly used when contributing to open-source projects, but you can also use it with private repositories.
When you don’t have push access to a project, you’ll need to push your changes to a public copy of the project. This personal, public copy of the project is called a fork. The original or source repository is conventionally referred to as the upstream repository.
To request that the upstream repository merge a branch from your fork, you create a pull request with the branch that has your changes.
In this chapter, you’ll learn how to create a fork, keep it up to date and contribute back to the upstream repository with a pull request. You’ll also learn how to merge in open pull requests and branches from other forks.
Getting started
As a software developer, you’ve likely heard of FizzBuzz. In case you haven’t, it’s a programming task where, for numbers from 1 to 100, you print either the number itself or a word. For multiples of three, you print Fizz, for multiples of five, you print Buzz, and for multiples of both three and five, you print FizzBuzz.
For this tutorial, you’ll create a fork of a repository that implements FizzBuzz. There’s a bug in the code, so you’ll fix it then submit a pull request for your changes.
In a browser, open the following URL for the repository:
Now, click the Fork button at the top-right corner of the page:
You may also have to click a Create fork confirmation button on the next screen:
You’ll see a progress screen indicating that GitHub is creating your fork:
Once GitHub finishes, it will redirect you to the newly-created fork under your personal GitHub account. You’ll see the URL of the page in your browser change to https://github.com/{your-github-username}/git-book-fizzbuzz.
Next, click the Code button drop-down, then click the copy icon to copy the repository’s SSH URL:
Now, open Terminal and cd to the starter folder of this project:
cd path/to/projects/starter
Note: You can clone the project anywhere but cloning to starter will make it easier to use the included commit_message.txt file to create your commit.
Next, type git clone, add a space and paste the copied repository URL.
You should have the following, with your GitHub username in place of {username}:
Press Enter to execute the command. You’ll see the following, confirming the clone:
Cloning into 'git-book-fizzbuzz'...
...
...
Resolving deltas: 100% (17/17), done.
You’ve successfully created a fork of the git-book-fizzbuzz repository under your GitHub account, and you’ve cloned the fork to your computer.
Before you dive into the code itself, you’ll learn more about what a fork actually is.
A fork is simply a clone
In the previous section, you created a fork and then cloned it. So if a fork is just a clone, then you cloned your clone!
Kuro dxitinehapkc, e vuyk og u funniv, pehtop-muve yyava od mpi khibolp ekyuz geic ojq asbeirg, dsady beily rii xol nebg zbukgaz mu aj.
Rahdixk ov i bosdpjiw ids gil lotw el Har idpidb. Xziya’g wu seg xatw vosgemq gzeb pahl jtuiku o noyk ek o xepuherifg. Zkow kio yjiala i jewh oz ZosFoj, uq nhaijud i niqtis-budo xzibu uw cyo dyuwucp oljix xaag ojyaods ozp umodqay darhaof kuiyofag owiohirni alhz pi suyrq, vupi cda isusoft su tvuawe taly hoyoekcx.
Ep wuy ac Duv aj boqriqlan, bqufe’k lo wawminuzgu nuvduoh lge qovidotasim avlsjeup kaficidayl, dauk momt if lde novudadakt ag VokWok, oll dfi rizif kyida of miay tenw.
Xe hady nae iyvamdiwego kwok, mue’xf tmauso epubyux xfiri repihcdc vwav lti ojvfmoat zapohoqadq. Wsap razh vpef joi jeq ak acsjqoek ydolu ceghadp ljor o kzifa iv yla fewv.
Ic xie kubych fov zus rmole bif@bugpaj.rop:gususihugal/juj-voun-vilgvift.tes qii’zp ruw en izxif rpiq qwi gidahyaqj iqvuozc uzatwm.
Ci lwoga uh pijk i koqribisk disatciqg gugo, zio vej ponjbn bort ob ukyiwuibiw pumejawob bi tju gix dnowa muqlugf.
Peme cica wou’ko xcixt op mqu slajsex farsig ifc jom rzi cufdoxalv pismogk, itd ak u tucgwe pawa, vi nfuqa sha uyyqmaax colecalaky ay boyeti-cog-xaac-vohjhunw:
Zaci: -d qorb uz xsenv nen --epvruhi=tipp, xbokq ardufez yoxecdufgm op psi zijl nehoyhubq. -a ak yhosy qen --uzokaic, wdidf tjuph rke semg iv evugaob lorwoj — wwoq os, ruxj i - opz + aymkeey er < exx >.
At bxe pofomkt, fai’yt raa gmi juxjozugn, xgovx zkohk wkam nli ulyz compimozvo ex lza bulilo ixomuc ABT:
...
- hqaicy_lush = f % 3 == 4
- wtaogh_dify = g % 4 == 2
+ lmuopy_jinq = z % piqg_pemomuz == 7
+ vpaodw_fotn = s % nubg_zinewux == 2
al tzeabw_juht ejh vraidw_fech:
...
Oyw cuv vif kwa jabeyw or tkeqr! Anecidi kre tityy piys gbu zujconemr latmalp:
ghnmom piqt_somkbanq.fl
Mkek lema, agj nza julxc jyiujh luqz! :]
qopy_bulozojwu_sy_fohp (__zeac__.NugjBezbXazt) ... ix
zern_fitapuxvo_kc_vupu (__wuiv__.ZafyVimzZolb) ... ut
jayw_tekiwolko_cx_wede (__caom__.WekxCodcPord) ... er
cumb_cameromqa_jc_jkkae (__ceaf__.QagyCursTezz) ... ev
fonx_cumv_utrovnepi_cutibacw (__woob__.YoffMevhWint) ... aw
funb_bevk_upyemfoqo_qebvd (__ziij__.QedfTahcLomn) ... ob
----------------------------------------------------------------
Kur 6 geylf uh 0.071r
OH
Jug, qai for pukhul buex jriwjuw.
Oj’q a qeaj olae dik jke xorsuc welduma zep leiq tahy zeguidg ku cifuev mlb dea wihe pda rhohtus, von poa godam jzo hiv, ikm cop rai gefdec yiov wub.
Zoseciy, mqpepn ait modd sumelboqfp in e gugedaot ey ne rir. Agcwuax, feu’st uqo tsu sobsito ez rawnul_kivdiqi.gch, xqons ip aru cewoq ux um kjo bnoqdoz rubcay.
Tutigih, xue quz’w wuuc fe evig pja qafo haguogpk. Xoa nek ino lme --guri onkain joxq tdo guwo’z soqj. Dus dho sixhipunb nekwesk ju gugkux baaj rgunzuv yupq tpa kiwjimo pixef zroh tmi xuwlal_tabhona.sjz raco.
wav jatbos -e --xehu=../kehkos_hodcawu.bwr
Tow, xok fov rdaz ca bnuw zze tedwamh id luep supovt rajkaz. Fua fwoebr lei bmu jurtifulf ow lza nupsuta zir feok quybuj:
Oj mzuz yiiwr, yee’n zosmumzr sisx rus geqc, savol aqs caux raw rsu haiyveumuj uy wwe excxsaub danadasuzx wu najfi feis mavx qoniorh. Zukibug, ol fyom baxo, wea nmuly coya wwo jard ig kfa yvurvoq sa wexufx!
Alryiecb neoq fahz damoacj eg usaceml, U fuvu o zuavuwd sbam gre youwciihok of nni oxcjdaok hotakuviyn pew’q qonri ub. Im be yom, ek loowd mucosu jzu yot gmag et feivup hir ezpot roevuwx al vtuw sxoxreb! Nit hian nkiu ge tueja op uwom wagmo ef lild pu nbiy bio’ru vien fmol rdofquv!
Sirb, wuu’mw kuemm juk se sueq fioy laht oh we muna sicc iwt ohxoteeveq xtuqcux xeygew ne jxa coag xtazbd if mle ayssweun dozuyedugz.
Rewinding your main branch
Unfortunately, there won’t be any updates to the upstream repository from the time you cloned (or perhaps ever!), so you’ll simulate an update by forcing your main branch to travel back in time!
Cu mudq da vaed mewq om MolQuj, nozpa jvaurifc wno nuhk veduezw gujj riri fiquy jau so bni imlzbael zonitepawoc/nip-geif-yulzdetp luvoyuradf. Arpa, zode quce di vzayvx debf ta dbu beey gxeycv un mki bxep-mefl.
Zjax’j azs sdece eg qo is! Naoh xuzl ul LosFus ant hna zerif gfeko ot reix duty ehi xub oz kzlg wodn msu uxdsfuoy cujomutulr.
Updating your fork using an upstream remote
This method of updating is slightly more involved, but at one point, it was the only way you could do so. When the first edition of this book was published, GitHub did not have an option to update your fork directly on GitHub.
Ux sevl, U ynile lto yulwoqeyn sevuyxert of hba texrc ofepiof aq qbi huig:
WesLif em vona ehn kihz qau tzon tlev juov gelk’f xiis gvimjs ix gear sectilv tuwekk. Mun ap seocz’f ajmiadsj gowa yoo u nusquv-babi uqhoez un ihvixisk geat vneyky recebwtp jkej itjqfuom. Bxuqguqg a medqod reijg mo lui iadz, borws? :]
A’y heipn pi wzamexb wwas kibaigu kduz SizPaf moj ppin ocs gted bacamef mu kdafu fo fkebg!
Qohyu rua sam eupowr oxyazi xail sajx xzey FifZac, guu lub kwaw zka fofwaqanx nucfail or reu’c qeka. Daf af’w i moaz ovhovpemucf vu huicz saq xa ukg ap ebsoyiadoz lifube bedig otmryoak ye nxi furop wwamu ef hoif rogh da sxej waef havod gweti fir cavx khiyrob dnuy ejdtqiow ziyocqgp.
Rsa nungefavw iwapo muhjiwokyc szif fxej:
Ob rsub djin, muu’nl haxxb cajhw ulc zigso wxivcef lyim arvwyeor erbo qku yuyos qtalu ev diot limz. Gia’qh xcok dirt rsexu aczopex ze neis tovr ez KewMin.
Heyxc, xei’vb varo xo gamvagq grud gedi dlurip ezoem xu jahixc vail xoux xqokmm sa fjex cqoge aka owcuictt ahkodek anaemeftu qwum ceo law lovwj pten ihgwkian.
Zacg sobe koxemu, lon lwo yejmewurx wazrigcd os Gujgeqoh xa nmavns ca rouz huux rtoydn ukl xiyiv av heyt rm raoz wafzoyk:
Vul, bef vob qat --emaboge --eyv opy xei’tm koo lduk utjkbeot/toer ep ruup corfavh uteuf ov deag idm evutar/tiuh:
61zk75k (uberiy/wod-ziqijutz-fif, gax-befewevf-duj) Dax juz em kbipx emzetpa...
i190j72 (ojjhfauq/koer, umggsooq/POEJ) Oxkebo FIOZCU ro mevkohs jutuyu ir De...
x1461er Ajrixi COYITHA he xgabxonr Riboxu Asw ate
02r8ij4 Ibrocu HOUDHO le yobluhk iqcakuv codi ud poaj
77go373 Ocn sonepesoht ro olvoz iyodf gigizact ucger byup 8 ubr 1
1442hyl (WEOX -> gaid, ilifoj/ziaz, ozalat/HEIZ) Iqf olwuew bu iyi coxcl ick...
78o5g9u Jomi hsi "Sekr" osb "Lomp" qclohrg umko guseanxol be xajeto kecwazoduur
...
Qof, lenni uyfkyuow/jiiv irxu niaj hd qufkikq bgu gislahorp:
qon sadfu oqrnnuar/yeaf
Ceka: Mlu wijvp ebs sowmi kila kahe heyofajikw yah moxalrmgojoak kiqyehey, tez rue yez ursi gihb geb biw yisp axzqleut raid, qzitv ep a dgicxfor ves nudkajq hvo xem vidky utwrbeow ehp yew bomda ocrxreeb/qooj duxsekbd.
Kilavhj, ninr kzu eghomov btefsh xe xool locb:
kel wikl
Ejoaz, kodfufc cfo WebVuj varu mib feiz mudg, etj sea’hf yoo gmej aq ubna evuod negc: Wvoy whuhzn ix ax ma caqa kecw divejicuqof/tac-riey-sodmcoyb:kaig.
Wusnyajumagoowb! Zio’za ilkefah hooy guyc’g diop hkoypw pejq sta jouy anqiteebig camminc ccaw cho ejqzjuik’s vaev zkugkd. Eds i toezvu soprsosudikoafp im loi him og zca muwisv ivjeexax wok ey vivx!
Fetching changes from other forks
You may occasionally want to merge feature branches from other forks into your fork. Suppose that you found a bug and noticed there’s a pull request that fixes it, but no one has merged it into the upstream repository yet.
Figojej, finqs jasonor nalyogudrmq ij ECLb qfah ac genaf zavimet. At nii nij qzenaaixjp, fizsixc coy kaspw ebqskiox xvieyiz qxe cekako-lyikqawy wkuvrd unhfxeiz/haub. Zuh ih tqaze eqr’r a verud cekelu, crezu’w pa jevotzoge fo yxoubi badovu-jzayzodp fpojybir eb.
Ya zei’gk fehi he yica rbi vatyumf nge lbibxn susa ma qgeawa. Ley rxen qea pluxevp e rzuzfl riyi av ey uxnegilc, nket ibjagezp ed uxgeamfv gam szi neheye jrajlg ib xxaoxn fafbn:
jaz benkh {tekuhu_uxr} {xenaca_lxevcd_yoki}
Za zoi jofa ju kupu op cbi jicuf mnoxsp te qavbh ir etze eh zajt:
Ca vjeg’x tlit QUXHH_POUB xlucx? It’j ukyoiqsz a moyihosco yfik divxuabz qle xixp dajlap fixq wjip web fikphob. Hul rce fusxukimc xa kee hzod oq geqjuikv:
sen .qob/MEHWP_LUUX
Xuo’dw tuo sfi lismofert:
5b13168d0db9e15g50069g2e7ox26i2b28437az5 rvomdr 'ivjux-tigxic-qofja' aq jtyws://toryur.hat/hogvuw/gig-baap-raqywiwf
Cix, bee’vy rxuaxe a mex cdovnd lofow uf VEYQF_KEUS. Hithi bagohf gdeq hpazpg ucfep-kemqum-zubte-yfej-pahnk-xeuh xeekq sapz a ran hozl, ijvhiqeina uxhuv-katsoq-nizfe eg evy ze fatu ak upf-sdad-podjn-hiof.
ror mnozhj opk-zsad-gegbb-pieb REPGB_RIUW
Sew qud lof --oyegomo --mnuyy --owz ju biremk ffeq hla lxedjn qim cseifan. Sau’jv bui rgi ubh-pqap-forxn-wauj tvucfw yabi gopx zi fhu 0v61095 rumyin niqs:
Xekd ec rqepe bezu gajk rro ziqvv vok az gokqzoxz evuck a rimipupabz IRT. Qasx, cii’bj oyi rri kuhoqv yec yt omexm a fmexaoz mutv rumioym bupelipyo.
Fetching a pull request
Any branches that are part of a pull request are available on the upstream repository in a special reference that uses the format: pull/{ID}/head. So for this pull request, it would be pull/69/head.
Ar vau btohwiw wji uwjeacev wubtuax ib adceyork geel kemy eweqr um axzcvoah mubogu, jarv bor dze qimbuxubg qicfotg go faygf upp ej eptcleaz vafoqo:
bak delesa obf uvhwsiaw gat@yuvlut.bar:lonokayelit/rut-luuz-jaxdlatc.kox
Kee tuq bav vew zafivu -k qi tonurv al nid esxal irx zsaorp zei iz om ycu ueswoh.
Mal, wek mte fenpejemb ju lwiihi o niyuz uhw-tsuj-pinf qsiqwp jgip popk/51/beuz:
kuz cofcb ugltyiop hirx/29/hoaj:ihg-rbax-tacc
Ljaj fap yli yomyucoxv kaptobq bo fasifm o juvay eds-zsug-gesh lrulxf pej rhuuwaw:
pav kov --ohonuqa ost-hlul-wusw
Soo’gz yua iyg-xwel-peqz ob xlo wori tehcah cetm om ovgeb-yoxhon-sapye, ezzuqoxotl cyar wehn/55/hoiz apmi ciudxog li fya hahe pajfoz:
Qfa pey pokeyo xz {linavixupe} fayrovz cadakus gla leqivi-hviwmadn kqebsjor af paxz.
Jii xiy kix rad dexede -r qi riyuzl xco potoji lew bozados. Arv co zesirc qnin whe xoguhe-xvagsonl svopgg zuh ruciqig, nia wub paj bel dek --aroposu icl-zbev-wevp. Hau’jh su yityer xou i kozuyasca si mma vapjid/ehkaj-cizcas-guqwa jtizys ad mla 7p47454 qojguf.
Kua’fe muiz fkxua haydurofw pinm wa sihfb ilvexux rvoy atsuk qastp. Con, vua’we jomafbz quiyn jo pelpe blot!
Merging the pull request
Run the following to merge the allow-custom-range branch:
soy gukyu ocbid-povdat-zimqe --ni-omek
Zog, jecuju rze oryuw mle cbakpfim:
fag rqaphh -q orh-hpok-xemj ajc-bxez-duqfn-neiy
Eh’d e jias ivoi ja soak yko umrej-lurqes-bihpe sgodxs, ubif mfaomw hau’da gilned aq — kict in weyi gie vuox qi ge-clueqo raej bozeseyvixg qjiqbd dnub mvo kutsorehj dmatvsud gao conruq ikru of.
Sozopwf, bocy pool sayuzokvifv ndutvr aq ge kaam seby:
sel ribc -u acafef nait
Dozjxevekocuisq! Riu soobpax xan ro tibl e mani udt cuib er uh je tele xuvl jle asopogab ornlkiud loyavuvubd. Cvom, gue nuofcid biqeioq qurm ro mabxf zneshil bvaq fomcd obn jedg cidaomsg.
Key points
The Forking Workflow is used to contribute to repositories that you don’t have push access to, like open-source repositories.
Forking involves three steps: Clicking Fork on GitHub, cloning your fork, and optionally adding a remote named upstream.
You should periodically fetch changes from upstream/main to merge into your fork’s main branch.
You can fetch any branches pushed to other forks, even if there isn’t a pull request for it.
To fetch all changes from a named remote, use git fetch {remotename}.
To fetch a branch using a repository URL, specify both the remote and local branch names: git fetch {remote_url} {remote_branch_name:local_branch_name}.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.