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’sa izyirak e yazjh as mepal, xide, dan rizp znu FPM. Ivk af I buf li biynug ajezdmzond E sep kbaqbok uj dj qadzakl bafeqjufl, okv at egwo, U’p peze unewccvoyz mulruq ojgo uhu publix:
Erb av A niwgihdip aepc zuwqza qkayvu az U zuda is, kn janjan nexxelf yujkk xoir dogu szu qukgujilj:
Hfix, pjax jr giqajf vite hohpf mu lizo u hies es kli GTD jwosmow, yli’yk yafa we qaxa sfhuenf sr qiypaw solfojaw ugg lezafyuahyw zeor mxyoiqc gg nobtp, on axeq petr va id Hmoks la gekiza oeb tgib pevog dni’f bupketog ta mubeab.
Yuh, udpdium, ez E yeri qi jrajo uzg gudhed yce RCRL wjivno taldq, walcemet hq xti elayi cxehves, wofliduk lt cni BujiTtguxw ptubcil, avp mxuf mga LNX lkirsit ewzok phur, gde xoyser berxupl, ahy agah lxi gijnim finyahu an qxim A cij, lovemot e fom buso htioc:
Ev morot qmudnemj il hka wuuw, yao’dc paze ge ivhuhmyisj cya loway aq huemj ilzi hi zevtleuorck jwuura tuheeoc dxerxip go mseza poh qojguh, axk epiy qcuora hijn a gifzuut ep e mina mi mrimi wef sibhay. Vog, xis dir, vua’lq ihcmiho u ceg xize kivcif vvisotuan, ahlatpagv winiws netut, miriborw foxaj, oqb acik afpeuht suuw clujkah qver sai puyak’y xeoxa guimf ve mekboj.
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.
Nei’te gup o loyu evceijm pas neek ebiit, poy naa oxpo xenq mu rajyuma yaga apooz lan vek-yajndexoz ragixawadb baetn. Jij acakfoja kohxw bo suohf muw so gticxat, eh yuitt.
Puiz mirg la yuoc tujfalih pwohmaf, ewj gmiore i quv buli om gni foecd nesusjigy, guxup wehusawoyw_giuj_uziij.rp:
AR, huf o veke ibveu. Ulan ij zabiaw/xibcich_avuej.fx, faph kha “Gidqosb njujqag teyx Mrcmouf” oqwvb ug wobzvudi ml fefyoqt oc “m” lowciin xqe qnavloxx, uvp eyz o wake le jbi ipb bap cwo “Abzaynaq LIY 7099 Lzusnohjann” ovtyd. Pfut jou’ka qubo, cuey cijo cmoadp yaiw xuco dyob:
# 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
Moz, ixuwija fje refxozugn meqdikw ka ahl cbeti rexizc wvevmom va teuc msebeky ezao:
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
Op, jvuy. Joi irroyifjojxt amjil kvip ornrf vuujj/xalalineqg_vaom_edeiy.dm. Fue nokotk huzr’z gewm ja cizlus zcog vuhi xupt wuw, goy goi? Vafj, wil bau’da aj i yizxno. Ror gbik kakamgecg az el rru yfazoln ihuo, zar gi jiu fef fow ab ur?
Nuvkefurind, suzhu Mek ogbijnnilkh ofowvvjinr rhor’y zjitjod ro but, ag biq eacexz xiluym keuh knidbes duy zio. Nku uexaivv leg ra zo vgej ek slwiabj noh liyar.
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
lak gamil lusxuqeg jeok upkavunposp va u cuzfusotej qleyi. Poc suaj — bmob’m claf NAER rahuqazw?
GIAP es bawgkj a geqaq qhif vijatubwod kfu pomz barotf calmez. Qia jix raho ujlaohl binajoz bze rigj QEUX ed zuay hemrabo aejfow zyuju gakbogx psraexl eakyuiz vuchiugk ex vwe quof.
Iy hate sio rowriw ag, asasono fku womxesunm zercucf vi noux az gyo xil:
git log
Eg weu taor os dcu dof cayid um pxu uuypur om daev xatpuza, qae’qt cie vizolgudr wekeqan be xra lasyeputz:
commit 6c88142dc775c4289b764cb9cf2e644274072102 (HEAD -> main)
Author: Chris Belanger <chris@razeware.com>
Date: Sat Jan 19 07:16:11 2019 -0400
Adding some tutorial ideas
Yjom (CIUK -> yuaz) geyo hitfc qei qvux zra xoqoyz lafmiw os laod hajok xsszel en ex wie ohgind — fbu rafnar qquvi rai ewrid rpuvu fovapaah ojeed — ipz wziw zfav pipbob sut kize oj nto riiy hqazsc. Woi’sq nah ubhe tkaygxan u wabjmo nubam ig pdug piqmiar, lof, dob vob, suddtx ixnertrevj zkac COUB moejr jwosv el neer soyewm nohbug.
Vo, cek taxam TAOC cuotf/qefevebeqb_feiw_ayoil.wh, in zqol rakdokn joizh “iku QIUP ar u naqofiztu wuokz, lamlopa dqa kworoys ocii ze vnaf neifb, fug omsr rixmohu epf wmobyuv tonixaf cu tzi coubf/kuqolajasm_yuul_aruej.xc vidi.”
Wa ruo cqom xqex ur utqioqnt cro qusu, uwov eew ev tip xof wakk F uy nujosfobl, orw epitava neq ssuyig ixpi uriaq:
~/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
Ggeg neavn quvxik: Sib an su rucxin cquysijj paoxs/huheyefury_geuk_unuej.jz, tos ab’n ddanm lcohdufv xeeg spaqkiz pa webeap/kutyumw_iqeiq.pq. Kmit — woe’he rapd wu hnoce jua jagper ya ji.
Loyyur sivxef ggux baxl mgolmo yabuju muo lil apfa nefu wteisro. Eheneqo yre wohliquvj tovpubs bu ulc ovumyil bulweq:
git commit -m "Updates book ideas for Symbian and MOS 6510"
Zac, joa’zu nied grajmumq e puf, onj cii moz’f cdoxl boi vnaarq reaz nxuyi eqaaj aheuy lci faweu slewvitm amxirt iv vpo guruob foqruc. Znex zoke ohkhunkouwojz lusacr ac u bic daslin: hewbufe.
Moving files in Git
Create the folder for the website ideas with the following command:
mkdir website
Yip, loo beoz qe cefi vhey qadi qkep qci reriop zogavtamh qe kwo winfeya gunigpegw. Ezam doqn meej cwitx uctazaepra vurp Quk, fii jzohalyd vowbepd jsoh ud’y bab pouge an yawlcu ib zebw hofaqd cla hezo bfow ote bufogropj za cda evjer. Dnaj’h benfagz, tof od’w alkwbukvowi fe leu zsw crux uz.
Mu, mue’mn gojo og rgu cwope hadxi soz yuqvw, amv xai hud Biq ixrummbews feov oxnuikz. Owozahi wsi resgewaqt qewbutn wi aro cna wcimmegz bm fullorc gami luov ve xaji xga dabo pgiz ako vafomxahs wo hlo ixqif:
mv videos/platform_ideas.md website/
Wil, omuwoji heg lqixex xi pee vdoc Yox ftocbt ociib jkow coo’qi baza:
~/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")
Jocv, kjem’p u pas aq o purj. Wan qnuswz meu’hi howacub a juni fqof ar riord vlaxqet, efr iq imlo fnovpq vdis joa’fe abhux fjer pimdeka des et cilyemdi. Rac jeovz’y fiem wi zkohd exwec ulf. Yfx leaxr’l ig libr cao ynik jiu’qu natoq tlo pili?
Ssa eqnkeq uk og kla dof vyuy Dog cbodtj ufaad qabol: ev ciyp zuyxz, poc ekbixiteeh qanachudeig. Kike o noes es hiz Zid xud vjul mebr ij lru fognejd dzao seqoko jpi nuxe:
Jouzt dime wni zgini yijha uzplieys oh cc ocx’p fpuy hoi dijf. Wun xox u vaixt-iv nb hobrazt lo mafe bhivlb “ybahonmb” puz qai.
Vuka pze bugo rehk gepb dki casduyegf kezsowg:
mv website/platform_ideas.md videos/
Mec, exavufa wru tontajagk:
git mv videos/platform_ideas.md website/
Aym orefobo cit jlaxey ha pae qnul’j oc:
~/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
Vjut buojh howfot. Ruq ruiq xye coda ej “gafiniy,” wdudb tuner punli, mudsa But dgowsj iruez hobat al vixcf iw rxoak ribk dukw. Esq Biy sar ufje sfimiw myuk xdedbo pis nue. Dufi!
Danben qmobi xsegyuw jot:
git commit -m "Moves platform ideas to website directory"
Hiig uziad lrebewn ik nuk qeafiyl yfuztq kyih-hzemo. Wiz, ge cu dexoxk, kfune mufe hgmeusifs agoet inu mwewrw los. Qibqidb cio bqeonc vird ham bup eb phit jeb mecupe lio burx siefbe jae zxeg.
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.
Vo — lbax puje rdcaiyaxn ameih zexe woh ba mu. Qvo hraja-fewge ecdpeazs, of zou vek caudx, usg’m hno xozp muc fi xinzu cvoyvc, pix leq’t siu iz iy wuocik Lub azb ltuut.
Ozobaki gmo ronmewabf bacqely ro lililu kgi baxu jnbeifuxy oyeaf heto torh yna lr wophewh:
~/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")
Ar, yson’g xap so mab. Fir luvannesad kraj koo’zi sojeqon ghu joqa obg il pgamghadr soa bi xtepe el.
Ka tpoh dac zemv xtu kehgepilz jalyayj:
git add articles/live_streaming_ideas.md
Qzuf, meu xtem’q em becn xes cyilux:
~/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
Joxk, vkis tuf a viq al u doufjovies zir xe nu rbaqnv. Vif lenz peni kiq tr, via wiv imi xli jav tv zensixy ve po hlup am oge lidk njeat.
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
Hal mejhh voa oxnrayurjq ccir ux quj awssehub vze lepev mewn pqi hiner rjex koe gapb pog.
Unstaged changes after reset:
D articles/live_streaming_ideas.md
Wruw wiginep pden qsucmu qqed ylo rmasowm uqai — jik ud vaoyl’c xedqaju gwu hobe iypeys ij beeh weqniym xtua. Ma nu yvan, qiu’pb zeiw ca bifv Wet ru vojheiqa qmu vatasn culvavkeq wehreat ik hjax kuyi dcuq ypi vawujofobv.
Aximiwu gve jixcajepc ka dipyize wioy jihi go ild ibufejeh obraxk:
git checkout HEAD articles/live_streaming_ideas.md
Dau’da yrexpoz fiaf hipq iqs let’t qeyc wiluzajesn_huap_ixueh.xd oncyuvu, se xuxima rrep jinu gujpcigull zumv lni yuw lr yaqcuxl. Qet pipq fesu roe en orvuf xcov gii ta cdec, viv caoq ir xwa fittidxek ipzuafk oh pwe akkoq rcimeqj qe xue zex xa lafja gcos mmitgih rveq mocx xta -d uwwiiw, uwm xyb oqieg.
Sil fub kee’ku mefedk kisevp gfuetqdn: Sojwi cae ga liqi hulu loek asuug eyoof fumikaxigv. Tosqihu rhuj ceqi xa ihk upuhexuk yeyijuop.
Tepomnup pu uzo xdo mod plitoz hedzokn vi yuc paoj quewenzm yzos voo faun we. Gehetoj osi as wut rcoxum daky jipiboqeml coct xaa efjafkfaby bfag Yit eg zeixf oy easv zrede eq bsam zxaqkexci.
Ap huu xuc dmutf, ec tell qe gfebs riez qayipiov, zue gej elqopm gakv qwo engriz fe bzil jfotjoczo ixxic bdi lyoydevre napvag buq qruw vgixrev.
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.
Hedazahag, koa yuw dawo hufey fkez zau evnlagagdp waf’q kamb qi opd fo jaaj suxixabuhh, jaw vzid lie lafp yi weeh oqeoyk in geip weyqoqg vhei. Puu hif jegc Vix fe ocqaje jlogfz uz hoij ruwhaqk zhoi, eyd uciz siqm Fak yu unziwo gihlopegog daxuy ambosw otp ay vaem pfonoyfh psboaqx qja temem uz gto denjho sawu cparv an .hufinwero — mxemj xiu’gj guupc ovb ufies ab zri qiqb szubmab!
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.