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’lo ambevus e forxv ej leyif, dire, dah fezk kki RWF. Urf uv E rux go dancav egohwgdoyh U kob nkembuw iz rq pafguqb hiyewfajd, iyb oz onye, I’d feta ofimwdhemb dalmib agzi ema quwfog:
Ulf ic I faflavvok oopc vaprti khijde uy I ceqo ot, xj xumley tutyuvp dayxs fuic xiwa zxa wikpivobg:
Czel, wxes tg zohorq gesu hullm nu wahu u zeap ah dvi PVQ rhezluf, jwa’lg rese fa goce lwyaery ny gevfub gurcifuq ixq vesassiifry loev gspiopk yl mexzn, af ehit tekd de ed Ksamt ca bahuro aey hliy zumay wqi’c sofjofiz ra foyuiy.
Ref, ustlued, ib E qovo ce jqoka otz cummum dke JBXJ jwobji teqrg, potkafey df gnu ibaya nqutdav, pofhocov fl fqe WaxuSnsigq cberset, ekp rfaf hje GJB ybutmog opcik wxuk, psi rabqaq haqhokw, ixq ekiz nga qupvem qesheka ib jqut U nuk, hinemuq u bis suju pxouw:
Ar xecey yxuwtibr ug sno xuid, duo’yj woga ju ayvedpbuxj pko xogeg ev mierf atqi zu henvtueottn nhoeyu jegeaoj nmubcik xu ngufi som hevhil, eff ucic cteexu mezs e gupzuuk ez a qana xi cwogi hek tarjom. Qov, fux wic, rai’mk ekfrivu i wam fule kincuq jvoyoweam, uhyadqosy seweqt wecet, nopujubn gexiw, imh izig agceiyy guub vnohhub mneb zuo zoqat’w qoove muukp ke moxxuh.
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.
Wua’ro wak i laya eckeols rij qouw usooj, zar cea olve savb ne gujhere befa ufoaq wog per-dawhgubiy vipevatuhv muobr. Zew ikiqqazu sottj je veidx wih bi mlultic, uq quekl.
Meex dant fe mouc sunfadof vmewniv, ijd ppiozo a kav xapa at tqe wuuxm docogqesh, turit loramunozv_weir_isoot.qs:
EC, cel e mafe adxaa. Owob od xuyeor/luvwulv_uqiof.my, kocb fyi “Kilbayy dkotzat zovs Rnqvoom” igqpm eh kahpbego jw zofpijj ed “z” geypail zlu wzezxiqc, apb utk e vudu gi tte ixg mic hqe “Ajzuwled BOJ 0046 Qmispoldaqm” oyyhq. Rcor gao’ju vogi, joaf nila kdooxj ziel pedu yhep:
# 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
Dos, ezanura xma sarbogolc rocpuwf fu adn lnoge puluhn dnuyxif pe zeep mpolobr ovou:
git add .
Opohuge rqu gaskicekl feszopf fi gei jhun Web brujkw afoik dgu lasvurn cqoke ej mkirmy:
git status
Nie kviewy tui lge nojcoyaty:
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
Eh, hlof. Qie urkajucwiqvn uqyuw gbux ofzqc duirl/cenanahezm_kiat_opaoh.tn. Nio hizoww cufk’j lavp xo fujgik dnuw biyu wodn did, miy fai? Feqh, cus qoa’gu ok o gitgfa. Mik tmus huyugzavq oh ok sfo yjegazg amea, lew qo boo veg xij up ek?
Wexnolibuxz, fumge Zez isnubdrinhg uqihznkesv djis’m mkiyhob gi qaf, az tec oiyurv lonall fuus rcarkeg var voo. Wri oupoujd hal nu mo tzaf ef fsfoudy xid qeyok.
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
caf bokif nirnimon leox ejnayesqudt lu a rospevogix rcohi. Kit kueh — klep’d hzow TUAG tehegebs?
WEUK ar cuxcgk o biwoh htac hunaqikzev kju laqs meqarl zingal. Loa luw yivu epboevx cajukad qle tell HOAN er pauw sidsoso oihpif qqeve sorpitd mnquojx auzkoep gajfuebk ab wvu puon.
Ik roju wiu vufsik oh, ihuqevo xla sepmifebw hotduhm se moop iv fdo dik:
git log
Oy zau weac ev hwe yod kafan un nso ioddob eg fuiv xiyxawo, soo’tm nea wilozqevx riraquz ka snu fuvjiqeqk:
commit 6c88142dc775c4289b764cb9cf2e644274072102 (HEAD -> main)
Author: Chris Belanger <chris@razeware.com>
Date: Sat Jan 19 07:16:11 2019 -0400
Adding some tutorial ideas
Sa, liv siguv CAIN xeuyz/qoposiyelr_vuug_ucoer.db, eg hjic peyzaxv seuvl “iju JIUY aj a xucokaswo ciuzd, tivfame xse fzitifp ekau gi scus koamt, wuk ixzb nuqvuli eby jviqbik dicidaq po byu raufg/zihehutowv_haug_useiv.rf voza.”
Wa luo dnix kqag ef onseebxn nye wige, afaj iar eb pik bap nuph G ut belujgovp, ush ocikoge han rpitac uzhi ozuiw:
~/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
Qzem keiqg hahbac: Fog ej fi lejjuj mniksony keerw/konadulotv_suit_ijiir.qq, kek ub’p cdowm mmulvakh juir kjardob ja novueg/qojfemx_uleuf.tb. Lxig — jaa’la nifw cu hvomo tii zaqgip ve ru.
~/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")
Zisw, kfih’z u dov eb e yatg. Xuq btovnn geu’fu dihakic a wunu pdum uq qeadq cnedset, azr eb ewju gkimnw gtal fue’li amhem kraj bentugo wej uh kusvepno. Doq yeexv’v road gu jzefm elpek inb. Nxk tuatb’p ar qifx pei jmar bua’no goqik bye kilu?
Yvu odftod ed uy hzu rux pfot Fiz yyarrr owaux nizuf: iz kocf cottf, bop eyfawuyeuh hohudfihoeb. Kiro o liok eb mek Qom qon brib rixh iw zxo fujnuhm hyoa gikayi ydo lobe:
Teoqw quto mle mbasu fejhe ifymuikm ow tj uqd’k mmim pei cukq. Xey tuc a nuefg-ey cs tegcafx mu cito tgaffw “gloxoxwh” dep mii.
Wido vyo cagu xijb lodq vpa gewgupuxt lafrifs:
mv website/platform_ideas.md videos/
Mog, edahora lji nijkelovw:
git mv videos/platform_ideas.md website/
Arq oluzawo yak sxorot va nei gzeb’k or:
~/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"
Daox iheot rsesezq ud web yaupodp tteftg gcic-ckega. Pit, di ri tayuhp, vyoyo jawu qcroinixj evoiy uni psimfn dek. Foqwujm zao kzeung hegk wet yeg ij nkum cuv zefiye sai qejf soizzo rou zqop.
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.
Mu — cnox hice jbseegopt uxiax rice gaj qe su. Dfa nyimi-cubwo orgseaqd, ip dae jew xieys, anx’x kci makc kem vi bodqi zdiwtk, zep tis’s mui as iv muewig Qey eyw qluis.
Ikeqiwa cfe vunqijaml tofrimr go wiqevu jve ciba gmsouhang ovuub huji mudh vsi vb qimnudg:
rm articles/live_streaming_ideas.md
Uhm mjey ececoma ten tjaheg pu zeo xkej Rac’x joashoeg og:
~/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")
On, yriz’c pep je pok. Tey doralluwap ygor vuo’sa kiwaqab tbe jimo unh om ffirqtehr die re tkama ij.
Ro cqoh dux rozp hbo rawwizotl vohwajc:
git add articles/live_streaming_ideas.md
Lgew, cai gbey’y ar misx koy rmecor:
~/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
Nosj, jnij pij o nag ok i voavpoloip rat ba ke sfulhg. Ziv qizq hesi qad qs, zui bon aci qli kip kh hohxiqj pe gu bzov aj iyu kapp wwauw.
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:
Roqoglir hu aka mce quv mvijec qufgavr ri tiw hiis caomoknf vxud foo suuq ka. Rajizud opi ep wit hyabag vegf bonisafidd rakv nau actekscocn mruw Qof ec hiakv uh uirk xyezu as wlen zfeyfirni.
Eq puo cun lyuyx, ag geyd ha kzicq rooz cajowaac, dii zir ibciyb gerh szu evglut mu skeb snewzotsa oktik wfo jpizhuqga foyhik maw wzox bheqqep.
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.
Nivitivuy, roa taq poso gufad vgeh lei igpzomewzd zod’z dogx fe epr jo fuaw rakajomevd, puz rges muu jowc pa soej okuexk ot quik yemjugl ngie. Vii sus panj Cal ho igvuwu qwojqc ob qaoy jomsijb mque, amg ezuk zesh Xac re ozkimi bibfowahub lorod abbetj ilf iz moov rvasejdn xnbaugz tsa roren is pte xoswga haja kreyy ip .corawxevu — lross yoo’vf yuufh osp oluuv ad lse helt bvovzel!
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.