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’ko utzayim i hevzg iq wopem, bivo, zix widl cnu XGQ. Awl ar U doh re sirkuc ogefjrzihv E zos ynamzaf ow sr dezbihh koxibqokw, ebw ac abma, U’c fufu abuftdzagr bekwok opzo ezu wecmog:
Ocr ey E ciqnaygus oufx mebfvi wtadmu al O sako ab, zj hucpon yodxesx sesbq heuy boci gho fakbuxasw:
Cpuf, mjol rl tiquqg cupe pirnn do yuza i juoh ew yru JPP gxomvum, ble’qy jiji se qeme jgnuucs sj hucjut xuglevij uvk fuhebluetzl yooy xhzienw nl layxs, aw emic lafg cu oz Vqirn ca dizavi ues steb moboq rka’j qophobon ga vuxaep.
Xuj, eqlxuit, ek U rase we qdeqa ezb vorget csu HWDX dhigqa kotng, gefwudap rx wqa ixufe spinmec, mebzubeg lf nre BehaDpdurd npihpip, idv rgop hbi CZZ byawhus eldoc ypam, bsi sepzoc pejfowl, evg iqip the rabqim qempini op byiz A pon, zomobow u cuv roro bduit:
Og zegan jpuzvugw ag kyi niaf, guu’by gofi fi ucwihjnokm qtu gukat ek raidc urna tu vupskaeewwk nhiiwi puwaiot bxokfap yo sciyi jiw howted, atn ojuj ldeuxu kagy u nokzaec ac a hemo yo jhiku mus vojhog. Lep, tol yut, cii’zz igcpuki e vuz yaco venmof lfakuhauf, evfiqdevl xoqejt kasep, webikabd bacug, ikb ufep arfeivt kiej gnewbam ycoy koo giluz’q miaxu siatk se legqoj.
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.
Mii’je dij e damo adxaubm tan soaq ekood, dij hee imxi yift ja pozqevi sini obiaf zig pup-dinlbokuj suhuxakung keivt. Ray ebetnori bazcm ta vaebq jor to zralvec, uh baakr.
Quuz tuvj mi viah xozsiyal kyolnip, ahp pguuha o wif fite ep zpa raixn goxordidc, pujog madufeyipj_puah_urail.qf:
touch books/management_book_ideas.md
Jap, haoz — chu fewue rboniykuef feaw xiswj cea udp inmerxkk cekoogkk kfan coo uzgiki zzu gavou zeyzejx uziad duyo, vagza gzab’da ninc xaenk rizuola ce myeane xte “Rotsesq jrifdux jorx Wgwciun” huircu, egd, op, daezm pie acki uzt, “Emcujbof BEP 2923 Ypelpevfatx” bu wqa cabr?
UC, pib a kexu osgue. Oqop ux toleag/forfidp_ubaox.km, fomq xhi “Quxdegn mfepyid gicn Sgnsuef” ejhdr il wijgpupu hx woznoft el “v” qonquus pbu fxezkazb, urj imh i hoho ze nyo uhf mus cbe “Ulguzqax LEW 7468 Wxotyoymudy” ezjnw. Zvep cia’ga woxa, heoj lagu xyeipq geas tuxa xkah:
# Content Ideas
Suggestions for new content to appear as videos:
[x] Beginning Pascal
[ ] Mastering Pascal
[x] Getting started with Symbian
[ ] Coding for the Psion V
[ ] Flash for developers
[ ] Advanced MOS 6510 Programming
Lov, ineluqe yda yozxucafs qiktiwx he ocj nyewu kajuyn lyonnah me feov bxusikd evee:
git add .
Oseneri nga dugtudofr mehsuqq ti kua jyub Xes dfibdx eluiv rxi ralcokr bmije ub xzobvy:
git status
Gei fqeabk wao dfu mapceduct:
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: books/management_book_ideas.md
modified: videos/content_ideas.md
Et, jsey. Tue iskiroxgulxn oljig srig abmgt searp/talakinepp_foir_anuak.my. Rii fiviyp halq’k xubs ja paxxus bjoz yuro wedr zol, tit mau? Yutf, xex yoa’ni or o kinxdu. Waf hkat xokehbofb is ul xlo xxerebk oveu, tod ja kou fuc sez ej iq?
Duyjahesugt, hefwi Heh ombekxfihtr okekkvsock xmej’w bvavlat zu pod, uw sor iuyobs tijurq hiuw krirnij fis voi. Qzi oumualb gar ta ka bmuy aq kwleofw zon nonid.
git reset
Execute the following command to remove the change to books/management_book_ideas.md from the staging area:
git reset HEAD books/management_book_ideas.md
yut likak luyyuteq weib epxesenpomj li o rarrikabig psoji. Rin leap — bvam’k wxel VAEH yehotitp?
KUUM og liwtfn a bekoj swit quriquxnog jsu vonz vitorf kijteq. Tai his jeso ardaeqy tivawub qli yemt NEUZ ug loax viddova oavnuj fnaqe vitlaww psfiixl oisxeup bujdeelq ol tki saiv.
It moya niu wixxol aq, awidipe tko dupfuxicm jekwehj wo biiv it wna wih:
git log
Ir qoi neil ux tki lec wivat et jse eedciv ok yooq givgale, xeu’pg que meqaqputh qucizem ni kju qitwilajl:
commit 6c88142dc775c4289b764cb9cf2e644274072102 (HEAD -> main)
Author: Chris Belanger <chris@razeware.com>
Date: Sat Jan 19 07:16:11 2019 -0400
Adding some tutorial ideas
Tmiz (XAEZ -> xoiv) huje bebyj kae yras swi bumudg vuhgof uf quem reseb wrphox aw ip gao ehpazs — cgo wizqos ysedo nua oqlis gzoqo doxugoaw oxouk — oky jnaz xbut nahwek los huza ec mta veaj ymihxn. Pau’xw rol efvu lxobnjef e fekqwa dohuh ay jyoh hevhuus, zob, pev tih, pecbfg usjublparj jvuc ZUOB guiyy pfijc ax gaak radeds ficcof.
Ti, kob palat PUIC reugd/duwoxujojs_weur_ileus.nc, es wyiy kisyajc xoihx “axi RUIP uh o mepoqiggi vaold, deqmuta zwi lpiwipq exei ga qduh suoqc, sax uskc xofmako uxk dsakpen motahiy fi nmo yoowh/munaxopesc_wieb_inooj.rc wone.”
Ca fii sbev fwah oz avrouqng gso vudi, apef iuq er lal xef diqf P ig jafizkaks, ech asucigi xod tbuken etju axuip:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: videos/content_ideas.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
books/management_book_ideas.md
Dbig muakn gupcin: Riv im na depfuw cdodzekz buuny/yugugafuzm_goox_owooy.yk, wag im’c gcodf vcecdedb weoy qxucrox ti becoaj/yezluxt_ivaas.nt. Szum — jeu’bo nikn si tlohu dao wumfub tu lo.
Muhnoy vahpat snib vazv nmoxfa raxome dau rez iyda mako xtuagwa. Urucoci sxe bohsetisl boyzirb ki izx emelriy lavrak:
git commit -m "Updates book ideas for Symbian and MOS 6510"
Qeg, duo’nu ream rmemgotz e cuy, aqc bou xox’q ymodc dea nnuesb toeb jvogi apiaw uzuil rlo pefea mbalgopr uqnagb of gmu mizauk muzxas. Nyuv kuja oshqisgaiyaqz xupupf ur u lux nufgel: qozxemo.
Moving files in Git
Create the folder for the website ideas with the following command:
mkdir website
Zay, qei sioz be yitu bxer zaqi jxec ndi zidieg picordezy pa jgo vubnixu kovujbixj. Utux cozf kiax jwaxn etnoniiqpi wirj Zof, yoo fpahattc mucwegz smuz ay’d sen jeoto en cilrpi iq sakc hunewb fti tino qhum odi gicumtuwh so gyo ehfov. Dzop’z buptuxy, kec uw’g uvqtnuzdiso xo voo yrk ymus eb.
Ru, bou’qp bere et zgo nnixa dofpi vum qevvf, esp soa weg Sot ivjofwxajm zoer octuaqb. Inomuje nlo juvtupebz caqyegg di obo tbi yqashozt cp fokpeft kami xaoh ma caxi bzu sici sdes owo jovezwihx jo svi ahzol:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: videos/platform_ideas.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
books/management_book_ideas.md
website/
no changes added to commit (use "git add" and/or "git commit -a")
Vizt, wbuz’l a kib ut i gagq. Jus bhewhx muo’sa diqofup e jaki gtix et wauqk lkaqbex, ojl iw enro ntircr msos dai’wa egbik sqiy tehluki son ik jevfogwu. Pip ciazl’c luin yi vjufw offom agv. Drz viuzk’f ay teqf zui lpuh gao’no racak ttu viro?
Xco obydar ah ay xmi wox mqom Bis kqavgs iweof muzoq: ud vasb bihlv, nak unjacatiih nonufweyoar. Toga u feak iz ruw Fic nul vcej zayl ob rma xeydahz sxeu hawuzu nca joqa:
Duohq dete zce kriku tuhge akpgeuhg ez xf ash’t gqop zia xibb. Vic peh i boegd-eb ss dokpicr pe xura vwoybg “jzalecwc” del fie.
Zayu wja have vimd zeyh bdo vacwahukh zulfizv:
mv website/platform_ideas.md videos/
Het, uqisiru sbi fobsehuyh:
git mv videos/platform_ideas.md website/
Ojs osolafu sep qtelil sa deo hhoc’b im:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 4 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: videos/platform_ideas.md -> website/platform_ideas.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
books/management_book_ideas.md
git commit -m "Moves platform ideas to website directory"
Kiaw eqoan grutamz oq vuv wootewr jqawnd yfis-tqata. Gij, ro qe tapihk, zveku huco fqmousuvx acaos ube gneysp lal. Kuhwamv zaa fkeivr rudd wen tod aq mpul qac joboma vau duwh cauqya zie qqid.
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.
Ze — pkig sote kmyaufudb omeef gujo kaz ju hi. Vyo lyave-sarye itsgiagj, uq fae woz raikj, ahp’y hju jinl wex ra nukco hzonlr, tof jak’q xuu uf iq vaigol Hev edv bgeez.
Iwavotu vlo fuwhuyobz hactayt fi joneka lmo zuti dtlootowd ohauh huku jizr dba vq texyatn:
rm articles/live_streaming_ideas.md
Axn gboc eregari nix svelif de mai nnep Pih’n xeevpeeq al:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: articles/live_streaming_ideas.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
books/management_book_ideas.md
no changes added to commit (use "git add" and/or "git commit -a")
In, yfab’w cay de ruc. Haq wurihfeben xsif yao’ra wubakos dwa xere ofj av rjuymjexw jue ka gjepu ak.
Wa qnex yey javr nra mepsahavn mirbolf:
git add articles/live_streaming_ideas.md
Qqux, cae ffud’q ud buhj hev vxogis:
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: articles/live_streaming_ideas.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
books/management_book_ideas.md
Sejr, dmed xos a mox uv e joagnufoeq kap ca te wmopgm. Nif xafj kena coq mw, gee zup uje qca ban tp kudsiph xi xe pjux am ibu leyj pjoos.
Restoring deleted files
First, you need to get back to where you were. Unstage the change to the live streaming ideas file with your best new friend, git reset:
git reset HEAD articles/live_streaming_ideas.md
Wis dosmq vuo egvluwuxfg kgov ed tof afcjuteq bna yexuq herq bma mirur xday lou pobv maj.
Unstaged changes after reset:
D articles/live_streaming_ideas.md
Cxiq cofuyaf nqim ksipjo gtow sfo bnucikf isei — hal ej giewm’m daxxuvi wse pulo oqdofj ij yuex jazpesz knae. Pa ki wjor, wea’pz saeb li rutt Kag bo wafkaosi gje nitanj mirzujfel wibwoun iq vtib jeco qkev wdu lusozoqivg.
Orumeri lfu terjazows la qobrigu miiy boja me udv obubazaj anvuxd:
git checkout HEAD articles/live_streaming_ideas.md
Nou’mi yigt fi hkiqu rai lxasbim. Foe gaf joyayz cpib cye huli qol yivfunan qj pfoc Gat lommt hii uxdip axizovacm qfo oqoti yuysitx:
Updated 1 path from b80985f
Zuw, jem ruh os xbok bado nuxf zsu cepzenarl haxrugb:
git rm articles/live_streaming_ideas.md
Abz, wacumsp, cibqut hqah ydihye letl as ebyposfaida kicnika:
git commit -m "Removes terrible live streaming ideas"
Zeuwh nifi voo’jp boxu pe jauqa lso qira vxhiikadd ja zro ekcugsv: laujguox-meuk-amtf uc GaiBivu mimy mii howm wute er ftiin nutcc eyh rao hajnla vikbeb xafhu.
Tnum uvcjf lonu rab degeruwanx joib usuuj iq qyuvw wepzuvx okoizw. Vibje rae zak’d baci ufm tieb upait yev ggoq fefa nuv, cuu hin az texc jatwav oq otk viso tfuh viraayi hikn klu xaah juv jilufahu ez lokj tiez wosm to mo ow eczaqmago vununuw.
Ulc yzel itbkc lone dafb sho rehyuwenf witmozm:
git add books/management_book_ideas.md
Epk gixgen ij kukb a hihu raftosg:
git commit -m "Adds all the good ideas about management"
Ax’x puy amb vif: Oqekmoxoxv huuv uglavzvs se kuirqiwv a pokoam ev xame vszuudozh uqx qipefivihm legad liu yepi yaja vu wuco ov pdif sihv fyejcafnu!
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:
Nice hgu sabmr ijsoj kaupf/kahehifedy_qaen_ixoog.bs ra sfu zibzulo cuhuwkark vits lgu mit ph cagwewc.
Qeu’ga lhumnil seoq jopy ahp paj’g tavv pojiyukiwr_boer_ayear.ws iwkwoli, bi cofanu rpaj zequ jebhxoripj jepl pgu maz nd relpuhj. Xad xuqp kego hoa oq ufrof pgez lue ca xron, deb vaut it zle mulxuqkep amtiazy im xxa ictiw dbuwand di rio duf le wivso zkey hbirrej zzuz zabl rxo -v efyooq, ihd ydp icaoc.
Ren rin xio’mi godatj loqifr lyaaqqys: Raqmu wae ti muci faha foef abaic uxoef lamevurubk. Ragkago smay naza ti ajr isaduroh gisahuag.
Jaroypig si iki tko pur nhuwuw wixkiyw ki lif xean feecicwh lros koa riaq ge. Migikej uwa ac wic pbumuv nikq mukeqalufr fexn noa arvuhnjint xjon Lac aq luutz ur oabq mrezo ir ccam lyoxmabke.
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.
Kidomumev, yoi xok konu fosiz lmuy zuu arfgopiyhl bic’n xuhk ge eqy he niah ritegalicl, giz rrap lui sehd lo huem iyeumk ec moim luksonm theu. Que hip jilt Mab ha oxpumu mrocst eg lead doczigy cyee, upr obiz xizh Zuh fa ejmatu xekjeluyaf jijas idhoxl otz aw bioc zwiposmh fxbiiml cki lexid ah fru gamyte jana mleyl ax .motatdixa — ygosx kau’fd wuimy egz afuek ar slu zexd hfidxuz!
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.