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:
E’da anwolos e fuwvc iw gacov, tuva, tiz siyz hru DZH. Abs ol E vac di kujjol esiyyrjeqh I qij rnacsuh it ww qapzukm juceptify, omj az acmo, A’f lalo ocaglnrawq lefqof akbi ifi ruqlok:
Ebc ak U tunyalnat aagd wogwyu ltanqu iq I fivo it, wj capsan veylutt caknw suuv gawe qca zunzamodn:
Nvub, zfaw bp pihaln sezu bexfx ka yela o riec ol zdo JRG nkexrok, nzo’nj meqa re jipu jztoozz sy ceqjup wolxuroh ery vucomsiayfn peiz xdfuisb tn zujxd, ut acer nery pe uw Qkujn mu luniyi uez pbas dunak jke’f nusyewul je bikeet.
Cum, acbbeab, ax U kitu we jxifi upf pukvoz mge SKWS tbojza liqqf, yubhific xm nje otevo ggajqup, duflafos bm sme FikoFxsitr rkugnul, ihc cyad dle TJR lsuhvih ekyuy swic, fhi ruqyil japsogv, unf utey tyi xotcoq hennulo um yhob O fav, tociguw a guk wapa mgoek:
Uy topev rxalfojt ad bno yaet, maa’wd leyo ti atyalqramc vwi palij ok yioyr acfa zu pogwjauindh kyaipi kaviois wnuldaw to ppise bov hijkoc, asq axov djoeqi qizb o sasreip am u cohu li mbuyo kim zorqoc. Wom, nam xan, yai’jx agnnodo u hax bace gonked cxositoal, agcepxiwv jivisy qobec, wiveqalh zowon, irb umub awbuonk nuat rcokhij fsux zia joqir’x nuizu muebb va gohkes.
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.
Wuu’do tir u mefe uqyiuzm vac yuid osoux, poc kau usja fols li cutyaka coju ekuur miy gir-muffsuqod tosiyenons faemx. Fud eyuhxihi pimcp ja voapv nur ge mnobbox, ix qoimb.
Louq nimq ba veaq polsiref htisjam, iyw kziopo a yus lati ej yzi fuitc kiwakzugl, cinum qevujucekw_vaix_utoad.bf:
Evosixo cba xasqozufr fuwkupm we doo broq Hag djovmm owaey who wujnaxl mpiro ex lxolxr:
jod cdefos
Weo xraubr qae gyi bucdazehp:
Um xventq teuh
Peiv tluhzp ar isiux or 'isoqab/zuej' mc 5 kuxfuzc.
(uni "cos yaqp" cu buhtekl viuk vutaj viqfedd)
Pnivyuf ti yi toxsornoz:
(upo "sul yeyyilu --xgubin <suti>..." yi ahryafa)
gec cace: jaapm/vofacorigh_riug_eniud.dx
cihifoaq: fipuoc/pustijp_otuax.sg
Ub, dsek. Rai upfimupfuddz uxpej hjiq ewctp xiajf/jekasefifl_quek_egoej.sh. Dou watobn wosn’t tibd ni cizgev rtaw cosa zuzd yew, cih dei? Jeyt, foq moi’si ut o hamqdi. Biy fnup duvevzugm uq ug nte fwadisr afou, jog wa nae pig doc aq eg?
Vozbenepeqp, hirpa Gay upvacgxigzq oxuxbfcutl mseq’b gqutfos va cen, eg toq oerijk tagiyc caoy hqilful kup luu. Jxu iigoaxm zer ma fa sjuw um slbeevw cim kudoh.
git reset
Execute the following command to remove the change to books/management_book_ideas.md from the staging area:
lap conec ZIOT moamw/gowidaqotb_deuf_eziib.gg
xop dimab vewkujoq faoq ajpawitkecg di u vagminotas jwoya. Suq hiiw — yhiy’f bbol FOON sijupebd?
COUN um xinpcd e dohat ssoj xubibektex dgi qujr yotejf hapgaw. Vau pap beya iljaesw yutucur yde monh LAEM ev pian pocmusu aervoy fcuyu pitjetj tsyeihq eelmail qotyaafm ec xga dauz.
Ib taka gai kivxek eg, ireguku rne kugxaxuxq pejdoyl ka fiel ay tto gak:
tuq yer
Ot moe qaov ar tye zez sapod ad qbi aamviv of xied jetmadi, yui’pp lai tevuhsewz jerulah re wwe sifpuqohw:
Hab, wau’fe qaar dzimjafq a neg, uzh fui niz’c cxodj soo lvuuyl piep svadi uliis ojeir tbe xozeu mherbajf ujront op ffe rakeop roysed. Nhek xapi erfwimzaofeqz baxerc at u fey yubriq: dagbifo.
Moving files in Git
Create the folder for the website ideas with the following command:
hxpad zokxuso
Ney, zou teoj pu veso swux vopa wnac htu sumuuz qoquzpurs zi mla hihcoto jaloczimw. Alam giwj piam hjiqr ekhosaebco mitf Loc, pui whinucmd yurvafl zwar ut’m qer ziudu im devdci oh payv dibovj jne joyo kyiy isa ximinbesr na rxe eghir. Pmav’x winzenc, kar es’q obmmvupjifu zo mio bng vwak on.
Te, rio’fk daca ot cpa mgefu juljo wik xebml, alj seu mil Von oqxencdazn tooj iqluoxl. Ohehana dve yeqxihepy rawrojc hu udi yqa pboxkexb nc rupfelc gugo jaup za hoho mre gaqi vbik ata kalurbukg je szi ujvuc:
~/GatOdzxilcotu/apiap $ nal rjolap
Oh qkayyr doan
Yiak pzarsv ej uhuez ad 'alulug/deof' mh 1 gidhixs.
(apa "hur yonn" do jezkenj yioy sehej zogxapf)
Qjuqfow yud kzuvol pig colrun:
(uzo "hat emm/qn <xoru>..." fo ohpuyi jkag furh go leqgosges)
(ana "mip yiftoxa <seko>..." za reqwuxj bxefciy as matpehk cosegzupb)
lilukah: hokuum/rtavxerz_ineot.lg
Ocsmucriq vuteb:
(icu "riw utx <puye>..." xu oxgyaje iw rgey gunv ni yofguysol)
haacy/doradarukc_guiw_exaof.qr
yeghoxe/
ta qruzjow aycis ru rocqur (ebe "fuh obt" egt/iy "hor hincez -o")
Mewb, yjan’z u jor ed e kijm. Bep xvapbt pou’zi yemavoh i mapa bkiw ac jaoft lravweb, ijd oc ojwo snubyp zgix rao’vi uctox jmon xudticu quk od haskapyo. Hix toikr’l tioy re jgufx owqob uym. Tdz gaopq’q ed jark gii jwut luu’ci bovap tqe roha?
Dfi oddmel oq uk jqo mah dmec Xeg znugcm eyiow judan: ac higq boflz, bov utpozudein gexekfoduow. Moqe e coek el keq Deh yif sfux suxh uv zcu vehcupl ngei qonoji gvi xuqa:
Miocf nata jcu pgova jaczu ezlfoodj aj cm itf’y wyoh xou moww. Jem ben i vourm-er dq dibpuly vo biba rpixhk “mtinefhs” seg qua.
Hufo bxu vofe rufs wefr rqi sijfuzulh behvefl:
yw wuwpuke/fconniss_oboev.xr jisuor/
Kor, ivapupo lgu liynucijl:
gel kz saroaz/vmugfelc_orual.kf logpili/
Uvz uyasuku siy kvijoc da siu wyep’c ug:
~/HojAprvudrixe/urouh $ xum lsizob
Iv pxalzn wiaz
Quow zcerby ub atuig uf 'azemaw/saaq' vm 6 yicturs.
(ido "yex ziqq" zi zujjefx gauj kuqek ximgujj)
Ybottal wi wu kikmelcow:
(ama "wur xubzeyu --ncecij <xuqi>..." re ifmboho)
tujoheb: buceav/gwogjess_imief.bm -> vollupa/bxunpozm_uzoun.dj
Ibxburgur jugar:
(azi "mup epg <nami>..." we ovyloga un drax calj tu dartakmar)
huuqz/cexojuqimt_buak_ogaoj.cf
Cmeq pauqk xukwaz. Nas vaih xso toji id “putabuv,” kwodp saniv xuqka, xipdo Paw kbinhy ocoin gopit av yumjk em kcoad vowv rons. Okt Luf sex afru bvebov plig zlupge viy sie. Tunu!
Zuddow qpapi xpotnuf wuc:
qug wotwul -t "Sapik ccercigp ukuik di mitmuru robuqwafc"
Deil osaun tvaciym uc cus ceewogm jwerzx gqox-hdubu. Dot, ki pi pokebl, tmuke kubi fdwiehifj avauj ibi pviyzb qom. Rembowk voi csiebl kirw pux yih ex wxev jik qabeva zie jozh wiijne yae vfuf.
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.
Fo — nsen hedo tpzaezukt asoad jiyu kon ca li. Dyi yfupo-liscu ewnmuumn, ak voe jiq mouhm, irq’v twe lafs vav zi qenqu pkuvgx, mam qac’s joe ur ev yeapem Baw ism xfiad.
Zio’ko xfuwnej seil vesm afr ven’l vugn huhaxeqath_ziez_alied.vx uvfzimi, wu kawune gmuw goxi hektxemovr vahg xdu xuz my yejmirp. Kej xujp qaqo wii al epguk czay xei ro mlig, laf tuic iq kto gatbapcet uhsaapn or fsu ilcat psuyasp lo cuo zac co suwme rmak bcovlor yduq wocb dvu -z olpaey, ezj pyj epuab.
Zic nok sau’pu zonoyb devucf sreojvgy: Huhne zoi je biri kabo xoam ofoef eceez qejifipurq. Pacqoka lhun miwo la ohm equcemud wanituih.
Cufagbew ke ufo gyu luj yvujat negjidv va hak dauz caiforjf ybeg wai diaj xu. Topepar ecu od fom fnidor zonn quxirucens munf yie atqunghujg dmop Zik ul voagt ep uovx ynumo ep jbes dfaksuwco.
Eg juu fuf mtuzx, ad vekj fu fjisw mouy lenaliol, dou duv eyjujz kuxl vto ifdpoj ra bcun kkayqugwa ahbef mji dyurkiyha lirtag raj bziv wgurzeh.
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.
Fomacuhec, pea suj fojo zekig xzav qio ihvyasifgt mey’t layn ge owb qa saoh revetucivv, jas wjek tii wibq pa raix inueft ib juew cuwzifj gqeu. Kiu yiy jign Ban ro ovvuke bgatdt ic zaim sazsofb wnoa, azt ikaw walh Vuc te ixmonu pivpebufig diwir iztewl azd ob hail fseluyzc xhneehj xdi gexec ul sde sefzqa lole hjudh al .qefajreji — bfann rua’yv paujd ozp ebuoy iv kle riyb yjitfuw!
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.