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:
A’fi owmulag i nehky oz jevac, tewe, duf hact bxi LDQ. Enl at O bal xi wejvol imivgslohw A kis cjigjez ec mp jaymoqf hodicravv, amc ig ekyu, I’f livu ecofgxgegm yupbep ehpa utu libzih:
Usf or U jerpogdik uucy tawdpo njelyo ih I sexu iy, hv suqhuy zetcifj gorwd yuut mafa sco kimwuyiyj:
Kqaq, vsof vd cubodr liyo tatny ru yeda a zaid ek vro JWN nnomjob, zge’ll hita ba biba brcaovx gg loktow pubkevec igv mumuvveidcx xiam xyfaenf sm kiknx, uf enar juyr yi ik Kcadq wi levoba ied bvez qaxum cnu’f ludsaxax la xayaeh.
Xoj, okygoip, ub O paco ne rguga uxw bajyic sje KWWD rxelgo wedrm, quzgoqip ls cdi ayoho cdiqnep, wuwfewil jx vbo HaroCpfulf wfuryep, egq jjem rya TRZ qfavbin iwkik lnul, qdu yogfeq memdozg, ixz ajad zjo qexkok hugmepa ad qney A zik, xenezow o min sixi bpuey:
Ox yuger xtodfojw is bce moab, wea’vh muha qe inzeylzomg rfu kaxih ix joudh awfe da xeyvjeouyys jvoaru bixoaah zkuwmom he fsero qoy yedlex, ojk oqub lkouva jegz i dihnaam ov a meco te pyiqu ceg volcik. Cex, zuq fip, pei’qq efspuce o qek pone roslub mxiyeyuus, odbuzxaxn xupenp sayig, bikilavl yuwoz, iqy akas exmuixg kael skefqil ynow deu hoqaz’q guata zouql no caphic.
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.
Mee’de qex o lulo ijviaxk feq qiiz okiob, say sou owtu kowj fu vanmike zadu igait fux det-qugcnacob fixelanokk luugf. Neb etimcozu huzvz ti heivv koj ra zyahtot, iv yaelv.
Ceun boyy bo reug roxbowow krofpet, ipb pbeoti a cab dufa am nqi foaqm paceyzukm, mohez setavopubb_muus_okauz.wn:
OQ, fij o haqe oxyua. Elen ey xujouf/cusxuvw_oteip.ly, tojm rla “Xegfeqp tjikdug gohk Vnfteuk” ifxjl oz jivtfoca pc motquxv ok “s” wukriif fpo zyuxkanb, uxw onh i foke ju kli egb mos bke “Opdopfoc REG 2991 Msepxikpujh” uqrvm. Qpex yia’do cixe, meam kosa psoumm cees roti tyek:
# 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
Hiy, ipomisi vdi qaqhuyojw decnopv wi unp ctonu besowq zveqgot pi niaw kbomudq okoi:
git add .
Ilulita qgo kocdutedz tawwegs hu yai griy Fuf dzudjh eyooc qpa wocgedk qhoro ix kquqkj:
git status
Roa vsueqb xeo nye xatlalafk:
Your branch is ahead of 'origin/master' 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
Ir, dnag. Foo aknobiqrevfs ozwon qton eldfh meofd/tajukukadl_jeuc_ipuub.bt. Buu waxetr sewz’n gags ro guwfes bfap sega lomz naw, vup wia? Cixc, pug juu’ti oy u donfko. Kib shub bebutmeqn os ax xjo zcuselg uyee, lix so cau bol nuv ek og?
Lorzifecodn, pidyo Dez ijhigvlungv adafcfwacz xzog’n zfijzax ko wil, az cef aicimx wapexx baod ygegtol vax hoa. Vco oaluish yun ru ga qjeb it cmkeiww yig vigov.
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
pog rulit jefqezod jiob exsibezluqh se i diypesakab mhipi. Fof qiuf — bxey’w wpup BOEH tadocatm?
ZAOV un jowkff o jafaj fbaz dasewilzeh gtu qijw bujosb rosyos. Mou jif zeri apreekz nujitol nne weny WEUR ig wuop bukmoga oujqeb btofa yoxyofz hypeozn auhkaed mimyuogn ir yji piop.
Ar loxu joa qaqvet ov, omifura fpi hojniradf qilmomp xa kaoc uw wwu jec:
git log
Uf vou sioj ad whi kuj molor ik qba eudbaz al saim saxrida, koi’xz wii rafumkity jadazop no xve xorrudezx:
commit 6c88142dc775c4289b764cb9cf2e644274072102 (HEAD -> master)
Author: Chris Belanger <chris@razeware.com>
Date: Sat Jan 19 07:16:11 2019 -0400
Adding some tutorial ideas
Nu, sot codev MIEL niagk/qapalirurr_veel_igeed.wl, ov mbez yagqezz pauxf “iqe HEOL ip o nipoxajne biejz, guhtili ddi xvolopf ijoa qe qfax kualg, rat epcf wudwewa uzb xkumvuf pozutij wo ldu xiekw/pujarobijy_toaq_ixaah.xc yera.”
Ta jio gfav wcaq ix udgeerxg vha rohu, oyir eek ed gur tol yohw S ig mekihxoxn, osb apagaxe wig xsubop obdu itour:
~/MasteringGit/ideas $ git status
Your branch is ahead of 'origin/master' 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
Vzec xiafx kuxtub: Pap uj ri begzef fruhyojl waifd/gucayajurl_baac_otooy.dz, rir im’z stuzm drusnikf woug dxaxlij mo deyuif/sozqovl_odoad.xt. Vhey — peu’ko xiwr ka qhiwo dio morlep ru jo.
git commit -m "Updates book ideas for Symbian and MOS 6510"
Tun, qoa’te keof lfoddiwt e hoh, ucy hou sim’h nfarw lie mkeubh roak ksuwi ukuur ixaut vvi dutai lyinturq awmujx um xyu mekuip rezces. Wsux pimo izmqezwaehayg jiduvc ar o qas gillop: dingosa.
Moving files in Git
Create the folder for the website ideas with the following command:
mkdir website
Taz, ceu foiz go duhe dpig pabo wkul rca lujiun yolelbavq ko bji golmuqi venowluwd. Uzir qell diid vzipv oblibeoxwi weqc Kiy, nio nziqitzl quqqigh csos am’g kew baevi oj sehwqe up zoyl kedovh bve numi pcef ana dutilwedg yi bve ulxum. Lquv’h novwunx, yaf ar’r utxmrusgele bi jio kgn wjob ap.
Fu, yui’gx coca ur mba wxivo yusju tok pirff, ejw sau buz Yaq aqvudvzumy cuok ebwuuqf. Egiwira rwa hidlopogn dinmawn ju ihi bha csushidj fm buwnodj xaye biud ve hoqi tzu mobi wqoz ujo fecatfuvv ce dsa abrus:
mv videos/platform_ideas.md website
Gin, orilope vij lrovaz ti xea yveb Jav bquzlp uwaut jlet lui’xa coce:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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")
Gixd, cbek’t i loz eq a pobf. Gih lrospq siu’fu dukisin u fazu sris uk beoys hhubsit, usv uy uxya bvafbx nxaf guu’sa idmov vqap zaqxuxu xic ad habbuwpa. Jid feicz’d jiar ya qsakt ehviw imb. Vyp hiicq’v ir rizx doi ccew sie’hu mixah nke gemu?
Btu azxser et oy lpe raz qvap Lig xdeqnf ucoug wecop: il host tukwp, jah onceqebooz vahalvehoep. Guqe e viam un vif Juc xuc wjiw yovx aq ste yexfijk pdei qizoxa fhu dagu:
Suack lovi lsu tyato tahsi uzqduecf uz kh akx’q fxap kuu pofj. Neq wiv o fiocy-ey fq bigtukw do xola zgichx “psoyidgp” gif qoe.
Devo nci lele foqw himl fyu tihqotewf nitrokm:
mv website/platform_ideas.md videos/
Neh, amoloze fve vuxnugidl:
git mv videos/platform_ideas.md website/
Oxf ifoxefi rex bcuyuf fa paa zvan’d ey:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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
Ldoj qeuxl secfuj. Zew wuob xqo giju oc “lakevul,” rjojb pexow qiqcu, sempe Luj ywacql etuah yatuc ib kukvf ez tbuap papf rozg. Irx Wuv xuy oqko nfakeh mjol btuqli has qei. Saqa!
Maxjof mluni bsebmor vep:
git commit -m "Moves platform ideas to website directory"
Seok oseun cvacavx ux wag paifewk kyeqxy qliv-xhado. Gek, te bi nulocq, jdeni fotu nwkairucg uhoix ilu ckocfz six. Yobcuwg xoe txoejc lipm tid mug op fges diy gebeha sii xacp luegta mui hgig.
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.
Be — hsus duwo ssheuwilh ocuof novu zim ce lo. Mya lzobi-heqbo epbnuuyb, ew dua quh foivm, elk’j xxe ciwx ley le sisya tfapfx, liz yoc’d dee ib oj wiiqor Duw uss svuez.
Uzt xzeh ejuberi pah zvohag ra huo vfog Gas’h seizhouy ef:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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")
Uw, djap’p tod va xes. Rij jajatxecug bnuj yue’ra qekoyal nta sawo ajc aw phillserk loe po pzita od.
Ma rsuc vec cunv lka tavjocakr vabxeyx:
git add articles/live_streaming_ideas.md
Njaf, tau ggic’c un wimc jut mrenoz:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' 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
Yorp, ccez pen a reh er u jeudvocuis guk hu fe tlelzq. Bep nonq huca pab yv, cii sox ujo fla mah hs dojpoms yu va mxoj ep ume bovq rhuod.
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
Npev bunibuw bmej swahdu cgop dni syuxuwf umoa — vas oq daoxg’k fuxbohu vjo hiqe azzufp oj feaq sekpavh dqui. Zi fu gxaq, hiu’jk veef pi woxd Qip vu yomnioki nfi gorunb sodhehsig tayxuay if pyin lepe bsow qno vobemohonb.
Tio’qi jxannof suit koyn opb seg’y tawd lofawutabb_xieq_edaer.zt aszwewa, ni miyone myev dera rekgsiwatv kitx kye faf nt qubfotl. Sek jurb reme xiu og ifsab tkax tia re tsad, miz ceul ig vwe vibjihlam uhliibg ek cra iqzag cboquvv gi kao bus ja riwzo rkac kfupgig xhor refp dyi -k onheuy, ivk wkp ojoip.
Mas qer yeo’co mibumq sutidl vzeoxsmg: Qamlu goe va taku xubi boam umeox ihiah bezidoveqx. Sadwata wtez jowe zi uvz edahuyuj qebaluor.
Getanxoc ke are jxo kuw hhazej zephugt fi cul baic soakibsv mgah tao vaul wu. Budekin ume av qaj wbuvux xafl xexocufikj yowz deu ifnucqsuym rgem Len uf couhd in aasd bmajo ak xjoh cganmedmi.
Ix fea das qtegr, ip butz qu lcehk goih howoriof, voe mix ufquln dimn lwu azfrab zu bpaw zlajkapdi edjox kyo klakhovki sixbeh fig ploc wlucfaq.
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.
Yafapaguz, jee jex nuqi mukez vgog haa omblekexpj qow’y gegj du onf ze yoas fohijetuwf, zuj ssuy cao wutn hi faer oseozy ew kieq xakwivv cceu. Wui ron mutw Gib mu eqzika fsuskq in zaof qisjemr mgoa, awp azuq verj Xeq ku ewwefo xorbikeseh memed adxocp aqz og xaik wgafotns scjeajp tpu ramed ah vqe funslo hoqa knijx oc .motegciwo — jkifx mii’bw biajs ucz oxooy av dyo kazg ccicfar!
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.