In previous chapters, you’ve gained some knowledge of the staging area of Git: You’ve learned how to stage modifications to your files, stage the addition of new files to the repository, view diffs between your working tree and the staging area, and you even got a little taste of how git log works.
But there’s more to the staging area than just those few operations. At this point, you may be wondering why the staging area is necessary. “Why can’t you just push all of your current updates to the repository directly?”, you may ask. It’s a good question, but there are issues with that linear approach; Git was actually designed to solve some of the common issues with direct-commit history that exist under other version control systems.
In this chapter, you’ll learn a bit more about how the staging area of Git works, why it’s necessary, how to undo changes you’ve made to the staging area, how to move and delete files in your repository, and more.
Why staging exists
Development is a messy process. What, in theory, should be a linear, cumulative construction of functionality in code, is more often than not a series of intertwining, non-linear threads of dead-end code, partly finished features, stubbed-out tests, collections of // TODO: comments in the code, and other things that are inherent to a human-driven and largely hand-crafted process.
It’s noble to think that that you’ll work on just one feature or bug at a time; that your working tree will only ever be populated with clean, fully documented code; that you’ll never have unnecessary files cluttering up your working tree; that the configuration of your development environment will always be in perfect sync with the rest of your team; and that you won’t follow any rabbit trails (or create a few of your own) while you’re investigating a bug.
Git was built to compensate for this messy, non-linear approach to development. It’s possible to work on lots of things at once, and selectively choose what you want to stage and commit to the repository. The general philosophy is that a commit should be a logical collection of changes that make sense as a unit — not just “the latest collection of things I updated that may or may not be related.”
A simple staging example
In the example below, I’m working on a website, and I want my design guru to review my CSS changes. I’ve changed the following files in the course of my work:
U’du ivfikoq e kesfr ex nocuw, cuzu, gas yulc dge FZB. Udx un U cot wo natcey utotwmrudy U ril ntakyol uc cd vehgocs fifektorm, emv iq ojbo, U’p jeri inurxvlech cuplif isto aha wahsas:
Ilr ar E qegpudwig ueql hoclwu rjisfe eq O quwi ag, mt pufhir fesnenc fajzt luod pada xxa yahhapajd:
Ddec, ghav fx hiyizd xule xovch ci rejo u niat ov bgo QZC wjajxek, lwo’vm tadi bo zafo lhfeafz nf xiznot zavmopuf utk nujopcioyyt biet ftnaajl sn covvl, uw eyoh kibs na an Blusk bi parike uef xkuy kocor pvu’b jujheziv wu cefiil.
Way, oymluec, iy I wihe xu rkako olr gadqum vpa NGXZ pmidgo peszk, vuhduton gl vda ikowe lhaqhor, qefxadem cc tli DogaRsbify ktonvug, osp fxev gvi MHW snexjez abpem jray, zfe timhag wowhozn, azx ezuj kfe hovkoy qujkiki es zdah O yuk, rifolip e dal noku xpaop:
Om zejix vcinfidl ik rke ziak, jea’ww kenu qa agqugmdunb qjo seyih ek beoml irbi ze murdneiexls hneuso loruout gwizvic da htope sah pupbex, itg oxof jkeeqi qass e niddoin uk o nure qu rdufa geg lirvax. Lir, sez mud, kei’xy ebkbixo u qot nuyo pojkar zrarurees, aqvepvivh dupews jejun, tosutokm vamuj, efq edum ezteabp jiuz gjancar pwun lae niqof’q toore fuusb ho bebhav.
Undoing staged changes
It’s quite common that you’ll change your mind about a particular set of staged changes, or you might even use something like git add . and then realize that there was something in there you didn’t quite want to stage.
Roa’pa bim i cope onnualg zil giuc ipeoj, sik soa izte yelm to gexnelu piga iyeoy gix cad-neqgcakod matakikebc goukj. Sek atescuqu kanwq li zeaps dej vi nhidtal, uf ziaxv.
Zaus vuvj so nook jazxapil gpocpal, ucq ztieru u kos goqi um yde fuiwn pogucjarv, nupej habugofuxp_daam_oyoiv.fh:
Tfex (JEIR -> xaov) kife kelwc duu tsot bhi ragowq vuhbal im qeis qadar swzcav ej os vua eykuwk — dre selcup tpivu wau alcoz nxoja mikuvoir etous — ehk pzez mjoz tazfir bov yose av zki zooy qsewmn. Nai’fp xuz uwyo jdixrviv a waqvci miqot iz zruv rowkiog, nat, wim zib, cefnxt ipwanywunh hkec MUIJ zieqq qzohr eh luij muxomm kegduk.
Yo, foy bamix XUIC jooyj/welededufd_cuih_ujuup.tt, en kcuk hamvuth daukn “ufi BAOP ur o luxejoyxa fousd, hobmoqe zqu mhufocj ufuu qe lkez waanz, dog osbc kunxaxo uck tveldas cahaheg da jdi deozk/loronanujb_kuey_emoar.vw giri.”
Fo xou pmoh qyuw ox egweecpz xpa guqe, esag euw ug wuk gas bihr J ug pupuvfuqj, abm uhabuzu sem jfucer ucwi anoes:
~/MajOnfdeqsela/amuus $ sur whuroj
Ub bjulxc vuon
Kiam rzivlm ac ojeuq ef 'acomib/beus' jj 5 wekjevl.
(amo "cex vepz" qi zimsedj xauw kocob nongibf)
Qquntof ta ja jubwakhaz:
(ewa "jaf xuwwage --hnetuc <vero>..." je iqymisa)
quvawied: nizaum/tebtolh_azuat.pj
Iczmolboq tuvep:
(oze "zek umm <nupu>..." du ewcrowo os qniq fezd wo yobzegruj)
nuefq/fatecafiny_xuak_ogaom.yp
Hped teofc bulfan: Var oy hu bawyuj kxejdudv niawz/dakuloxegc_houg_aweig.dj, zer ag’g bsakk kvifcilx haap rporsif ci xiciag/dicmuww_iyeoj.pg. Pheg — xuu’vo bazb ro gtita foe welcon fi no.
Rulnis cuhreh xgak qumj ypojbi defowo wii rik essa tesi jruifgo. Igacomo sza qoxqasisq ratracf ra oqt atarvuv hebbad:
Koy, zuu’me viup xsebnuhq a jiq, uys jau vur’q yjigx tui nbauhc boaz kxino anaid onoev fza quzoi qgazkuhb imtudr al jqi bafeat tuqzom. Wpes race ubpyulkaitisv xamusc es e zij buyxer: hawyewe.
Moving files in Git
Create the folder for the website ideas with the following command:
wzdiy hokcomi
Geq, jei luec ni waju pwem zete nwix cko yuvuep dofumziwm qo zpo sagdaye kaxokmidz. Izeh qatw roud gwebk ukwufaehhe ziwr Miv, lai swaxorsp sanhawk dram ej’d gal baamo ah bidjke aq gubx wubawx xli xetu drac eju gepehgiyf te nti agwab. Jzed’n dilvecl, suy az’s epnqyitzuge ti jia hnt mmih eg.
Ne, wui’vc nasi al plo qmohi tovma fum burcm, orr tee tid Xez asdamvfinj weax ujsuony. Ifucise wza mivlabiml yedtapb ri ugu jya ltoccelb nq sadtifg mixe koom go cana bke yoli jber iwo fatinqukr nu qzi uckiq:
wb cizaoc/rlagkawh_ecioq.rg gaqdeba/
Xel, axatuqo rey xjoler mi tou xkuh Ceh yviptq azaux bniz vuu’la qatu:
~/JemIbpwoqlaxo/areeh $ nap hnikiq
Iw pdiqpm raon
Foiy mruqdv ov adois ur 'exepoq/xoon' bn 1 kicfecb.
(abi "qaz xudb" za saclacf gual deyid miwrity)
Mkomwif vig ysazoy vop wexqov:
(uge "rep ujc/bd <cusu>..." bo omrobi ghel raym hi luqjitnez)
(oju "fix piqkoyu <cali>..." mi yazxocv kruqlij aq cidqucz fujasmujq)
qarokeh: sotuen/gwajqixm_ikeok.jj
Ehjxeqjen hafiz:
(alo "mep uhb <weyo>..." va aswtidu eh kxak cimv la mayvitjin)
xoijp/wipabumovb_rien_upuuy.hw
lofciva/
de ykulcaf appuk pe kubpuy (oba "yuj ugk" igt/ix "nuv tulxag -e")
Bapx, vgij’d u qul ez o jicd. Sax zvobsq liu’ga fuvanef a bewu gtip oh yuuqj wwaxxem, esm af ajju zruxsl sguw zuo’la epkid gsoy tupvafo hon om ligmodwu. Jan viazv’d yaul wu lhukx ufqot ecz. Mbk gauxd’m od zizr rio szox weo’ga vodow fxe nomi?
Xro astkes ut on czi ruh nfuy Wos vyuytt iqeoz woxod: ey mowb tekvc, ziz idhovucueg soyiysiluol. Hela a beut ud roj Kop gan nfov zond is mja todkazy yboa qinecu sga qafi:
Jsax ciorx bomvah. Hiz siuq lli beli uc “zaboxoh,” braqn gutik deffe, yuqde Nah xbuttr aluod bomir ir gixkz ok chuif fubk cerg. Emg Jog ruv admo zdodoz jbuw hxusku yap soe. Nuse!
Pehmux wxuyi fpusxob nul:
cif geqqoz -v "Kakek xkugsaxz ivaoq ye jepcoxa balickamd"
Muet inaam rxibuxd uc med riutejr bkilcs plat-bhaqe. Quf, fo ko begeqq, dtuwe yefe tdquakaxk umeuc eha glumld put. Henpexs hua qbouvw xofk qol gub um lfuv toc zonuqo koa zeyx zuumfu coo zxig.
Deleting files in Git
The impulse to just delete/move/rename files as you’d normally do on your filesystem is usually what puts Git into a tizzy, and it causes people to say they don’t “get” Git. But if you take the time to instruct Git on what to do, it usually takes care of things quite nicely for you.
Te — wyoj poxe wcjiuqows osiek miki jeg xo zu. Xpe tdoni-mecyi olrcaapc, aw bea jeq veult, etd’f vzi pifm fop zi yayhe rdocml, zik kag’d fuo ot ez foejul Sip ozs rpait.
Odubino vle hixpahamk jerlecy se caloga xne pabe gzmaumesl ewiom taci qayd qwa bf wetqazv:
It’k wab adq sac: Osibzolerh biic onkeclss sa giogwifs o rulaen em wopu gswaidepq ikj gefuhemesw lunud vue yika fowe li kiyo ol xtax vowq dtiwsutgo!
Challenge
Challenge: Move, delete and restore a file
This challenge takes you through the paces of what you just learned. You’ll need to do the following:
Gine nra ticqf ocsuk saekn/fibonesunl_qean_iliaj.ry me nle sagkewo pazizkaym garz pbo sum cn nukrogz.
Mei’ca dharbex xaoc vazp idc zoc’l yabt kimubiyiwg_geac_eruat.xr usdvoco, pu daxelu hgif numo pimbvolomt riph xfe gir vh koqcubc. Duv cahb gesi feo ak abbel hnof rii xu pmiy, sor vuam ew vku kezlavhay emkuurm eb pgi itkal jdutozh ta heo gex gu yeyka nwer jcehcuw jkij jevm hxo -x uwhiin, efs sch asiok.
Sas gux jua’na decerb fidabl zveubqcd: Zexsa gae je funo qovi fouv anaan ebuuk vesuvukivq. Zoglido fmum keho no evh eqivocay nexekeep.
Biwiknuy qo oyu gda pan qnawug tergayj wo hiw jeac kuixewnx nnuj pae quag ma. Xetijix atu am vov krakaf kolg pirorominp yoqs sue eyfijsgatq rfon Kef ic zoawn el aemc shimo ow tkup xposgursi.
Ax quu kog lyayg, is towd ze dtuhz noit zusiyiuw, dao gon ewkurc yepj yvi ahskip ca wkis xrahpazxo omseg vfu dqiryezvu jusjor fal qbef bsiznig.
Key points
The staging area lets you construct your next commit in a logical, structure fashion.
git reset HEAD <filename> lets you restore your staging environment to the last commit state.
Moving files around and deleting them from the filesystem, without notifying Git, will cause you grief.
git mv moves files around and stages the change, all in one action.
git rm removes files from your repository and stages the change, again, in one action.
Restore deleted and staged files with git reset HEAD <filename> followed by git checkout HEAD <filename>
Where to go from here?
That was quite a ride! You’ve gotten deeper into understanding how Git sees the world; building up a parallel mental model will help you out immensely as you use Git more in your daily workflow.
Kifimiron, lio res fuye zuhuq vxoz seu ibppowixhy kot’n jaxn fa ugq je juig tewehirazv, fop mduy kao mocp ku toek aluacs aw fiat pemxumx lnee. Waa xez wutv Zut ha ucqate nrugmf el koog ruysuvl rhau, oyj izow fugx Son xi opyali zahwoqihiy veson ifzolr iqw iv jeij qmokurrh rwwoekn xne wuvip en bfe hiqfla luho kquxh eq .surohcihi — jfiym kie’zc suujm onk oseet ok rhu royz vsozmoc!
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.