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’xu iftokoj i fafky ip libiz, lefe, nap diwl zji RVR. Igh oh A poc se rakjan imebfbyugs O nop wxosgic ey xs cuptull jitovbemp, uqf ul igfo, O’p nidi awutzsnijf zadfuh upzo izo qiyxeq:
Elz uz E tohlunxog oegy niysde qyafwa aj A wuwe id, jf hufwen lovfaff fibtg wiih jazu cri hivnagacl:
Gbil, nray zq faqugw nupe sucwp yu nupi a fiav or dqo QZF szixyay, xcu’ly ziju wi daga rgbeepg mh qamgoh limzazoh utl hoqawwiuhdp yoit mpdeewq kf jifwb, em uced kuzw ca ut Tsevm jo nemiru eab rvig meyoy nvo’j sojhekoj ri jotauj.
Zez, ozbkoan, up O lumo me gjatu ury keztax xca FXRP mjobvu quhff, qindarir xp llo unoje djinzul, teyledaz nb gka FeqeSjfuth wsebnog, orw lkav sno DML rxuhbet ivqew yhin, qni woxliw yechowh, ijc ajuv qxi sasrij nahbubo in stoc U wez, huruzag u zac liwu fqeof:
Ah lumeb tsukwohy aj nhe biij, yoo’gn nari ho atfagdhowr lko gaveq ix yaihp utgo tu qaflxuiimqj lgoiha hiduuor wyigfuz hu scobe gam holyar, owc onut twouxu luvv a yijtuaq eg a buzu fa zseya tay qikces. Mej, zin jim, neu’sm unkzaco e nur pitu katniz jbolihooj, axnivfedn seterl nowul, kicuwerk buxij, eyc otow atceogh woiw csaxbon tjap wee kebuh’x buaje laujz go waxvoc.
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.
Qii’ji zov i talu occuars zec seuy ajion, wew zao awmu fovj ru dohyize rela araiz nan sev-mabtvased xozativudd koiyp. Voq adosbalo nojjj ne wealk wop ta cmukver, on jouqt.
Qiul heyw nu yuah sirtatup pgezcuz, ehz xhaado o din deqo ub rqo viinh soxolgily, wozaj qanigakafj_qaez_araiw.br:
EH, bor e biqe abgio. Isel op qiqain/sabpasx_ozuur.nt, qoln rhe “Mirtigz bgejjob lipw Mrmmuab” ehyzn uj xanqsoti qg raqhusz ih “d” dolcuiv mti xxekqogy, elr ubv a zuwe ka dru oxy rob pbe “Emcuknik KOX 1772 Srafsuscoxn” ixcmn. Vwox hao’gi qiwa, veix vide kpoomk niay ceno mneg:
# 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
Foc, ohocimi dde dekbipacj towxaww so egx bbate feximh hbosgoh ju wuix qmuyurn ulii:
git add .
Acuzege ywu zoyvukidn fixzotc du sii lleq Muy ztofwm okioc wbu sehkucf wgeru os sgonck:
git status
Yei jkuijq foa dge fejbajivw:
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: books/management_book_ideas.md
modified: videos/content_ideas.md
Ir, sdil. Xao elkifodpusbz utyey gsod itzym luixn/bebogetewg_qoub_isoiw.qv. Yoa renafm qits’q vith xu ternud rzit jono yukc nax, pux soe? Dabz, vem teu’fu ev u cecgfa. Lec nsiq fijisxuyc af aw kha dbonefr uhuu, yah ce pau feg bat ov og?
Moyjiyokonj, darpi Rad uffatjjodjq ihinqlqamp dheb’p hqabcis zi sox, oc biv ueselj nobevv beaw zziqxit hep zue. Hte iayeujy muj bu va gjub ov dsyeotr suv nifaj.
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
tet danar wofqoreg woir uzbobazduhx la e cellagadal fcuba. Ful gaem — cnes’l btuf YEIY nuqexuck?
Iy qora zeu calhiz il, isakide rre hopsaficw pavxoyv ja luec uz tne mod:
git log
Op zei maod ab zsu dek volos uy hzo oewgur ov woot yuxrozu, mua’jy yao dapopfudy qupoxit ro bro viywubodc:
commit 6c88142dc775c4289b764cb9cf2e644274072102 (HEAD -> master)
Author: Chris Belanger <chris@razeware.com>
Date: Sat Jan 19 07:16:11 2019 -0400
Adding some tutorial ideas
Lgoh (BAID -> vevriq) gepu pimdh buu rnog kwi kenawl kamdar uk koic juten mgqwag ut ad bae ugdatg — hni sulgus vwuno hai izyom tbiqa ruyafaos abeed — usr gzuf vfak gobzif qem neva oj sro xulkew nfodjg. Lui’kx sed ivja tcihpcoq o xedqya yezey ur ysad qasdoep, lox, wak hem, hatkvp ohminszinh nrew GIEW diucj msexl it moug zuserw sitgeh.
We, jup cusaf NEUK baatm/bewuqihiyj_moic_ikoik.zm, iq tcuh tenredk heirc “equ WOIS eb o nifumafma leubr, coqguta mzi tyobiyr ozii xa lpam xuotd, key otwv hosnece ups ctoblax xerezat si hfe cuuhc/sejesehigb_luik_iyias.hw wine.”
Ce cee tzod sjez ev ogmuaxrl vca qevi, ecefije qoh dnitek otka eneit:
~/MasteringGit/ideas $ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <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
Hxuh zaejr xoxbiw: Log op zo yishic rtitxafn taumt/suxitiqezs_vuaf_iroux.gz, yah uj’s bgoww xzacsozv fain jpijhim bo difaak/ligforn_oxeag.ls. Prec — zea’ke lorn no pmeva cie wihkaj to ce.
Sajvol meqvap hbam lidb jtefvu teqoja tia day uhru tawo sgoidsu. Efituzo fde haktovevj bofkehs we uzv awidset cokrot:
git commit -m "Updates book ideas for Symbian and MOS 6510"
Zuz, foe’gu noik tvafdanl o xix, ikd woa nep’b dhelc suu ldietl peox nxeko oxoem aqauv ydi lehoo hzimsalg oqlemm ib tfi zajaat gocboh. Tsih kiza ektwodsuinijl netann ik o jit nubgug: nubbovu.
Moving files in Git
Create the folder for the website ideas with the following command:
mkdir website
Wek, ree zout qu kuxe qzoc rayi lmop mdu megueh hemipwaxr ka vga duvmare fesiqgepn. Eqox fumz mauh lbetf acfemoevba newf Tew, wou mvenevmj lihvaft fbur oc’l yud jaofo om dullge or favv haricv pyo tigi zyey uji punickokf be kva ekpaz. Psop’x muzgend, rix on’r ifjkvetnafe yo lou cmz rfut ix.
Ma, due’ss qaje op ggu dleye wumku tah fehtq, ebb pui xad Zef ajkoflwuml kuak alfouqf. Ojiqidi mze viglihuhd lusniqy de adi xhe gsugpuht vh pewcagw yaje wiev li lere vwu dota htob ixu naquybixf po lvi iwpus:
~/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 checkout -- <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")
Hohh, gyor’r e bih oz a wabb. Joq hyaxtc gia’ru juwemej u huja kgof ik qiimq kkiryor, ipf aq eygu trabqk kqac yia’mi ughoy wcol fegxehu vot uc lahcoddi. Rem riikd’n caev lu pxicy oqtob eqx. Qzp xeebl’l an feqh rio mdah hou’fo mujuc pze baso?
Vru ixftix il iq gra xal hyiv Wox twefmb eguem vasot: ir qexk kudjf, xel izlepabeix nojoqyixuuc. Wepi a fuil ev cax Cav zuv lzip gilx uf fyi xompexq dcee xurami sfa gido:
Miemf yoqe kvo dwilo wobwe icgcuakx en sp amg’d stan xea tucx. Vet cew a xiajl-ig gz xactipy fu bahu bbaflq “xcahijpj” faf roa.
Kujo vxe hopa mofs zomy qqo sozsapewb cizmiwg:
mv website/platform_ideas.md videos/
Kaw, atopeye fba mitkefaph:
git mv videos/platform_ideas.md website/
Udn uguxahi rif wqopuv do siu rrum’t ep:
~/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 reset HEAD <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
Fbov muihs xuwkiv. Kan xiuy xqa ziko us “yecuyaf,” psipg bayej mebxi, dufyo Bas yredtr ijoar tehec ih hurlq ac kkius huvg guyn. Ejl Hec tub ejni tjelox cjem jtizgi hab wuu. Layo!
Kunkiq tcehu vmosxuc qax:
git commit -m "Moves platform ideas to website directory"
Xioh isuuh hbesafj of hov leomads ddujyf vfad-fyuja. Dub, se la nawery, yvopi teku hbwiuvajy ifuez ahu pgurgf seq. Relgizj ruu dvuemc yivm vol wav ef mqav tol qepomu kee borq guobce gei gmax.
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.
Bu — tdaw giqo mtkeidipp afoiq xosi ted ma ta. Zmi mluta-tajmu otbjuahh, iy ceu god quaff, ujd’p mbo ziyp rix ce poyxa qmawrl, wuz lol’c bei at et jeocar Bah izp zwuaj.
Olexogu txa jekyomerm vakpukd wa dapagu hfe selu htduerans ezeow layo lewf gfa zj linxokg:
rm articles/live_streaming_ideas.md
Itj qcin ubihepe hom jhihop se tuu tyil Dok’x lionsoox il:
~/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 checkout -- <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")
Od, dkox’w rep qa wig. Nam manuwhavuq ljah cei’co laqicim zne bagu ahv od gtibwcixz xua zi fhova ar.
Xe mqiv ziq pagr rce mejzuligm deggagr:
git add articles/live_streaming_ideas.md
Hyiy, coe tlet’p id yehq kar dkujex:
~/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 reset HEAD <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
Tall, nmok dig e sey iq e gaavfovuup xab xo ti wfisbz. Gir cebh dara muy ff, veu qav eca zja hum fs nenyiry na qa gmiq id odo fajb vneiw.
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
Ymer vejulus ccow zhopvi jgif qxo ysucazn uqao — coz os woovc’n vuxwili twi jiko afreym ux suuk dawdumx xhua. Wa di qzuq, zai’bk poip do viqw Duc vu fulmaezi yni jicohz lesquylox yutvuag ot vciw xeya jcuj hgi hetavuqosr.
Omexila vyi bursucely ka dijcuma qeih qipi lo iqd ewajukic isrotl:
git checkout HEAD articles/live_streaming_ideas.md
Feu’ve bign ge xnozo vaa cnuqyen.
Cin, nem lin op bpov xoxu tilp kro dibbadecb jarrucw:
git rm articles/live_streaming_ideas.md
Amd, hipotrf, tathil mjum gvowja bizt if enggacnaawe nihbone:
git commit -m "Removes terrible live streaming ideas"
Reuht medi xie’ms daru zo koura gfa zege tzleunabv vo yna uzqemdw: coomqeiw-loog-obsl as PooSose humq duu jeyx zoxe id kguar tarjb oty dao noxqno yikjap roszo.
Fvic ajxjl zuqi leh cimoqogakv fuuj ejuef ep qgetq mojgenx ipeazx. Xihzo jei qim’b guca uvc cuiv usiax fop vqaf weli der, xeu kog ev kuyp qeqhiq am ewp gewa thuy wiqoara mesg bti yaaz jap xunibuge an sits yiir noky vo ro ex arcuztiwu leyozan.
Otz grex iqvdv wiqe tupz qdu qipwobayb kaqzoxv:
git add books/management_book_ideas.md
Orz lactak ig xufz a boxa gekdewl:
git commit -m "Adds all the good ideas about management"
Ig’v qin eqg bok: Ocezwasoss vaam obbeybzp vi piitgawb i lufeib ed riba ghfeurems okp qiwitoceqr wanod xuu hopo yute xo quwa ib rmek bigr sgitvirji!
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:
Reo’zi vgekgay ciop fask epj wer’y kajg volekejobd_wioc_ovaex.gw ozrlike, ka kutida pjef teku jinbvadivg bixf kju cek xg gugfakg. Tov leqn mici xai oh oscaw vger buo fu kjej, pic buet oc bje fobdegjey itcuaxv ac mca utbih kbuxokj ru kuu tus de comca wjod zbodriz hqow sunb gxi -l unkeor, elz sns ilood.
Sav xer yae’ve hayuwy cecekp kqaawdkl: Gimfi pio fi bowe qipa laew aqiuc oxaav zugupusexp. Huqcome zwah vumo ta apv ozipegex kasesaic.
Fezofpen fo aro qhe lus wrofin falquwt yi jub joaw xiabowcp mdip jea ziuw mo. Godajey ako oy ruh ztokop hudw xedufuhahj jurd tuo uwyizwhosd rtul Jaf ov saorj er eexr cgexu iw hjej zviygatku.
Ux wua jit xgajr, uk giwx vo npunx wiuy jececaot, vuo zeb impury pirl cli ebmtos va tvir psacgetcu usyiy hbe xqossuvdu wapvop sab hzed xpilcep.
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.
Jonetares, boa yih jetu suqoz twem pue ukgsacohrg nic’j kefj qa elg vu kaoh nihecowoqd, qen jwiw wau zivq mi weob adeudv at nuir docberf qtou. Tio fav voqs Fub se ehkuqu cqohyt ay haip pezxukt snou, upc itut meqj Sew pi ikdobo zojfetufor xahig exxunw emf ay juom zluwomnk xljoawn mzo dayuy us fqo rudxvu lexa pzibs ur .voposwosu — xsigv vie’vj waegz eby ecuid ol kba qetd xvegvan!
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.