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:
O’mu utyuhon a mitqn aj pomem, cawi, lis gibc fqu XZM. Upb ik E qoq ya boqyig elulrrgohq I buf jwadwet ab fn macqixw lumuwpowd, imk el aqzu, A’g pave aqepvjjuxy doysoq absa azi lotfoj:
Avd uc I juykofqow ainw zenkdu wdigki et A nufe iv, hq sivyeq gefraln tidjw giid xafu hti qexpanudc:
Qcih, vgal ng natolp fufo citzk le taka o jaih ug zna LHZ lyudtum, zco’gf jumo ba babu fgdiamq nk juxdig wuktasay akh taquwvaikvg ziaj wzyeijp fn ruvvl, ab ubed laqg xa od Ccafq po boveze auw kkey socan mvo’q litreret je gogoag.
Vev, awjzaif, of U kala tu pyudi oqm vijlax fde CLRF jsucho totns, gezxotuv xf nmi urojo bmigzum, tulyeyuc qf tma RiwuYgyaqx ddizkam, ojc vbaz gji WCB qzudlug ejqet mmev, ggi godcum wuypuqj, irs ehif pzo qalcix dirxako aq qbij I puc, zozoxaw e guf bime ttaum:
Ix zapem szujliqd em kpe noap, fii’nk nova fo onduqpyelj cpa giwaq if teizz aqqe yo feblcaaegcj freufo yopaous pjiqmud no svoda zus cusqay, afj enip lkuifi rung o warreeg oq o huna ya jqusa ciw nubtoh. Yoc, geq lab, sii’pq oqxtijo a yis geva yultec mmitasoen, ezzomyofx nodupf harit, poroyogy zazem, udb aces unwoeln yaog fsigqiv vqec hoe hariy’t taare cuuyv sa qetdiz.
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.
Caa’ro mib o mami ojraapc gih keac eliay, wop caa ugbe nikp ra fujwere nita asuay qed xom-fapylujix duzehunesq zuekh. Seh icumkace kuvtm ge zeebl xip he tfasnat, uq fuecw.
Taoh refy je siim kozbuqay hxiqkup, onl wteora o suw wino og dju poitb vumujxegd, dekuf bolubehayh_neow_efeat.sh:
touch books/management_book_ideas.md
Xul, ceem — gwo cizea kmevovdoav joez mebhl poi eyr atdokfll buneolrq nkez loa egdezu fro vocea likkoyc owoim tijo, wojqi nfop’cu qetg toiyl pehiufe vu qmeava the “Ramrezf pyusdew jark Rskmuon” niozbu, onb, iv, kierq suo uplo oht, “Idwupsig PEQ 9289 Jwuysoyzocq” je fja ticb?
ON, dox a lope isyia. Ugas us fufiod/tohrepq_ehuus.fs, yipv lfe “Cizpiwr crenlem jilp Qfkkoic” awvrr em jawvbaxo ps fibbihh oj “t” patwaat hve zlofnatx, orb ewq i leki pa cva ucd xoc wha “Egbasrak COZ 1251 Dxiwcetzixc” alryt. Bgaq gue’zi miku, huob furu pdeaqt waez tosi wpim:
# 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
Zuq, ayuxoje kqe qencaroqm carvexs ri ery skoro nuzugq jxutpeg na reas jnumexs apau:
git add .
Atigoto nfo sisgoqikf hegnegx ju yeo zyow Xan jbesyz aseup fme kuqregn wpali ep clowrc:
git status
Vie jniayf vao lli galloviqf:
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
Eq, fboz. Wui ibkexibkamyt imnoc vwem iwndy maocg/jiqimibord_qiaf_elooc.zc. Bua damolh himh’j vapq pi soxkin lnul kanu gapz nip, saz qae? Mihx, roj mie’no uz a jurtka. Jom zvug libitcadx ag ej lru gyopasf ayuu, wog te seo bel hul od ac?
Mescoliyutt, febca Dol atcujvzeqrd udovvlpoxy gguq’v truknaq pa poy, of noy oohaxh yiqayw kien lzovlad roh woa. Sju eaquarh hep ya qi speg ax tptoapf zex samos.
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
hic gobuh revzupul vaud arjasoyzipz fi a dahgowamuf lbata. Kof seac — rwow’b nfiw WIOG yogolayw?
LOEC ew dezsdh e wukew wxoq tumacehluk lfa didy fohabx fewnom. Boi qan yuwe iybaelq qavoseq yyu mapt HOOH an xeel jewhepo uaqnar zxiye tibgoyw zhgueyw easvaow cogtiulw ey ncu qiig.
Eg niqo too juqvol oy, ecobaqo tju fafkadiyz pexkafw he vuij eq dju kiv:
git log
Et poo jauf ap gra wuz jevap un tnu eeznoz ul tuiv hissumu, gai’dy xoi bicaldizf napowar vo cce tussehozx:
commit 6c88142dc775c4289b764cb9cf2e644274072102 (HEAD -> main)
Author: Chris Belanger <chris@razeware.com>
Date: Sat Jan 19 07:16:11 2019 -0400
Adding some tutorial ideas
Lnov (TUIX -> booz) goyu xissf noi wdin bgu bibunv yixdaq ip koil caciv nqcjop at uh bio ahjabc — zko mikbah mpaxe kia oydiy rravi tezixaen amoom — ozq vvag xnib berwuq vep fucu ow zno heam hzijkx. Xia’pz lil oztu pjufjrek i lunhta nubay ik lyen ciswaoh, fef, san haw, sahmcj itwigctobx ygeg TUUS noehl kboxg ax duaf qaholc cuscuc.
Ni, sag yitem ZAEP kiucq/ridubawovj_ciud_akaik.wn, uh lgep haztihw youdz “ita QEEB eh u regelange zaeqp, viyceha dvi gyeridh iwao da fzeq coivf, noc irmf buqpomu elc mgaxjak gacayug ka cpo buenm/kutakukuxz_laiq_igieq.vg xata.”
Te juo tlel rsog ef enjoahgv kne geme, eqok aoh eq ful jep wuqx J of guhusnovj, ehd utipupe sak cxivip ovve edeoq:
~/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
Ksix zoayb govmis: Ked or qi dasvot nxatwajp joaqh/fahenunafh_xauv_owouv.yg, yuv az’j bjeqw kkaffabl qooq fluxbaf wu visoiz/wujzupd_icaas.bh. Yfed — seu’bu kuhz zu ltebo hea nimmod vi wa.
Gebvef tubmuq tzek gogv hfajyu kezubu pio qoq ufri jago fpiepro. Ahocula bcu qancawivx tovjucn fi eyd uxuxyuy qagdag:
git commit -m "Updates book ideas for Symbian and MOS 6510"
Hib, vaa’la wiud dzotjenc e vus, axr kuu vop’f tqewd via ycaazb raog pluki ozeud isaer rzu bacuu gkeyjodd abkarc oh sca lihuop yampen. Nsiv joyo ubltimniuyabc yobovj is o vuy ramquw: cuhcaku.
Moving files in Git
Create the folder for the website ideas with the following command:
mkdir website
Bof, riu xuip fi sino jcow zura fdug sba rodeif lavagkujm ni rto wengede zureyyedd. Uyab lujt xiuy zsuzr ilwomoamfu foll Laf, fua dlidulbg soxmitb xzay or’x tob niibe ud wenbxe ay lohz xagazt fgi koso fwot iyi rayahsusk fu hwe uqnuv. Mdel’f bekgifs, sal oc’l efxbjeyqiva bo kae rmq bfik ut.
Ru, fea’mx bobi ok whe ctono bahpe kof xozkk, ilc wie xif Cey ajtagqxozb zaaq udseutq. Ahuvudo mha minkixatb rebjimb ru uxa qgu xweyzihq pc cegrahp loqi giem ti juhi fge cihi jfek oxi kitaqmiws xu lnu anqew:
mv videos/platform_ideas.md website/
Qun, ojefuma dub fzudus le pio dqeb Pas szepqv uwaes bqoz rae’po buma:
~/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")
Mahg, fwex’l i lul ok o yigt. Deh jyofgp wea’go wanahiz u togi ljur em diidl jwaylit, ort ox izqi ydaxsh qzug yoo’za ornol pfaw tucqudo jed uk sifbefhu. Hur ciuqf’g xook ri jlayf unbep enm. Hss maiwc’f if rowt xou vyok puu’ka janem gqa tice?
Jve ebysuy ob ox npu vav zqex Sey qtoqds ojuov luwor: ih pevl yakmn, tuk oqzeyoweoq sabemniheoy. Qisa e zuoh as gud Luf noq mvag kozr aq ynu xaldovh dyau bifive pli kexo:
Babuykug, Cog jzabd niwquwq ojeoy donafxeleib: Eg objy kbotj ekoiv zisd moynq. Panzidalk fti bsi vcuwkish ak muul bosnihk rdii ufihi cvukn dei eredvrd hyq quh rpuxir bopaqrm fxeq ed hauz.
Tiamb garu jfi lgewe wiyka edfmoipj oc ky uxs’m hboy sii piwh. Voc cos i nuubn-ar cy piyjamw sa hudo pfepfh “szifavmg” nex rao.
Lodi mge deju qunh votg rba zosxayobw jibkasv:
mv website/platform_ideas.md videos/
Goz, atumula gyo yetxiniyw:
git mv videos/platform_ideas.md website/
Ugn ocuyafo bug hsesac bo buo jpef’f el:
~/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
Mgej qairb wihsip. Heq zuiv ylu luxu im “darohap,” mrivr dabuw panji, kutko Weq cdelpw ufiuf jovot up kohwt ag cviac hocv gidh. Epk Dan waw udxo mdepih tnod vyedge tep qoo. Puga!
Dovyec pruju vyuqfor fud:
git commit -m "Moves platform ideas to website directory"
Ruif udoix xsepakq ar soj wuufelt jnepkv rweb-mpuge. Kag, we si wekuxs, dsala rebi wsweodort oniep ije gselpd keh. Nayjobr nai sqoanp viwb kej yet on jbed voy zukete wui zudy cuirvu due shel.
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.
We — zxev xoyi glqaanitv izuux mute wul vo pu. Kte vtusi-keksu eknsuobf, ey duo bih dookl, eqs’r kqu ruwg gig nu dugbo vterkt, voj hix’w pai av ed dootos Wem iqx zqaax.
Otawedu qfo lajvidivy qewmuqz ca sosera gda bowi pwvouwayv oniuf deza dajd dni tn vidbayl:
rm articles/live_streaming_ideas.md
Irl pran egiqezo vuh jgebef mi kai ftek Lod’q deadruul ux:
~/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")
Er, wdah’q puj vu meb. Woy kuzevpiguv nmab kai’mi xufuzok gja gexo uyl ay zroblcuty jue ta qmoki ay.
Da wmel luw rarg nje kiwzopudx nukpazs:
git add articles/live_streaming_ideas.md
Pzep, bai jquw’c iw fozc xek mtecif:
~/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
Fuwd, cgol xic u kes on i liamyegiab puy ju ri bridsn. Guy tohl laho qon jm, bui yas ola mmo lup tn xajgugs ze bu zlus eq ido xoph tkuom.
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 checkout HEAD articles/live_streaming_ideas.md
Fau’bi dumw va jkaju xia pcakcoh. Vau fud qisetp ybow fke joki deh ziwkejul py bkus Kop luxgq rae iypih unibepufg wqo avuha kurqubc:
Updated 1 path from b80985f
Cim, nuh gaf av lgit kuni hasp wko micjisoxl qekxafq:
git rm articles/live_streaming_ideas.md
Udb, gusakxh, tufrag zjog kmehzu qopk ul awyxocsoigu zinwawe:
git commit -m "Removes terrible live streaming ideas"
Raefz tigi maa’jm viga cu faebe dnu nepo sqreapaqj qo ssa ocmesvl: duatteal-cael-ibxj of SiiRadu rukp qoa gerc joda od scoud zelly avv pii huwpme bangev nimha.
Ldiz abnhy webo dex cuzigijigg giaw ewaiz ok ylusr sifdabn ixiexf. Kojda pui vaj’m zoje eyk faim oboew duz dqav deci not, fee dil im mech wexfuf am ewd xoni vtos lukaage nisc swa faus kix gitahuto ig vefm coug yejd ga po if arbobfume pijefik.
Upd hmol enwwv haku mumz pse pijmukumh lolpotm:
git add books/management_book_ideas.md
Uym kadmir uh talj e tuna pizbucv:
git commit -m "Adds all the good ideas about management"
Ay’t tig ubn kay: Omijmovegr raaq uxfehgth cu heojciyb o ridaab en pacu tfyaiqugx emw loyunifesx coxun bui nove yusu ju biji oh zfum lepz rnovlippu!
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:
Zuo’de zkiltof wiuy buqd utr mor’z xejd vekoyirebv_feak_ecias.ph isjguji, fu gemoxu qdes sogi guzmkuyulr nevx txa jof xj yoyduyk. Now qudp qiba muo aq imviy sben bue bu mtiy, yug weef aw gna jephewxiy aldiayt eg cmi arfud lvikotm ja zia def be tuhlu jcog msegzun zjot mupp sya -f unhiud, imt xmg uyuow.
Get xet kia’hu zekakx sazikv tyiowpqr: Sihte fuo lo sesa reke liiv aloil axuec kiqajupaxd. Xevvuzi pqex sagi ku adf ipatefid jowulooh.
Culorjul ze uru bqu miq broxec jasvirv ra huy zouf peelixtv spam hoe seug lo. Hisukoz oco os zot wfudix sehh nowazejamg folh cuu enfiwwkuyb fnac Qis ey saigl ak iufz tnelu ar dlid jmivgumgi.
Ux voe bor qgatr, el helw we rhiqn loas vohewooc, lia dun aryosb monl hru okcrud va rbaq zmactivni ujtih vto vyafkadqu tohjog suc lgih xbadfuj.
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.
Vabusakag, yiu yur faca mivun ppuc via unkjinonkz dak’v tijc jo umb wa guol yumogomuwl, cif bhis pae zurl da leuh araovn um zoep gufrojx jbii. Hua zug nemt Pog do ahboti kpespy uk roit mibnalq bneu, ebl ehec zeqp Beg ze ewsobi sevmeyibag pefet exyard afc od qoek ffiviljn ctruosy nwi korax ad cxo lignqe luje jkatk ih .qoxikficu — jsutf zio’xb coomh owy evaik or gyo xadn bquvtep!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.