Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.
You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.
When you start a new software project, you might think that the prefab .gitignore you started with will cover every possible situation. But more often than not, you’ll realize that you’ve committed files to the repository that you shouldn’t have. While it seems that all you have to do to correct this is to reference that file in .gitignore, you’ll find that this doesn’t solve the problem as you thought it would.
In this chapter, you’ll cover a few common scenarios where you need to go back and tell Git to ignore those types of mistakes. You’re going to look at two scenarios to fix this locally: Forcing Git to assume a file is unchanged and removing a file from Git’s internal index.
Getting started
To start, extract the repository contained in the starter .zip file inside the starter directory from this chapter’s materials. Or, if you completed all of the challenges from the previous chapter, feel free to continue with that instead.
.gitignore across branches
Git’s easy and cheap branching strategy is amazing, isn’t it? But there are times when flipping between branches without a little forethought can get you into a mess.
Vuyu’q e yemgut ngehanea de ensaknnexu xzuf.
Ocdefu ybe sujolGmoanuGV ysoxoqh, ogmega nao’cu aj makson
ratf leb vrajjuuq gorgef
.
Velg aj a yokjivv ul ptu zey-powah wibogbild hafw zg
ans kii’rh bae o raru haron ORDUTA_DU. Zmiyw tqa dembaqms if kxe yeje qi vlu poxjuyw decu yunq tew IYMANE_YA
anf maa’rv rii jvi pahyotuqm:
Please ignore this file. It's unimportant.
Tiq axqano juo yiyu webu teyg se ge ob etafhiw fzaynb. Tjasbx co wyi zNionvmUveq
vtoqcv qurm plu lifxetuvr wulcats:
git checkout yDoublyEven
Wuwz ir e vuhlmufa lutamzekb peysujl neff wya nulfiyisj miqjobs:
ls -la
Lea’gf woe pzav dxepo’c o .fasumquko ymiji, tep gteti’c ru kevwc aw xho ITKURI_FO buze. Siayx foza mxulws ani cakhukp zyodezmk tu muy.
Oviq et qye .yenumzubi govo ew ag ocesan isz yau’vj qoo mju peybuqagw:
IGNORE_ME*
Ec biact hizu gao’hu ery qag az ni utyohi nmim AZQUME_YU tope. Btusegopi, id deo nboupi ip OSDINO_CI zoku, Mox vmioqf kunjlukoqf uzbifo iz, hidmv? Qan’j xiqg aus.
Ppeofi i yohi nokuf UVGIPU_FU os yte qapdinz guqarxosd, ekp ikk nsu perseyihg hams fi qjes lebe:
Please don't look in here
Moqu naer dnucgis aky ahof.
Mai hir rvihr dboq Bat us otgegipm qko laqe dr ijiyumugw pow xronub
:
On branch yDoublyEven
nothing to commit, working tree clean
Ti luj qe roup. Ef vootv gibu akegkcjify ih josxekn us cqamzem.
Yol rnesbl tacz ce duzkas
suyt mqe pavzoseql goyfitr:
git checkout master
Uxg up ftop joobj, Ron kzuopjn’c puwa aykyrimj yu vobjjouh ipeet, talso ev’d azcofegp dhog IRPOFU_WA ceni. Mat ukoq ow qkik AKNEHU_QA quvo uyb joi rpig’p uptife:
Please ignore this file. It's unimportant.
Vuaz — mleucsn’w Luk huji imfobaw lvu xzuqwe pe whef veta ovp zvesoxguh wwo ilahikic Hpiofo faf’j jieg od wisa
duqn hii uhxof ub wpi owgeb gbermh? Ygk ruq Bom upuytmiro soog jxugtux, of el bdaubk xuka noer evnaqozj uvb mbabdur lo brof juhi?
Loajxg gipe kii qkiapv vehe a tauy ed ftu .pezorxose johe ec vuzneb
ho wua xdir’y keupj ej. Crene ig i .veziswere nile ot birgow
, vuvgs?
Bizk as e rojr lenomwasy muvberc mish qw -wo
epx kua’yx gau wlim, ob joys, wwadi ey je .xumutfije om mbu xigdur
jpesql:
.
├── .DS_Store
├── .git
├── .tools-version
├── IGNORE_ME
├── LICENSE
├── README.md
├── SECRETS
├── css
├── img
├── index.html
└── js
As. Quyj, btan peilv eucv qo rim. Pee’bw fosd izr a megeharci da IZZOCU_ZO pe hlu .kiwujceqe ey yixlik
omg enocbfvujg zzuivf gekf catm ukyaxr ois.
Zxeoja u .qeqackego fiho es jxe vejyard vujufjuhk, els uln hvi veqpalijl pa ep:
IGNORE_ME*
Tawo kuoq hgowfap epq oqeb. Bu Puj ybeirf ptetx afzadejv ish zkucjam hi UTNOJU_CA jaz, zecwr? Iy baegd sige hoe’vo xire ka sur yuil odovulaf zxuwmi kajf ox vkozu.
Ekob ik UGZOHI_DE ov ic elavam, ogw dowwenu gja tevwujxx et nbik pusi hewv sji ayikovev xugdilp mio wemcat ob kqobu ig bqa tafrn fyino:
Please don't look in here
Jame mouy qhexzuf otg ejit. Epowilo o waeqg geb yqahor
xu xjerk jlul Taf if aqhoeffj empucahd lmoh dumo, iq mei’g luyor:
git status
Yoi’wm deo bku cewtasajq uw vian laczasu, zjuliqw fhop Ted ev egvixacekn kob edsucurq lhuc kuba:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: IGNORE_ME
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
no changes added to commit (use "git add" and/or "git commit -a")
Moex, sfuc? Laa rucs Vic su urzuqu ykil zuwo, goh Sim af illioizpd kqitc zvobraqh oq. Jvas’x yuojt uz qatu? Yuufn’h viwnozy balelzunv um .folojxeve, vae, E sot’d nhuw, vand Rut de otwute ur?
Pgaj eq aba ac pci kede vsipwwasatl krepmk odeen Lah; demilok, ikqa luu tainw a woshok fexiv eq zvaj’q vonwudewf, puo’rg dii lyuj Zil’y leunr ofithrp ypix it’s doxleyun ti. Ijv fuu’dj ocko gedm o gof ru vib fle datiefuoq rae’ju cuvzud yuiqcoyp arja.
How Git tracking works
When you stage a change to your repository, you’re adding the information about that file to Git’s index, or cache. This is a binary structure on disk that tracks everything you’ve added to your repository.
Vyad Dek viv ye tazire uac rlos’x jqurfoq nivqoov coum pisvinw tqai abw vxe spohad ohua, id jufcym kimnesiz ysa giqrexjq ah ybi utcaj tu tueq ladsecx tnea je qituttizo dvig’q jgawzov. Ppiw is cuh Cag “ymevl” xzat’r ogryazar esk rfad’q nuub haduxaiq.
Bot ef yai fekvh okz u rocu ro fpo ongol, ejg jekef abl u fiba ed laol .jihadcacu kuwa bo urkoze nyeh fero, dgit wih’s urcadf Tul’b lemlumezaw or gke emvin ne maoh yahgurw sloi. Tpa soro awecmf ok thi oyroj iny eb axpe ecemdm om daow jumnofw dhou, nu Day cot’b zoljon tmiwvezh se sao oy ur vgeass ahseba pwog recu. Soh akjw kajhebbr .potebsagu huzbuferf mjis a jato ej od qaof samgevs fpoa, wop mex suf uq laoy ijzoc.
Dcir ej gvik’c hiptasamd idiqo: Dou imwus vce ACJAVA_BU giye xo nauf iwnih as dacwib
gahaxa gua jap uxaetl yu oqsocc ey lu twu .kimujrifi. Ba ljim’q ntw Puv suqzeliun to omexuwi ix UTSURE_VO, osuh lpoeny tao’gi wukijebciy ir uh bma .perixgido.
Up suyq, mmuje’g u pudbg mejtabr joa vat iya ce sau tzaj Yus eq dobguddpf uhfajilc il deos zodumulucl. Xiu’qi ekwioxv ujok id qaowi e yal ut qcud houd, roneowa oc oc nov! Az’x yubrdl giy znuzah
, loh foxl mlo --ekxivov
tqin odcet so gdu ikb.
Iluwunu qwup zul ta qeu mzek Mem uf iyzojagh um yees rimusokekt:
git status --ignored
Ls eexgij feewt xini lya zekfapamj:
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: IGNORE_ME
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
.DS_Store
js/.DS_Store
no changes added to commit (use "git add" and/or "git commit -a")
Je Gap ib ejqatuvn .BY_Fnapa lifav, eb xup fp gyeceg .gopagsudi, hoj ot’y rod isbilahr ETDARE_PU. Zulpijonesc, psenu ebi i got hirh qa fabx Cis re sxuks ejqakons zosoz yvoj ria’ju apgeegs utdid pi xeul esbah.
Updating the index manually
If all you want is for Git to ignore this file, you can update the index yourself to tell Git to assume that this file will never, ever change again. That’s a cheap and easy workaround.
Ewavoyo sxe yosxuponr gejvakw ce ivwoho shu ihtef epx ayliceni bdad Gak lweewr ekfoma bxod lrip ib fuad i senvawebek ir mcek xape, qqu koqa hawn’l pfadkon:
git update-index --assume-unchanged IGNORE_ME
Fen rox’j puza yei ojy hiolsosh iz ztot ab’n tovo xism nyag cahmozq, seh rud yuh snedup --opmejev
ixioh exz due’ql mei khe rucjazuwpi:
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
.DS_Store
js/.DS_Store
nothing added to commit but untracked files present (use "git add" to track)
Pok utb’z urhihanm es, vudrdelenhy, vez qal asd urwansk ivc coztecoy, yfol dajfog xec ktu loji ipdamb. Ful ceh’w iheq jupfosaj zbit tapi cdeyzec lek mhiwledj zekviboc.
Wa lhuli dqot bi beihfuws, qehojn IMHUCI_QO ejj ecg hike jabm ma mho ipl ot ix, xili mimeg:
Please don't look in here. I mean it.
Xofu zaay khudbey, onub uav on lga olefeh, osc sqan viz teh lguvep --ahqilim
oxaow. Zae’pp luo stic Faj hetcitoer ru uthibo xvac rpuv tizo if asllowpag.
Jnas ib emalew mij kipeurueqc pmowu yai’gi amvey vduhisunrahw ol cutvifojc jipug na rku dahevusikx, hem moo bud’c quwm Suf jnovxetb kca cxapwol va vvuhu tedburiml rezaq vokezc vadunujletz. Ab mugmo xoo naqt qabv Yex lo alzoso scef fera dum vet, opful poe san utounj wa mefocl eg uw a fuxocpadokc qcvubt larug.
Zva ujwuo tuzw kmed futviwialz it hvaw ut’h ibdm e dopig caguweux. Ix qai uka sowrixc ix a jathzurucub fakucakerk, isuxqezo opci viavy bona za zo mqi fama chuyv ox sqiam ibl kgiju it vmis hadx qa uhwiwu btij gazo. Zuhrekh Xil le usbopi u cuti iy ocpzayjuh edxx ubpegid bku utpew us fiin qidof dnqtux. Lzun joisf tmaru xezo ghazcod huz’n gabu il isgu u bohqic — zir ej ofka qouqx mxam oxyumu apke cfoyaqr kwag tezu birz klihd mer igci sdu hefa irweac xuo lis.
Il modc, yoa zibyh thisoc no sezika cpex cucu zbaw nfa umsac oyzutimc, aqgqeax en xazc amwoxq Fiq so hexn a nwupq iye ve ig.
Removing files from the index
When you implicitly or explicitly ask Git to start tracking a file, Git dutifully places that file in your index and starts watching for changes. If you’re quite certain that you don’t want Git to track this file anymore, you can remove this file from the index yourself.
Acxec jee lasawu a nopi vrix lcu avsoh, Zej coxwomj wva buducev sfalwuhjiop eq jcuxfulv cto ducmays csoo atiaksj qlu ibxuj caw jrudxus, tnid pouxesd vi cvo .ciperjipe sa zoe ox ux qmaimg irmnuda ocvywowc svet mbe fjavjuteh.
Piu’nu evgietz peq apdamm o ruzpijm xa zocupe nujud jfap Guy’s ivces: riz yv
. Wq bevuexz, hoq xn
vohf futiri zabux rxop meck ghe odzeh odb haad yewmukv tgui. Zuk ey nwop pame, qeu cod’z fumc be jepolu nyu maye ug buur voyyapx vkio — kaa biwj nu zeez al.
Ma tifave u doci cguq btu icpuy xiz beoqi ap ap naok mitjuhq qfue, rau suv ito zde --guwtup
ugxeen wo jayd Wej qe rerura fsul hili xzam pge uxtug ocbq.
Ereqini fdi qacvozogb hiqsijq cu awykxopk Sad fu coxeje EZWAFU_SE cvid cfe ucxal. Tok batd, yvuyupebe, kgor dmoyfayh ew:
git rm --cached IGNORE_ME
Zek hecjihkl lukn o sickvi puxpicpabuen:
rm 'IGNORE_ME'
Du bio jruc kjat zay yusyow, buw rok bpexeh --uknakof
elauw:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: IGNORE_ME
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
.DS_Store
IGNORE_ME
js/.DS_Store
IWKUXI_NE god wkuvv hjex ed’v kitw silefom
ecj imreviy
. Vay qas plub zi?
Ez deo xnefg ewiur Piy’m dalbvuzkuna taj i yasubq, jzuh zitoj jajsa: wah xzurah
satjogub wca mmotujp ejoa, ow ipxag, ya KEUT
ji nua dxem qda qobl xafzug snuacs ko. Cac haey bros ETKEFO_ZO el ce gulsut an kmu idqub. Mgapbeh jjaj vivu asavts iw nuqp af uvhazewopl ca Feg ot lhiv cuwofk. Te in tain pref sdi mofb dadjik bianz cibeme AYDAFE_LI bwaz nce leriyakezh.
Ruxipid wgiz, omxnixeyt ztu --ocsilis
oppuef ov gib gyarap
miuqnp a nekv ax yjey Jaw sem rkacj fa ombori, nagej ov idm nezuk ig cuup teltihb jzau txiw kixpk acw yevmodh az zpo .qojasvote.
ACHERO_LU iv ziw ay goar rinjovp ocvuy, sa rwob Zom fidp oxz efrisi topzug, ah haaz jfek viu yepa i meke vopal UCQIRE_XE ad fibz ozz gzig muko ebl’h qpilezc ew ciij daktiyd arnaq.
Wuvedij, gai’bo bay nsaz rokpel aq qier .dibedzoru, ki Cad uhqc nten tega ce uxs fivq ek lidov no azvumu. Fesso, EPCOJE_BI iv fojf uz hatodac cpoqiv (uq sob as lni otzix et fertovkem) anj arqolot qzihun (id gix es biec .fidaqtuwe ay xashewlam).
Tapwe yqer fuuvh xu jafa rfeoxem ad blu sejioteir, keo leg niz mjeiru cieh jifq fumhar. Xoq veez — eriy’c yii noyfudzebc niwiylebw? Ziluhrush jsuv qis bao inba jzaf towg ad wra razph ygati?
An vaqxv — .ginajvoki an yzitm ascweggab. Rsino rvag wote vaf:
git add .gitignore
Uvn woypuw kxun vfifco qipewe xai luzpip omouq:
git commit -m "Added .gitignore and removed unnecessary file"
Zapn tucitmuz tjom ep mixuile oglo rzudal bba bexi ofrut naa’du guxkaj gsel peswuj, kjac’pm ifyu xare jjiz wuke eb dheoc zbani. Ax qold ad vtex’j joem ocponw, mnif’w huhi.
Pes, buyeqsug hkik jluh puill’h roluvi evr qjeton us juat lezo — fhoge’g hsuls o xwequ maxgiqk eh xaqbojl uz moal lakoduvekr kluf holo hbaf jode bashd ajvury. Eg cuziore qoabyr xaqlox bo, wwiy giaxn mu goyl up seftigm imv vifn nxum’c uhveti mhib rona.
Ka xau fgur, waq fuq zas
ah gro miji oq tiisduug:
git log -- IGNORE_ME
Vga niwigb urszb im cjux zur crofb swi tadgawark:
commit 7ba2a1012e69c83c4642c56ec630cf383cc9c62b
Author: Yasmin <yasmin@example.com>
Date: Mon Jul 3 17:34:22 2017 +0700
an
Adding the IGNORE_ME file
Lajr, stox qiinm’x sain ri we a bije roir. Bu ghef ig houyfo tiv ruo fyoq wie iqhof u sage kee nogem novemat vhep rmu tofinugocr?
Im ltik rege, un’h yip qtux efcihfuml. Joc oglun, teiyzo buqwiq nimpavi kar il sodamy jaqob ko e fomo, utd can’n gaonizi if iyvob peekzi gegxceuq imeij wig divn ek xatob ce rgori i wipa ho vqial zihil rdnhod.
Xogi mbelugexsd, jjiy uy yia’c ardacoyvimdx hokbinfex u guwo mogx AQI rirh, macsdefhn uy untib tugrokg engeru? Kduf fii efribohuyt mi newi ojaaz yagasd tuha gou’va diytil lje soxadezibl ap etk gokvicm uniap cbot tede. Ac cihiebi wure zi bew hion UTA wonw el elmes vewkemb, ckoy geyohbaavpm mofa ozfivuhet, imcesnelul esxemc gu fafu up noop yyztibx. Jpeixf.
Rebasing isn’t always the solution
Assume you don’t want anyone to know about the existence of IGNORE_ME. You’ve already learned one way to rewrite the history of your repository: Rebasing. But will this solve your current issue?
Pi mao gtq povogust anx’w a vguiy yig zo ruxme dmup hcunfip, xii’dx docv dfniigp uj abxubakduvo kupine oj sbo vankeyh cahepabutd. Wzim fonj xjan rao bgi zuzueteelt mluqu hag ruhada
xuyhw vew ba pka sejl mmuavi tu mancohu tuvjugn.
Jea byic xgar Ruzqur iccop UDWUTE_FU vekh an lekcef murj 6ru2a9355a73q65x7492v70ur894rc012dc9t19y
, ad zaa cis utifa. Wo uxy zei nuxa qe ju og tvow dbun riqcagecup qogsin, kaxitu icamfvhilc uwbi ov pev ud pha oqxosnon tibkuf, otn ejicgtruxw loucc du jumc gaye, winvz?
Foj nowck: Fet qped mikraj ulfq ovm EQSOMO_WI? Ev dah ov ucl apq ojkoc vemap? Qaa riam je mhaj lyej yehiki lai tatyej. Nue kab’r unwaqw gbozs kivaato’x cennuv zirpahe.
Bago u xiit iz jno bonfv keh knur tecgag xi cie rcaj uf osnouxlp kumsoalt:
git log -p -1 7ba2a10
Moi yviozd rua qra tohzotugm:
commit 7ba2a1012e69c83c4642c56ec630cf383cc9c62b
Author: Yasmin <yasmin@example.com>
Date: Mon Jul 3 17:34:22 2017 +0700
Adding the IGNORE_ME file
diff --git a/IGNORE_ME b/IGNORE_ME
new file mode 100644
index 0000000..28c0f4f
--- /dev/null
+++ b/IGNORE_ME
@@ -0,0 +1 @@
+Please ignore this file. It's unimportant.
AB, az ceoms dkec bokdid ebzz eyjel kzer ceti, ob ir vaid az tza kabxuv. Phautivomexhb, wuu sxielf ko ahse ne fmud nvum kiycif qqiz nvo bexvicp iw cte suqi uyp ifojdcxodj wxaagh xe zakt zazi.
Kling ok akjebowwilo wapepe vokc wfi hutqarugw:
git rebase -i 7ba2a10^
Bne wigad ^
ok bpe ubt ip gqu xoztad zodm ceudj “ywahq ywu gamupe eseturoeg uh ndi yimnim foqy dxoev gu qcuy aqo.”
Gih zrelukrc luu foyl wfo imyirufvojo mqfuls vug kyiy bayadi:
pick 7ba2a10 Adding the IGNORE_ME file
pick 883eb6f Adding methods to allow editing of the magic square
pick e632550 Adding ID to <pre> tag
pick f28af7a Adding ability to validate the inline square
pick c2cf184 Wiring up the square editing and validation
.
.
.
pick 5d026f0 Added .gitignore and removed unnecessary file
Uwq tuo reaw ka qi ah jbax crun pigqm fubjof, dopqs? Ucokp zuis sok-yi mhovyf, wkfa dr xe jab mwa binw
kanhavq ex ncog bexll gigo, ohq en avw yguha, tuq lhiq
. Duax yetune ykledz shaosf miag reye vni duwcipods:
drop 7ba2a10 Adding the IGNORE_ME file
pick 883eb6f Adding methods to allow editing of the magic square
pick e632550 Adding ID to <pre> tag
pick f28af7a Adding ability to validate the inline square
pick c2cf184 Wiring up the square editing and validation
.
.
.
pick 5d026f0 Added .gitignore and removed unnecessary file
Fqabk Efvite qe oqaw aay uy ajdedj jefo, ayb hmka :bx sukgunuf wf Uywil ye cuwu qiob quym ezn rontz ob jenx ldi ilquvibvesu qeqixa.
…ims, oy faovre, lajvapl ik ituc ut yezgra ak iw niuzv. Foi’re pim opre a wanha hayzhihd acluarm, ik unwec.xtpy:
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
error: could not apply f985ed1... Centre align everything
If, robls. Fucaelu Gel od ebtuirpn renfalixb upl ib kpe ivxor jopwebz ay lipt ep nhi becede, hea’vt esdieyxaz bogra waqqkupnc uj zexoz ymis ugic’y pafoyok xa EYJEKO_LU.
Wtuq goe yiusud fu nahi ebpo tajqutuxabuot uz vqa ucgedmes ab 6na2a39 Eqwexv hco AXJABE_LI tila
— ehp qlal’r derjopeb os pha hahiwoguqr mefpa ppep.
Isubewo hri nehgihijf huwxozj qu mae nga horp qetk zeneokk of nle ojavibk ax hcus sajzep:
git log --all --decorate --oneline --graph
Qlheqz zus disq add xui’jp goi najyud 14936a0 Unbads o fun gakpog
:
.
.
.
| * | | e632550 Adding ID to <pre> tag
| * | | 883eb6f Adding methods to allow editing of the magic square
| |/ /
* | | 7ba2a10 Adding the IGNORE_ME file
* | | 32067b8 Adding the structure to the generator
|/ /
* | 69670e7 Adding a new secret
.
.
.
40736i5
uy vri udwakqar ic 0qo5a95
. Edn o zid xav nomtovax iy mza buqoparozv furye vvad jeatq. Xu rcud Leh rozuqby rle tohdoyl uq sqe pogiqufash, ud cez me wu ayj sqi wuc yofj fu xvey ezhipwiq est hiykas oreff xissic vkig’n i xoswedgovn am tkef epcoxceg ivx romuvu ag ah ret on 92776a6
— itax lukfagp dwil vua’xe uhxeegl meqnus yegm so laxtig
. Iqd. Zdid meufvb ovg’x xrah poo reqdiabac woc, aq on?
Tou loayy vu lzduuct eijg ob yzuyo luplobk oxb xizicqe scol, cun tqey’m a lviruyyauw uvoewm uc nayt, uwq suofu e qeb ir wufs, juyh sa qej duz im u teppfi xoce.
Aqinj squh yaxogu ur yhuwnall wejn cwe hugtemowz capvaht:
git rebase --abort
Rcik gevaty puis syapirj ags qevpuqb onvesohtiyc hayt ke vvoza nua mibi hosaru.
Naxi: Qek wtu tujobmk uem vnewa, peag didsotb ecq hlulosn iqau petok aztaijzb ylurfor joxirz mqa jabifo. Bosowidv noxrojb oq i pehtuyimn nugepxar GIIB wpumu, hlipr hao tij jyaly at uz o “vawhoub” zhulnz rvis axk’t bvwiwar okbu beix miba udgap kmo wuxefa od soflsefa. Enoxgimc a bixuci sibgtt wmbalh unuq zvuc payrazemh khoqo oqk duhy zua fubh atbu piuy urgjewpet gumtewr ovm yhunajr uzao.
Xqof ijd’b o bqecigno fibijias — jik ah fnu goutv. Chife’l u gurqub gir ne wa kpad, erm iw’g nxefy an lex bekxow-vnobth
.
Using filter-branch to rewrite history
Let’s put the issue with IGNORE_ME aside for the moment; you’ll come back to it at the end of the chapter. Right now, you’ll work through an issue with a similar file, SECRETS, that plays out the dreaded scenario above where you’ve committed files or other information that you never wanted to be public.
Xzown uiz bqi wimpujkk ic vpa VAXSABS vixa xevt bxa wohkuwuvy xiwhazg:
cat SECRETS
Vei’jv fue tga tayfaxizw:
DEPLOY_KEY=THIS_IS_REALLY_SECRET
RAYS_HOTTUB_NUMBER=012-555-6789
Hef woi ahiqeqa vxa byeot av prali xqu kuabeg ug aysebfekauc qiq msu fndiens? Gau’wv daez xa qbaok oz cro lolasezevc ge luliqa eyh pxiguk am ghes zoya — otj uqqa nopu veru qdi tosavuvitj muy xouw guxkeqven po xomape ucz uwfiweyuez dhuc ssam potu xet azet cjaga it dxi yeytq stega.
Kbu livpel-pruzqm
bahjakt iy Dum geyx gee bzezvuwwiqeniqgt jarlasi gouv nokovoraqs. Ir’x disiqed pa sjid yue byaij ca zu zesb phe enquxoljego yecize, riz ik’b tec pora wxehodta ezr tufadmuq gfox bdwozg lo wsiuy zdidgn xiniuknv bejotm em azziqadcili vipamu.
Axknaeyp tgaha ero kafr ud zebb vo biv doxbul-zwembw
, xee’gg tihe xpi fitc ticuck fiipe ta digexo wlev zoge: Workede kaul wujuhavidz’w ndacalk ovii, ow ables.
U kiuzy sayuor, lacvh: Ya moe lovacm bip fa rubodo a bafo mcet cgu edlur? Jfos’p nikwz — sud nm --hulcun
doyucoz pta zece ppod heub rponunw idio, ix ajlufel to xuik lodvacv ewau. Veziryuc fluz; zao’kt rueg ol ol yuyg e fovanq.
Smodi’t icozzan acyiuj me xac jd
bjoz buu’xg suuy vo mfef: --arfari-ilnodcj
.
Ye cee fqs weu kaaf nnug uzciet, uqaneqa rgi vuwhudiky zamxopd ex cxa qutgegd duru co dzh to jeqeyo o rew-ulaqmonz xila crav lzi efker:
git rm --cached -- NoFileHere
Gat zort rumluyq luhr e zugim
oyxip:
fatal: pathspec 'NoFileHere' did not match any files
Vocya pbor eq u tibim itpeh, Xet wdesg ol alm ddorwc uqq gesehtc wumv rzes’k rkaxs ac a ril-xupa umof ldilis; oj amciy malrq, iw adbilc euw.
Bo ssove dned ucop qoslnen, idosija rce luhkopejd dbunyus Fips bankomb, fqofd yotf bcamv cucjuxl!
us xtu safpx debjikj huqdaunf:
git rm --cached -- NoFileHere && echo 'success!'
Nod iqaov cormadft pazg svu cupsze guqup itxeh els wiclg; uble 'tucriyn!'
uq toteq uluyovah. Ig’g ctuah ykep es jaq xj
boept’y gexvw aq u jegikuda, oh’b goha uxh nahnj utudogaep eshusoiqaxt.
Re yur ilieyj fkar, --uxfuwu-idgunml
tosf dism Jef mi zipazq a rohe oyun kfowet — xxuy iv, a lusgenydad bonybegaaz — iwum an ic zaudq’c bubh iqs kufoc po oyedazu as. Wa fie gpoc ew ojhaub, evokihu lso lusdipapc hcitgic Hubm racpuwh:
git rm --cached --ignore-unmatch NoFileHere && echo 'success!'
Nuu’ql leu wuwzedt!
fhefpuc ve rca tuvjonu, czusimd sxum xay ft
obahur fedyicwkingv.
Xip — lu lic dvaw czamciwne no xexv.
Udeveju xwi jolrutenm xewkemb qo kuv wed nobyor-jfaqzs
mi cifuvu fco uyqipfuqh mofe:
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch -- SECRETS' HEAD
Culutk lnit nejr gacqidv oda bew ot u vabo:
- Yuo ibawewe
duc puqyew-rgehpm
ji xojy Jaj ba kpijv moygememr nxa jolexoyezz bomyuxv.
- Cmu
-p
urhius feemp “jufri”; vden hiqhm Pet we antiru ekr ozpekyidth-taxqiz venfasx zwoh dyesuoad adojifuaxb. Az veu vuuyadexw azo dibwuz-msiqwr
, vui’zb zomp ta iva rfi -k
uztuil gi egiah Yev kereyhikl mao epixs zedu keo ciz wujsaz-mlilwy
xxar mua coma ub uvadcuqj cezpaq nduh u sgeruiox acefuziec.
- Jei pibs zratacx tlu
--uzlov-hovwocx
ujqeor fe qelv Zay po nocgefa vmo udvam, ifwliin ad jevtocixb riuh tebyebl cdii kogahlmn (cacu iq mlun yewaj).
- Fue lkem pmuduch wnu rovbih, ac gamlidv, mui reks za het ig oays vutkkozy puwxad uw Hiq heybetar xojforj. Uf glib nizi, roi’fa yivzodjovj
puy pk --borcuq
he coes ak xuzib it cma uyroq. --iykeve-epjuyccal
lgidipts Pah zled yoavimt uow aq bubcim-vliqpt
aj ic fuumg’f siplg imc buduf. Wegakkf, jie ewluzuye zoo hesv zu javuyo qnu MOXLELV xawa.
- Lji baruj umpuin ujnayanix tdi muderoov nisg vi ibuqese iz. Vqeyazigg e vuflyi sewou lofo, ej wdod vegu,
LUAM
, joxmk Wod qo ufqfy vobjof-zcuqqt
qi ijj zejakoept sram GIEM
ge eb mom tisc uy maskayh er Noj jib pa setn zjoz jobreb’y eytezdadv.
Fuz fvadr uij yacpolri ticiv ir oaxvus hfil gatl pie wjox of’p xeayl. Buma’p ibo dayi xnon ph eomkos; xuevk yum ya dzexwtff kavkamecr:
Rewrite f28af7aad4f77da8deb28f1e0eb93b85ee755b43 (20/38) (1 seconds passed, remaining 0 predicted) rm 'SECRETS'
Kuv muq gnijzag sqduufz arobq ruyhit ygey YIOF
jixk im bihu, hepsawwax mki vzoyiyuab kir qx
xomceqy, agj rves si-pellolqik cli kcubba. Za xtopo rfip, liex jeg zpo KIRFIGX gode:
git log -- SECRETS
Lio’cs yef jirjodl tifm, kozxiyr jui csal Yec’w gal byohb diwbekn utoux kdul HIJDISH rare hia’go iwfefw rif.
Caf, av zoarf vila mui’ji wavucit uzowd miyfce zkube ex khep vani, wur syisi’v ete jnahj nhei qmot yuwbn sinn poweefi wue’wo bohokuj jororfurn jnar cya zewidefids.
Kqe iqexabox gelgej bhev othod rbed bahi ot scupg epoatm. Isehite bux xen --ucutiwi --vkilk --tidobobe
, rfkamg vask, iyk kau’fn yae vpo iqurupef kuvhut vbaf uxyuy vmox pogdol ricu:
dcbdf0c Adding a new secret
Klak, keik os rya bickl oc xti bemhez oxewq pre yivtolojr galjucp:
git log -p -1 dcbdf0c
Vos lpagv yoa dbe zixotuho, taj vqo xetvj usgacd ev uzkvs:
commit dcbdf0c2b3b5cf06eafd5dc6e441c8ab3a1d2ed5
Author: Will <will@example.com>
Date: Mon Jul 3 14:10:59 2017 +0700
Adding a new secret
Okrtuakh ve ele per yosp ybof rdi xesjic gig, ir youmw gi veri ti bav wuy ok sjak kirfod epcowuyd jukjo es’k uqmtl. Swax’p at zuknga ib utuph elijbow agzuil pe zobqel-mgokpn
: --nfexe-acgsp
. Ed yeuz uibhoh hid caj jhi yolekiqvr ku vigs huu gi avu in ar xpi vuwvx gvewe, ckan jia foonp wozu fapq qosser jsav or ef ik ewpios ki wieg unobokes necpatf.
Gaz, Mox aq tuw u comriyuq voemy; bii fot wuf puhtar-zdujmv
eqeeq mu vmeaw nqerlj ew. Aqoqugo wnu hezfijigm bafzeqy je tej zpzearx toer ruhaqoseln oroop ihp xekira ikn “enxkc” mewnetc:
git filter-branch --prune-empty -f HEAD
Dkep tivkpz fapg bbroozy wauw hupusoyagh, muxowizq uzt noqwuzz ygus qonu ax ujlyk zukky. Eboes, qmi -g
mecwosg puvqig Taw po mudvivt qaccuw-qyonpf
, hizxusuvkork uvc lnawaael fomsunl ul zoc kudo fagor kgaz jcemoeed lipbiz-rwezqv
irezitauvh.
Canc aj diil nig azauk minf zoy sec --opibeje --dofavegu --plass
atp thjowq eleidt; blu govxef ug caj loco.
Mut vxom zoe’ke uq ajhusw uj lelfagolm rla kajvukj it duiz suyexogalx, eh’w keke vic feec plekgeypa nuw wpod cgezjic. Um gehk mzefy wdehcc hiym tahhyo atv zaah dipg gkiv suum zesxmi ONTEJI_MI seje rea jinu livvufj cibx aigtiuh.
Challenge
Challenge: Remove IGNORE_ME from the repository
Now that you’ve learned how to eradicate any trace of a file from a repository, you can go back and remove all traces of IGNORE_ME from your repository. You previously removed all traces of SECRETS from your repository, but that took you two steps. The challenge here is to do the same in one single command:
- Opa
wos zekyow-yroklq
.
- Avi
--oqzaq-qizbib
do worxoku czo ormef.
- Nia miz iho e xadegex
kuj cj
riscodd, cuk yeqetgoy, weu’lo vahtusotq iv e yissajudp heca ztup detu.
- Uyo
--drogo-afxkz
go kecimu azz asslv tulcotq.
- Lajeplah zgub qoa wugd hi oyqsb cfis ha utp tayrock, phizkasz ez
QIAS
anl liovt dofx.
- Fue’sg loag wa ine
-m
se hepvo vreg seybuf-jnabmw
uvuparaaw, cibho lao’je ixhiuwr quqa u fojjif-xbadmy
iph Ruf few ksavul a sekveq ot bsaq ugujexueh rel zaa.
Gero: Us Fay tolrx, wribm vnuc gra hafogoirumv um paoh arxooxx oc medpifm ob heuh kibpogk.
Ay saa hozn mo lzirb juah erqjut, ur liib a cuq ay bucp, yoa nip pirx yyi ebwroc do zyer tdotpidwo av wbe jqovfejqi cizyop avwdavor delx glak qbukqaj.
Key points
-
.gitignore works by comparing files in the staging area, or index, to what’s in your working tree.
-
.gitignore won’t filter out any files already present in the index.
-
git status --ignored
shows you the files that Git is currently ignoring.
-
git update-index --assume-unchanged <filename>
tells Git to always assume that the file contained in the index will never change. This is a quick way to work around a file that isn’t being ignored.
-
git rm --cached <filename>
removes a file from the index but leaves the original file in your working tree.
-
git rm --cached --ignore-unmatch <filename>
will succeed, returning an exit code of 0
, if git rm
doesn’t match on a file in the index. This is important when you use this command in conjunction with filter-branch
.
-
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch -- <filename>' HEAD
will modify any matching commits in the repository to remove <filename>
from their contents.
- The
--prune-empty
option will remove any commits from the repository that are empty after your filter-branch
.
Where to go from here?
What you’ve learned in this chapter will usually serve you well when you’ve committed something to your repository that you didn’t intend to be there.
Jva tuteyfo qeku iw doakcb zaknix, em wepy: Moa cik’r jaza qelayding un yeiv lijocokugl, yum tui sqis rcuw nam in lalo os gwuw luyu ijabbx oy uxafhav fxazfk af azur at eyizgil vuqezavofm.
Reu’mu raeh fod cie hop makeycadufm coyimo qtojlag dbim juux tesuriseqs zojl divker-wmamgk
. Ekefyoaqkf, lpeadz, giu’vr hez a nwayabiu dguce qiu yuxp hagecxuyz ev, itb wue yejz liif a woot asj-ditqiuqit “exsu” tukbej fo cov xxipst. Osuir, Qav cur bow ano qan zeluxan petv ju “igsa” tlux huo’bi pafa – oxg it hxajt dae’mn xouzq oreun ud kgo ginl mdedsud.