-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-basics.html
More file actions
999 lines (906 loc) · 57.1 KB
/
Copy pathgit-basics.html
File metadata and controls
999 lines (906 loc) · 57.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Git Basics — Git & GitHub Handbook</title>
<meta name="description"
content="Learn Git from scratch: installation, commits, branches, undoing mistakes, and the commands you'll use every day.">
<link rel="stylesheet" href="style.css">
</head>
<body>
<button id="navToggle" class="nav-toggle" aria-label="Toggle table of contents">☰ Contents</button>
<div id="navOverlay" class="nav-overlay"></div>
<div class="app">
<nav class="sidebar" id="sidebar" aria-label="Table of contents">
<div class="sidebar-inner">
<a href="index.html" class="brand">
<span class="brand-mark">git</span>
<span class="brand-sub">& github</span>
</a>
<p class="sidebar-tagline">A no-nonsense guide for people who've never touched a terminal.</p>
<div class="site-nav">
<p class="site-nav-label">Guides</p>
<ul class="site-nav-list">
<li><a href="index.html">Home</a></li>
<li><a href="git-basics.html" class="current">1. Git Basics</a></li>
<li><a href="github-guide.html">2. GitHub Essentials</a></li>
<li><a href="collaboration.html">3. Working with Others</a></li>
<li><a href="profile.html">4. Profile & Portfolio</a></li>
<li><a href="tips-tricks.html">5. Tips & Tricks</a></li>
</ul>
</div>
<p class="toc-label">On this page</p>
<ul class="toc-list">
<li><a href="#whatisit">What is Git & GitHub?</a></li>
<li><a href="#concepts">Key Concepts</a></li>
<li><a href="#install">Installation</a></li>
<li><a href="#setup">First Time Setup</a></li>
<li><a href="#workflow">Basic Workflow</a></li>
<li><a href="#gitignore">Ignoring Files</a></li>
<li><a href="#oldfolder">An Existing Folder</a></li>
<li><a href="#branches">Working with Branches</a></li>
<li><a href="#undo">Undoing Mistakes</a></li>
<li><a href="#errors">Common Errors & Fixes</a></li>
<li><a href="#gitcmds">Commands Cheat Sheet</a></li>
<li><a href="#tips">Tips & Common Mistakes</a></li>
<li><a href="#practice">Practice Checklist</a></li>
</ul>
<a class="sidebar-footer-link" href="https://www.linkedin.com/in/muhammad-hamza-naqi-569741403/"
target="_blank" rel="noopener noreferrer">Connect with the author →</a>
</div>
</nav>
<main class="main" id="top">
<div class="content">
<header class="hero">
<p class="eyebrow">Part 1 of 5 · Beginner's Handbook</p>
<h1>Git Basics</h1>
<p class="lede">This part covers Git running solo on your own machine — installing it, saving
snapshots of your work, and undoing mistakes. Every command below is in a terminal box you
can copy with one click — open your own terminal alongside this page and type along as you
read. Once you're comfortable here, move on to
<a href="github-guide.html">GitHub Essentials</a> to start pushing real projects online.</p>
</header>
<hr>
<section id="whatisit">
<h3><span class="num">01</span> What is Git & GitHub?</h3>
<p><b>Git</b> is a tool that tracks changes in your code over time. Think of it like a save
history for your project — you can go back to any previous version whenever you want.
It works entirely on your own computer, no internet needed.</p>
<p>Example: you write code, something breaks, and you have no idea what you changed. With Git you
can just roll back to the last working version.</p>
<p><b>GitHub</b> is a website that stores your Git projects online. It lets you back up your
code, share it with others, and collaborate without things getting messy.</p>
<p>Simple way to think about it — Git is the tool on your PC, GitHub is like Google Drive
but for your code.</p>
</section>
<hr>
<section id="concepts">
<h3><span class="num">02</span> Key Concepts to Know First</h3>
<p>Before running any commands, understand these words. You will see them everywhere:</p>
<ul>
<li>
<b>Repository (Repo)</b> — A folder that Git is tracking. It has a hidden
<code>.git</code> folder inside that stores all the history.
</li>
<li>
<b>Commit</b> — A saved snapshot of your project at a point in time. Like taking a
photo of your code. You can always go back to any photo.
</li>
<li>
<b>Staging Area</b> — Before you commit, you stage files. This means you pick which
changes to include in the next snapshot. <code>git add</code> moves files here.
</li>
<li>
<b>Branch</b> — A separate copy of your project where you can work without
affecting the main code. Great for trying new features safely.
</li>
<li>
<b>Main / Master</b> — The default branch. This is usually your official working
version.
</li>
<li>
<b>Remote</b> — The version of your repo that lives online on GitHub. Your local
copy talks to it using push and pull.
</li>
<li>
<b>Push / Pull</b> — Push means upload your local changes to GitHub. Pull means
download the latest changes from GitHub to your PC.
</li>
<li>
<b>Clone</b> — Downloading a full copy of a repo from GitHub to your computer for
the first time.
</li>
</ul>
<h4>The Git workflow at a glance</h4>
<p>Your code moves through four stages. This is the mental model to keep in your head for
everything that follows:</p>
<div class="diagram-wrap">
<svg viewBox="0 0 720 210" xmlns="http://www.w3.org/2000/svg" role="img"
aria-label="Diagram showing files moving from Working Directory to Staging Area via git add, to Local Repository via git commit, to Remote Repository on GitHub via git push, with git pull moving the other direction">
<defs>
<marker id="arrow" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7"
markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="#E14E2C"></path>
</marker>
<marker id="arrowBlue" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="7"
markerHeight="7" orient="auto-start-reverse">
<path d="M0,0 L10,5 L0,10 z" fill="#2A5C8A"></path>
</marker>
</defs>
<!-- boxes -->
<g font-family="IBM Plex Mono, monospace">
<rect x="10" y="50" width="150" height="70" rx="8" fill="#FBFAF6"
stroke="rgba(28,31,27,0.25)"></rect>
<text x="85" y="82" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#1C1F1B">Working</text>
<text x="85" y="98" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#1C1F1B">Directory</text>
<rect x="195" y="50" width="150" height="70" rx="8" fill="#FBFAF6"
stroke="rgba(28,31,27,0.25)"></rect>
<text x="270" y="82" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#1C1F1B">Staging</text>
<text x="270" y="98" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#1C1F1B">Area</text>
<rect x="380" y="50" width="150" height="70" rx="8" fill="#FBFAF6"
stroke="rgba(28,31,27,0.25)"></rect>
<text x="455" y="82" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#1C1F1B">Local</text>
<text x="455" y="98" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#1C1F1B">Repository</text>
<rect x="565" y="50" width="150" height="70" rx="8" fill="#1A1C20" stroke="#1A1C20">
</rect>
<text x="640" y="82" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#D9DEE6">Remote</text>
<text x="640" y="98" text-anchor="middle" font-size="12.5" font-weight="600"
fill="#D9DEE6">(GitHub)</text>
<!-- forward arrows -->
<line x1="160" y1="70" x2="193" y2="70" stroke="#E14E2C" stroke-width="1.5"
marker-end="url(#arrow)"></line>
<text x="176" y="58" text-anchor="middle" font-size="11" fill="#E14E2C">add</text>
<line x1="345" y1="70" x2="378" y2="70" stroke="#E14E2C" stroke-width="1.5"
marker-end="url(#arrow)"></line>
<text x="361" y="58" text-anchor="middle" font-size="11" fill="#E14E2C">commit</text>
<line x1="530" y1="70" x2="563" y2="70" stroke="#E14E2C" stroke-width="1.5"
marker-end="url(#arrow)"></line>
<text x="546" y="58" text-anchor="middle" font-size="11" fill="#E14E2C">push</text>
<!-- return arrow: pull -->
<path d="M 565 100 H 548 V 140 H 178 V 100 H 163" fill="none" stroke="#2A5C8A"
stroke-width="1.5" marker-end="url(#arrowBlue)"></path>
<text x="363" y="134" text-anchor="middle" font-size="11" fill="#2A5C8A">pull /
fetch</text>
<text x="85" y="175" text-anchor="middle" font-size="11" fill="#767B72">you edit
files</text>
<text x="270" y="175" text-anchor="middle" font-size="11" fill="#767B72">changes
picked</text>
<text x="455" y="175" text-anchor="middle" font-size="11" fill="#767B72">snapshot
saved</text>
<text x="640" y="175" text-anchor="middle" font-size="11" fill="#767B72">shared
online</text>
</g>
</svg>
</div>
</section>
<hr>
<section id="install">
<h3><span class="num">03</span> Installation</h3>
<h4>Windows / Mac</h4>
<ul>
<li>
Go to <a href="https://git-scm.com/downloads" target="_blank"
rel="noopener noreferrer">git-scm.com/downloads</a> and download Git for your OS.
</li>
<li>
Run the installer. Just keep all the default settings and click Next through everything
(except if you see "master" — change it to "main" if it isn't already the default).
</li>
<li>
Open your terminal (Git Bash on Windows, Terminal on Mac) and verify it worked:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git
--version</span>
</div>
<div class="line output">git version 2.44.0</div>
</div>
</div>
<p style="margin-top:-6px;">If it prints a version number, you're good to go.</p>
</li>
</ul>
<h4>Linux</h4>
<ul>
<li>
Git usually isn't installed by default, but it's one command to get it. Run the one for
your distro:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line comment"># Ubuntu / Debian</div>
<div class="line"><span class="prompt">$</span><span class="cmd">sudo apt update
&& sudo apt install git</span></div>
<div class="line comment"># Fedora</div>
<div class="line"><span class="prompt">$</span><span class="cmd">sudo dnf install
git</span></div>
<div class="line comment"># Arch / Manjaro</div>
<div class="line"><span class="prompt">$</span><span class="cmd">sudo pacman -S
git</span></div>
</div>
</div>
</li>
<li>It will ask for your password. Type it and press Enter, then wait for it to finish.</li>
<li>
Verify it worked:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git
--version</span>
</div>
</div>
</div>
</li>
<li>
On Linux you'll also want an SSH key so you can push to GitHub without typing your
password every time:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">ssh-keygen -t
ed25519 -C "youremail@example.com"</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">cat
~/.ssh/id_ed25519.pub</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">Press Enter through the key-generation prompts to accept the
defaults. Copy the output of the second command, then go to GitHub → Settings
→ SSH and GPG keys → New SSH key, and paste it in.</p>
</li>
</ul>
<h4>All platforms</h4>
<ul>
<li>
Create a GitHub account at <a href="https://github.com" target="_blank"
rel="noopener noreferrer">github.com</a> — it's free. Use the same email you'll
use for setup in the next section.
</li>
</ul>
</section>
<hr>
<section id="setup">
<h3><span class="num">04</span> First Time Setup</h3>
<p>You only need to do this once per device. This tells Git who you are, so your commits show
your name.</p>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span class="dot dot-y"></span><span
class="dot dot-g"></span><span class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git config --global
user.name "Your Name"</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">git config --global
user.email "youremail@example.com"</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">git config --global
init.defaultBranch main</span>
</div>
</div>
</div>
<ul>
<li>Use the <b>same email</b> as your GitHub account for <code>user.email</code>. This links
your commits to your GitHub profile and counts your contributions.</li>
<li>Older versions of Git use "master" as the default branch name — the last line changes it
to "main", which is the current standard.</li>
</ul>
</section>
<hr>
<section id="workflow">
<h3><span class="num">05</span> Basic Workflow (Starting a New Project)</h3>
<p>This is the flow you'll use almost every day. Do these steps in order:</p>
<ul>
<li>
Go to <a href="https://github.com/new" target="_blank"
rel="noopener noreferrer">github.com/new</a>, give your repo a name, and click
"Create repository." Copy the URL it gives you.
</li>
<li>
Open your terminal inside your project folder and run:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git init</span>
</div>
</div>
</div>
</li>
<li>
Connect your local folder to GitHub:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git remote add
origin YOUR_REPO_URL</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">Replace the URL with the one you copied. "origin" is just the
nickname for your GitHub repo.</p>
</li>
<li>
Stage your files:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git add .</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">The dot means "add everything in this folder." You can also
stage one file at a time with <code>git add index.html</code>.</p>
</li>
<li>
Commit — take a snapshot:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git commit -m
"first commit"</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">The <code>-m</code> flag is for your message. Write something
that makes sense — future you will thank you.</p>
</li>
<li>
Push to GitHub:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git push -u origin
main</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">This uploads everything. The <code>-u origin main</code>
part is only needed the first time — after that, just <code>git push</code>.</p>
</li>
</ul>
<h4>Your everyday cycle</h4>
<p>After the first push, this is the loop you'll repeat for every change:</p>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span class="dot dot-y"></span><span
class="dot dot-g"></span><span class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git add .</span></div>
<div class="line"><span class="prompt">$</span><span class="cmd">git commit -m "what I
changed"</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">git push</span></div>
</div>
</div>
</section>
<hr>
<section id="gitignore">
<h3><span class="num">06</span> Ignoring Files with .gitignore</h3>
<p>Some files should never be committed — dependency folders, build output, secrets, and
OS clutter. A <code>.gitignore</code> file tells Git to skip them automatically.</p>
<p>Create a file named exactly <code>.gitignore</code> in your project's root folder with
contents like this:</p>
<div class="file">
<div class="file-bar">.gitignore</div>
<div class="file-body">
node_modules/<br>
.env<br>
*.log<br>
.DS_Store<br>
dist/
</div>
</div>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span class="dot dot-y"></span><span
class="dot dot-g"></span><span class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git add .gitignore</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">git commit -m "add
gitignore"</span>
</div>
</div>
</div>
<p><b>Heads up:</b> <code>.gitignore</code> only stops files from being added <i>from now on</i>.
If a file is already tracked, add it to <code>.gitignore</code> and also run
<code>git rm --cached <file></code> to make Git forget it (your local copy stays put).
</p>
</section>
<hr>
<section id="oldfolder">
<h3><span class="num">07</span> Setting Up an Existing Folder</h3>
<p>If you already have a project folder and want to connect it to GitHub, follow these steps.
You might hit a couple of errors along the way, but they're easy to fix:</p>
<ul>
<li>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git init -b
main</span>
</div>
</div>
</div>
</li>
<li>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git remote add
origin YOUR_REPO_URL</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">If you get "remote origin already exists," use this instead:
<code>git remote set-url origin YOUR_REPO_URL</code>
</p>
</li>
<li>
Check that the connection worked:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git remote
-v</span>
</div>
<div class="line output">origin https://github.com/you/repo.git (fetch)</div>
<div class="line output">origin https://github.com/you/repo.git (push)</div>
</div>
</div>
</li>
<li>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git fetch
origin</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">This downloads the latest info from GitHub without touching
your local files.</p>
</li>
<li>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git reset --soft
origin/main</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">This syncs your local history with GitHub's without deleting
anything — your files stay as staged changes.</p>
</li>
<li>
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git push -u origin
main</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">Done — your old folder is now fully synced with GitHub.</p>
</li>
</ul>
</section>
<hr>
<section id="branches">
<h3><span class="num">08</span> Working with Branches</h3>
<p>Branches let you work on a new feature without touching the main code. Think of it as making
a copy of your notebook to try things out, then pasting the good parts back into the
original.</p>
<ul>
<li>
Create a new branch and switch to it:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git checkout -b
feature-name</span>
</div>
</div>
</div>
<p style="margin-top:-6px;">Replace <code>feature-name</code> with something
descriptive, like <code>add-login-page</code>.</p>
</li>
<li>
Do your work, then stage and commit as usual:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git add .</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">git commit -m
"added login page"</span>
</div>
</div>
</div>
</li>
<li>
Switch back to main, then merge your branch in:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git checkout
main</span>
</div>
<div class="line"><span class="prompt">$</span><span class="cmd">git merge
feature-name</span>
</div>
</div>
</div>
</li>
<li><code>git branch</code> — lists all your branches. The one with <code>*</code> is
where you currently are.</li>
<li><code>git checkout branch-name</code> — switch to a branch that already exists.
</li>
</ul>
</section>
<hr>
<section id="undo">
<h3><span class="num">09</span> Undoing Mistakes</h3>
<p>Everyone messes up commits and staged files. These are the safe, everyday ways to undo
things — ordered from "haven't committed yet" to "already pushed to GitHub."</p>
<ul>
<li>
Discard uncommitted changes in a file, back to the last commit:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git restore
<file></span>
</div>
</div>
</div>
</li>
<li>
Unstage a file (undo <code>git add</code>) without losing your changes:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git restore
--staged <file></span>
</div>
</div>
</div>
</li>
<li>
Fix the message of your last commit (only safe if you haven't pushed it yet):
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git commit --amend
-m "new message"</span>
</div>
</div>
</div>
</li>
<li>
Undo your last commit but keep the changes staged, so you can re-commit them properly:
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git reset --soft
HEAD~1</span>
</div>
</div>
</div>
</li>
<li>
Undo a commit that's <b>already pushed</b>: this is the safest option because it doesn't
rewrite history — it adds a new commit that reverses the old one.
<div class="term">
<div class="term-bar"><span class="dot dot-r"></span><span
class="dot dot-y"></span><span class="dot dot-g"></span><span
class="term-title">terminal</span></div>
<div class="term-body">
<div class="line"><span class="prompt">$</span><span class="cmd">git revert
<commit-hash></span>
</div>
</div>
</div>
<p style="margin-top:-6px;">Find the hash you need with <code>git log</code>.</p>
</li>
</ul>
<h4>Reading a merge conflict</h4>
<p>When Git can't automatically combine two changes, it marks the conflicting spot directly in
the file. Open the file, pick which version to keep (or blend them), then delete the marker
lines entirely before you save:</p>
<div class="file">
<div class="file-bar">index.html — conflict markers</div>
<div class="file-body">
<<<<<<< HEAD<br>
<h1>Welcome to my site</h1><br>
=======<br>
<h1>Hello, world</h1><br>
>>>>>>> feature-name
</div>
</div>
<p>Everything above <code>=======</code> is your current branch's version; everything below,
down to the second marker, is the branch you're merging in. Once the file looks right, stage
and commit it like normal to finish the merge.</p>
</section>
<hr>
<section id="errors">
<h3><span class="num">10</span> Common Errors & Fixes</h3>
<p>These are the messages almost every new Git user hits in their first week. None of them mean
you broke anything.</p>
<div class="error-card">
<span class="error-msg">fatal: not a git repository (or any of the parent directories):
.git</span>
<p class="error-fix"><b>Fix:</b> you're not inside a folder Git is tracking. Either
<code>cd</code> into the right project folder, or run <code>git init</code> if this
folder hasn't been set up yet.
</p>
</div>
<div class="error-card">
<span class="error-msg">Please tell me who you are.</span>
<p class="error-fix"><b>Fix:</b> you skipped first-time setup. Run the
<code>git config --global user.name</code> and <code>user.email</code> commands from
section 04.
</p>
</div>
<div class="error-card">
<span class="error-msg">! [rejected] main -> main (fetch first) / Updates were rejected
because the remote contains work that you do not have locally</span>
<p class="error-fix"><b>Fix:</b> GitHub has changes you don't have yet — usually from
editing a file directly on the GitHub website. Run <code>git pull origin main</code>,
resolve any conflicts, then push again.</p>
</div>
<div class="error-card">
<span class="error-msg">fatal: refusing to merge unrelated histories</span>
<p class="error-fix"><b>Fix:</b> this happens when you created the GitHub repo with a
README but your local folder has its own separate commits. Run
<code>git pull origin main --allow-unrelated-histories</code>, resolve conflicts if any
appear, then push.
</p>
</div>
<div class="error-card">
<span class="error-msg">You are in 'detached HEAD' state</span>
<p class="error-fix"><b>Fix:</b> you checked out a specific commit instead of a branch. If
you didn't mean to make changes here, just run <code>git checkout main</code> to get
back to safety.</p>
</div>
</section>
<hr>
<section id="gitcmds">
<h3><span class="num">11</span> Commands Cheat Sheet</h3>
<table class="git-cheat-sheet">
<tr>
<td class="command">git init</td>
<td>Start tracking a folder with Git, creates a new local repo</td>
</tr>
<tr>
<td class="command">git init -b main</td>
<td>Same as above but sets the branch name to main right away</td>
</tr>
<tr>
<td class="command">git clone <URL></td>
<td>Download a repo from GitHub to your computer</td>
</tr>
<tr>
<td class="command">git status</td>
<td>See which files are changed, staged, or untracked — use this often</td>
</tr>
<tr>
<td class="command">git add .</td>
<td>Stage all changed files to be included in the next commit</td>
</tr>
<tr>
<td class="command">git add <file></td>
<td>Stage only a specific file</td>
</tr>
<tr>
<td class="command">git commit -m "msg"</td>
<td>Save a snapshot with a short description message</td>
</tr>
<tr>
<td class="command">git commit</td>
<td>Opens a text editor so you can write a longer, multi-line commit message</td>
</tr>
<tr>
<td class="command">git log</td>
<td>See the full history of all commits, press Q to exit</td>
</tr>
<tr>
<td class="command">git remote -v</td>
<td>List connected remote repos and their URLs</td>
</tr>
<tr>
<td class="command">git remote add origin <URL></td>
<td>Connect your local repo to a GitHub repo for the first time</td>
</tr>
<tr>
<td class="command">git remote set-url origin <URL></td>
<td>Change the GitHub URL if you already have one set</td>
</tr>
<tr>
<td class="command">git fetch origin</td>
<td>Download the latest info from GitHub without changing your local files</td>
</tr>
<tr>
<td class="command">git push</td>
<td>Upload your committed changes to GitHub</td>
</tr>
<tr>
<td class="command">git push -u origin main</td>
<td>First-time push, also sets main as the default branch going forward</td>
</tr>
<tr>
<td class="command">git pull</td>
<td>Download and apply the latest changes from GitHub to your local copy</td>
</tr>
<tr>
<td class="command">git branch</td>
<td>List all branches, the one with * is where you currently are</td>
</tr>
<tr>
<td class="command">git checkout -b <name></td>
<td>Create a new branch and switch to it</td>
</tr>
<tr>
<td class="command">git checkout <name></td>
<td>Switch to an existing branch</td>
</tr>
<tr>
<td class="command">git merge <name></td>
<td>Merge another branch's changes into your current branch</td>
</tr>
<tr>
<td class="command">git restore <file></td>
<td>Discard uncommitted changes in a file</td>
</tr>
<tr>
<td class="command">git restore --staged <file></td>
<td>Unstage a file without losing your changes</td>
</tr>
<tr>
<td class="command">git commit --amend</td>
<td>Edit the message (or contents) of your last commit</td>
</tr>
<tr>
<td class="command">git reset --soft HEAD~1</td>
<td>Undo the last commit, keeping the changes staged</td>
</tr>
<tr>
<td class="command">git revert <hash></td>
<td>Safely undo an already-pushed commit with a new commit</td>
</tr>
<tr>
<td class="command">git reset --soft origin/main</td>
<td>Sync your local history with GitHub's without losing your files</td>
</tr>
</table>
</section>
<hr>
<section id="tips">
<h3><span class="num">12</span> Tips & Common Mistakes</h3>
<ul>
<li>
<b>Write useful commit messages.</b>
<p>"fixed bug" is bad. "fixed login crash when email field is empty" is way better.
Future you will understand it.</p>
</li>
<li>
<b>Run <code>git status</code> before committing.</b>
<p>It's safe and tells you exactly what's going on — what's staged, what's not,
what's untracked.</p>
</li>
<li>
<b>Don't forget <code>git add</code> before <code>git commit</code>.</b>
<p>If you just run <code>git commit</code> without staging first, nothing gets saved.</p>
</li>
<li>
<b>Never commit passwords or API keys.</b>
<p>Use a <code>.gitignore</code> file to exclude sensitive files like <code>.env</code>.
Once something is pushed to GitHub, it's very hard to fully remove.</p>
</li>
<li>
<b>Pull before you push when working with others.</b>
<p>Always run <code>git pull</code> first to get the latest changes and avoid
conflicts.</p>
</li>
<li>
<b>Don't work directly on main.</b>
<p>Create a branch for each feature or fix. Keep main clean, and only merge into it when
things are actually working.</p>
</li>
</ul>
</section>
<hr>
<section id="practice">
<h3><span class="num">13</span> Practice Checklist</h3>
<p>Reading isn't the same as doing. Work through this list in your own terminal — check items
off as you go. (This checklist resets if you reload the page, so treat it as a session
guide, not permanent progress.)</p>
<ul class="checklist">
<li><input type="checkbox" id="p1"><label for="p1">Install Git and confirm it with
<code>git --version</code></label></li>
<li><input type="checkbox" id="p2"><label for="p2">Create a GitHub account</label></li>
<li><input type="checkbox" id="p3"><label for="p3">Run first-time setup (your name and
email)</label></li>
<li><input type="checkbox" id="p4"><label for="p4">Make a folder, run <code>git init</code>,
and create one file</label></li>
<li><input type="checkbox" id="p5"><label for="p5">Stage, commit, and push that file to
GitHub</label></li>
<li><input type="checkbox" id="p6"><label for="p6">Add a <code>.gitignore</code> and commit
it</label></li>
<li><input type="checkbox" id="p7"><label for="p7">Create a branch, change something, and
merge it back into main</label></li>
<li><input type="checkbox" id="p8"><label for="p8">Make a bad commit on purpose, then fix it
with <code>git revert</code></label></li>
</ul>
</section>
<hr>
<div class="note-card">
<p><b>Comfortable with all of this?</b> Head to <a href="github-guide.html">Part 2 — GitHub
Essentials</a> to create real repositories on GitHub, push a whole project folder, and
connect to it from the terminal.</p>
</div>
<hr>
<footer class="page-footer">
<p>Prepared by <b>Muhammad Hamza Naqi</b></p>
<a class="linkedin-btn" href="https://www.linkedin.com/in/muhammad-hamza-naqi-569741403/"
target="_blank" rel="noopener noreferrer">Connect on LinkedIn</a>
</footer>
</div>
</main>
</div>
<button id="backTop" class="back-top" aria-label="Back to top">↑</button>
<script src="script.js"></script>
</body>
</html>