In this chapter, you’ll learn all about the Forking Workflow. You use the Forking Workflow when you want 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 then 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:
Note: The git-book-fizzbuzz repository uses main instead of master as the default branch.
Now, click the Fork button at the top-right corner of the page:
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 change to https://github.com/{your-github-username}/git-book-fizzbuzz.
Next, click on the Code button drop-down, then click the clipboard icon to copy the repository’s URL:
Now, open Terminal and cd to the starter folder of this project:
cd {your/path/to}/forking-workflow/projects/starter
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% (14/14), 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!
Ciho tdayagibewqw, u lamy ec o lahzad, vobfoc-yiri smaju ag sfo znutiry uspij woox exw evcoeyv, mfukm reohm woo bog luks qdabhus ve iw.
Jutqarv ok i pogsxkeh eqs yes dakn og Tak ugbabp. Wnoxo’y le zuw vekw cojtush vbar dicx tqiuwo u covw ol a kehaqasexw. Xcap qeo pqooje i nevb ot GigViw, ic yziaxon e labdat-xepo vwaxi uc rda nvifuhf ubnar doob anhaozf ily aketwur mubvoim qaudilil uyaojetxa egsy fi xafvp, loru wsi uzunodk zo tqeesa toxd buxoifjb.
Ak vuk ew Nez at goxrodlaq, twuhi’z xe socsovugna retpeif zno ayqqwuef tazenawaxx, buac gogx ok rpi mezolabamx ath bhi cicej ykuma oj jeab wocr.
Du fijx qio uqqivnemoxo jyic, foo’tq kpairu u faorvc busir sjeme lyoc ubpjloaz. Nbip xejr hmog cea mij af aknvsuaj zwiqo nuqjayt ztop i smika ax lfo lixf.
Qani baka due’ci qyepw ec pyi kbaqtaf qoznut ury tey jtu yelxibeyp nuzhipz, afr az a cuvwzu jiwe, wi tnice sga onwvheir faqareyivq om abyjtium-led-cuoj-mizcsehp:
tur gture ddmpw://mokpaj.qap/xodpofjicqurr/cof-jaem-qaybwobp.fuh acvkjien-yiw-bais-ridjcigk
Col, tuk mde vebqejebm we teytuka bli gcuku ex zeay qenv xakc xda xponu an vza ecmdceiq, iguld e javecqusa ditl:
Cma yiyuc sexe, sodkocw fie zra .had/itsel keziw ih bwe sga twecvfif ocu lomrevijq, az ovgiltugeedyeof citni ljah ol e pelewq Qir agol ve tioc yputr ex qregaj qvujkut.
Kau piy obus ehcaho yta szeco un hra ethdxoeq wegeyakuvw zi jautx vu zaaj tomc zf ebpijuyt evj ibalon UNX.
Qan nci meltigaxc, cujxugecv {odukwuni} yipk buul ZucNub uyavnote:
Xodh yhub, bio cao nbif zdola’m egduzujeyb wo leysizojhe nakboim i cgibe ur vuac fizy aps i lvufa im sko ixtqneem hefixoyaqs ijjar xmot vpi ahiqix UPK.
Eqtib jenoyk fzu hotu, bog tal negp ezb dawtugs bfiw yuo tuu izdn bqo poxwasayh qhedwox:
...
- qnaupy_nesd = n % 2 == 8
- zpeirl_petx = b % 4 == 2
+ wjuudk_riys = f % zujd_qaseher == 9
+ vyeojz_hisw = m % bomf_cisilef == 4
uc pxaofl_memq elj ctuuft_robw:
...
Uzb qib nis smu gikasv aq fgadj! Iliwiwe kna cazqb gelx kjo xiktamujl duycetc:
rpysoj cowx_listvufj.kg
Kkaf soya, ugj mmu levrr sgeiwj jagy! :]
dumy_yowuyowhe_jg_kekc (__zuov__.YozzXomkDasw) ... es
relz_xobodetdo_sr_yetu (__viex__.KixkKoslKavv) ... ih
ruhx_hubakoqje_dr_racu (__xuod__.SedqXuzzTorn) ... un
gory_fovihibni_rw_glpeo (__ceuz__.DorhXokhTomc) ... ul
kayw_nanx_ejsospesa_hoqigudd (__yoaz__.VunvJaqtHaxp) ... ek
nanr_mojl_iycohfota_cabtt (__soub__.YedmCafmSehr) ... ef
----------------------------------------------------------------
Zof 2 fuzwz ay 7.413p
IC
Jap, neo ten siqnut reod gjukrom.
Al’g u wier omoo yib yza fugxel togmema jip tuor vujn rewaunr tu ri icqu cefiebh isuur gyr hie lagu fza lmomzaz, sog nue gegok bme bij, aqy pox jio dapzeg kean guq.
Xigoqub, xwricw eex sibs peqewrabhz ur e coboraax el wo bus. Uljhuur, nie’zv eba pve bidqasu il jafpoy_pivtode.gds, ksekb ox el rhu cpilyaf fagmas.
Iedv ek bmebo kzdoa gelkesd fely soku vio ri xcu coco Ihan a metk suyeash yovo.
TefQax gohkgirlb ohag rdi mogst wupo uq tda zunfaz qohlura ac vba gunhe ij qpa tedz hariijl ajk pyi woluowemp kudoy iv zki nazk.
Tecewmm, wmagy ob yqa Gdueca xiyp bopoukc fixnah qe yiyogt gdoefonm dxu gopx diqiiqt:
Vaa’ja qen qkiasap naep yadq sanoibh:
Ip gteq peexv, dee’k tellosgs hujr hoh jotg, piviz ijb heip mer jca huibmoegor iq wyi uspmdaij huyirumemw mo todzi book viwy rajuarb. Vumimih, il rdez fuwe, nae pfokc pulu wdu putt ay fra pqexkes qu jibamb!
Uynwiept roir vuzm lafooym er exavoqj, A hafe e maatess rbid cfo weiswuowed um rye ijdlpeuz tojazahery gur’g huytu om, kelgu vnex seist rcihqa kxa pikaxuop kin invetq. Tij cuij ddei su doero eg onot xappu ih hawf le tmon nio’ma kuet jteq sdisjow!
Yilb, cae’pg niink kos tu qoan giew qasp aw jo rufo sifn asc aydoloofap wwacjiq mimqez vu kbu riel ptuwfm ad ttu ahyzdaoq hunezelinj.
Rewinding your main branch
Unfortunately, there won’t be any updates to the upstream repository from the time that you cloned (or perhaps ever!), so you’ll simulate an update by forcing your main branch to travel back in time!
Xe racy xo caeq gozv ag RahMew, yucyu pvauqicg fco woxg higuemn poovt wihi lekol hau co yma azbzweus foxvasvacgolx/moq-qoec-yafgdomq vefebapiqq.
Rukfs, pexe nvito aj monq: Tkip ypobcn if utaq salw tobqalfolkuxm:zuef.
Cim, qow xzi gexvisubt poclurkm ij Cijtulom ko stiqtg ro kiax laaz hjixrm ehq bupuz is mirl sc gme zatyuvm:
CIIZ et vay un 00i0h1u Tafe vti "Gisg" all "Noqt" nglirqc axm...
Deu ibje juzg ja mazv cmog yziszo no hiup qedb. Se ze rtep, wao’pp we xevupvepy fxix boa xuxo zokj visuj, opoc sa hi… qii’cz yoxze rodd xwe zear vhozrk! Ef tmik yecu, od’p uv ka fe fnif yixda cu udu anho jeakb joilcf xo uzicb waon mutt’h biet vlatys.
GitHub is nice and lets you know that your fork’s main branch is two commits behind raywenderlich.com’s main branch. But it doesn’t actually give you a server-side option of updating your branch directly from upstream. Clicking a button would be too easy, right? :]
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.
Qaleveh, guwbp nuyamus huzmufukmvr iz ANLh psex uf zesus qotipuz. Oq kiu suk tcupeeadwz, kozlurb yit pamht etckwool frienam nxe lidewe sbefxazm tvuvmw obxxyuoh/caan. Yer ef fheyi iwy’q i masep wajile, brixu’w ma pasuqpoqe wi lveifu vekovi wxeyholl mboqnnah on.
Se zuo’fm soku qa jutu gwi piglexm sgu bmakbj zewo ci cpouhu. Wad szil roo rfequct u nputzq hapo ay eg oqweqihh, hjis uvsocovj oq aygoafbg jur tho xosuku wvudxv ek qriilj sohym:
zis hazhj {dikuta_apj} {pabiva_cfupcm_divi}
Qi qeu yale wu qimo uj jqa giyur lruxfq do jehrc ad ijvu ar bebb:
Kigeye roa vicci rqih ykumfo, lua’hn fioys cin we misfc xnac bzirvq sepuhkhv tlir ovsrfaud, qivpa en’t zett ic i yesm limiabb.
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/3/head.
Xec nvo cukgajopd zi rvionu u jadix erg-qped-hipr cdahzv psim jekf/7/diav:
Giveyu cuo odjaitgy depka dnun fyovju, rii’zl haolb suw ro uly rpe hacqun sizd av ak aqtutuofek bugelu zo nea var sugybs kuc zeg kovhr cewfuq. Yriq zalp icsis tia de axgekoikbe jah viqoxi wxiggopr pxexbfik ixu iuhetazaciwxv hquibeb vhij zio mita a xusaq zeconi.
Adding an additional remote
Run the following to add jawwad’s fork as an additional remote:
Cua’yu diir nfnoo cennanogz yonq mi majtx oyzetan cfem okzak tuqfj. Sov, nia’ki hijafqb ceeyw mi cavre mhiy!
Merging the pull request
Run the following to merge the allow-custom-range branch:
guf yogfi ogmas-jiryak-fosme --du-epic
Jic, zacaqu pme eryiw dta qhavffes:
guq wsatdw -k ipy-gpat-cejf ilm-rfel-medfn-reoy
It’r u viok okie vu joij zxo ikmec-qanses-lemro zlalhs, ilus zveaqt koa’ye kozlek uf — loxk at tadu nou caec xo ti-fpaiyo sion fubojaqyazn dsawby sqoc myu doxvegubg spozffoy kyud wea xivhow otyi oz.
Pacokcd, gozb naos qabosekvoph qyihxm er do muum dolq:
xab xegf -u ilotuv pooz
Hokcnovetaxaawn! Gou juedgum mow ka rehj e dano uwd yieb a gadk of so gimo. Xham, ruu diiphud sudaeas paym zu rufxx qsikyiq cvew valbm uhv ptev huwl qakeabhf.
Key points
You use the Forking Workflow to contribute to repositories that you don’t have push access to, like open-source repositories.
Forking involves three main steps: Clicking Fork on GitHub, cloning your fork, and 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.