forked from LibraryCarpentry/lc-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-getting-started.html
759 lines (706 loc) · 41.7 KB
/
02-getting-started.html
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
<!DOCTYPE html>
<!-- START: inst/pkgdown/templates/layout.html --><!-- Generated by pkgdown: do not edit by hand --><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="utf-8"><title>Library Carpentry: Introduction to Git: Getting started with Git</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" type="text/css" href="assets/styles.css"><script src="assets/scripts.js" type="text/javascript"></script><!-- mathjax --><script type="text/x-mathjax-config">
MathJax.Hub.Config({
config: ["MMLorHTML.js"],
jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML", "output/PreviewHTML"],
extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js", "fast-preview.js", "AssistiveMML.js", "a11y/accessibility-menu.js"],
TeX: {
extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
},
tex2jax: {
inlineMath: [['\\(', '\\)']],
displayMath: [ ['$$','$$'], ['\\[', '\\]'] ],
processEscapes: true
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><!-- Responsive Favicon for The Carpentries --><link rel="apple-touch-icon" sizes="180x180" href="apple-touch-icon.png"><link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png"><link rel="manifest" href="site.webmanifest"><link rel="mask-icon" href="safari-pinned-tab.svg" color="#5bbad5"><meta name="msapplication-TileColor" content="#da532c"><meta name="theme-color" content="#ffffff"></head><body>
<header id="top" class="navbar navbar-expand-md navbar-light bg-white top-nav library"><a class="visually-hidden-focusable skip-link" href="#main-content">Skip to main content</a>
<div class="container-fluid top-nav-container">
<div class="col-md-6">
<div class="large-logo">
<img alt="Library Carpentry" src="assets/images/library-logo.svg"></div>
</div>
<div class="selector-container">
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle bordered-button" type="button" id="dropdownMenu1" data-bs-toggle="dropdown" aria-expanded="false">
<i aria-hidden="true" class="icon" data-feather="eye"></i> Learner View <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1"><li><button class="dropdown-item" type="button" onclick="window.location.href='instructor/02-getting-started.html';">Instructor View</button></li>
</ul></div>
</div>
</div>
<hr></header><nav class="navbar navbar-expand-xl navbar-light bg-white bottom-nav library" aria-label="Main Navigation"><div class="container-fluid nav-container">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
<span class="menu-title">Menu</span>
</button>
<div class="nav-logo">
<img class="small-logo" alt="Library Carpentry" src="assets/images/library-logo-sm.svg"></div>
<div class="lesson-title-md">
Library Carpentry: Introduction to Git
</div>
<div class="search-icon-sm">
<!-- TODO: do not show until we have search
<i role="img" aria-label="search button" data-feather="search"></i>
-->
</div>
<div class="desktop-nav">
<ul class="navbar-nav me-auto mb-2 mb-lg-0"><li class="nav-item">
<span class="lesson-title">
Library Carpentry: Introduction to Git
</span>
</li>
<li class="nav-item">
<a class="nav-link" href="key-points.html">Key Points</a>
</li>
<li class="nav-item">
<a class="nav-link" href="reference.html#glossary">Glossary</a>
</li>
<li class="nav-item">
<a class="nav-link" href="profiles.html">Learner Profiles</a>
</li>
<li class="nav-item dropdown">
<button class="nav-link dropdown-toggle" id="navbarDropdown" data-bs-toggle="dropdown" aria-expanded="false">
More <i data-feather="chevron-down"></i>
</button>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown"><li><a class="dropdown-item" href="discuss.html">Discussion</a></li><li><a class="dropdown-item" href="reference.html">FIXME</a></li>
</ul></li>
</ul></div>
<form class="d-flex col-md-2 search-form">
<fieldset disabled><input class="form-control me-2 searchbox" type="search" placeholder="Search" aria-label="Search"><button class="btn btn-outline-success tablet-search-button" type="submit">
<i class="search-icon" data-feather="search" role="img" aria-label="search button"></i>
</button>
</fieldset></form>
</div><!--/div.container-fluid -->
</nav><div class="col-md-12 mobile-title">
Library Carpentry: Introduction to Git
</div>
<aside class="col-md-12 lesson-progress"><div style="width: 6%" class="percentage">
6%
</div>
<div class="progress library">
<div class="progress-bar library" role="progressbar" style="width: 6%" aria-valuenow="6" aria-label="Lesson Progress" aria-valuemin="0" aria-valuemax="100">
</div>
</div>
</aside><div class="container">
<div class="row">
<!-- START: inst/pkgdown/templates/navbar.html -->
<div id="sidebar-col" class="col-lg-4">
<div id="sidebar" class="sidebar">
<nav aria-labelledby="flush-headingEleven"><button role="button" aria-label="close menu" alt="close menu" aria-expanded="true" aria-controls="sidebar" class="collapse-toggle">
<i class="search-icon" data-feather="x" role="img"></i>
</button>
<div class="sidebar-inner">
<div class="row mobile-row">
<div class="col">
<div class="sidenav-view-selector">
<div class="accordion accordion-flush" id="accordionFlush9">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingNine">
<button class="accordion-button collapsed" id="instructor" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseNine" aria-expanded="false" aria-controls="flush-collapseNine">
<i id="eye" aria-hidden="true" class="icon" data-feather="eye"></i> Learner View
</button>
</h2>
<div id="flush-collapseNine" class="accordion-collapse collapse" aria-labelledby="flush-headingNine" data-bs-parent="#accordionFlush2">
<div class="accordion-body">
<a href="instructor/02-getting-started.html">Instructor View</a>
</div>
</div>
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
</div><!--div.sidenav-view-selector -->
</div><!--/div.col -->
<hr></div><!--/div.mobile-row -->
<div class="accordion accordion-flush" id="accordionFlush11">
<div class="accordion-item">
<button id="chapters" class="accordion-button show" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseEleven" aria-expanded="false" aria-controls="flush-collapseEleven">
<h2 class="accordion-header chapters" id="flush-headingEleven">
EPISODES
</h2>
</button>
<div id="flush-collapseEleven" class="accordion-collapse show collapse" aria-labelledby="flush-headingEleven" data-bs-parent="#accordionFlush11">
<div class="accordion-body">
<div class="accordion accordion-flush" id="accordionFlush1">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading1">
<a href="index.html">Summary and Setup</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush2">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading2">
<a href="01-what-is-git.html">1. What is Git/GitHub?</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlushcurrent">
<div class="accordion-item">
<div class="accordion-header" id="flush-headingcurrent">
<span class="visually-hidden">Current Chapter</span>
<span class="current-chapter">
2. Getting started with Git
</span>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush4">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading4">
<a href="03-sharing.html">3. Sharing your work</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush5">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading5">
<a href="04-review.html">4. Review</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
<div class="accordion accordion-flush" id="accordionFlush6">
<div class="accordion-item">
<div class="accordion-header" id="flush-heading6">
<a href="05-github-pages.html">5. GitHub Pages</a>
</div><!--/div.accordion-header-->
</div><!--/div.accordion-item-->
</div><!--/div.accordion-flush-->
</div>
</div>
</div>
<hr class="half-width"><div class="accordion accordion-flush resources" id="accordionFlush12">
<div class="accordion-item">
<h2 class="accordion-header" id="flush-headingTwelve">
<button class="accordion-button collapsed" id="resources" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapseTwelve" aria-expanded="false" aria-controls="flush-collapseTwelve">
RESOURCES
</button>
</h2>
<div id="flush-collapseTwelve" class="accordion-collapse collapse" aria-labelledby="flush-headingTwelve" data-bs-parent="#accordionFlush12">
<div class="accordion-body">
<ul><li>
<a href="key-points.html">Key Points</a>
</li>
<li>
<a href="reference.html#glossary">Glossary</a>
</li>
<li>
<a href="profiles.html">Learner Profiles</a>
</li>
<li><a href="discuss.html">Discussion</a></li><li><a href="reference.html">FIXME</a></li>
</ul></div>
</div>
</div>
</div>
<hr class="half-width resources"><a href="aio.html">See all in one page</a>
<hr class="d-none d-sm-block d-md-none"><div class="d-grid gap-1">
</div>
</div><!-- /div.accordion -->
</div><!-- /div.sidebar-inner -->
</nav></div><!-- /div.sidebar -->
</div><!-- /div.sidebar-col -->
<!-- END: inst/pkgdown/templates/navbar.html-->
<!-- START: inst/pkgdown/templates/content-instructor.html -->
<div class="col-xl-8 col-lg-12 primary-content">
<nav class="lesson-content mx-md-4" aria-label="Previous and Next Chapter"><!-- content for small screens --><div class="d-block d-sm-block d-md-none">
<a class="chapter-link" href="01-what-is-git.html"><i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>Previous</a>
<a class="chapter-link float-end" href="03-sharing.html">Next<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i></a>
</div>
<!-- content for large screens -->
<div class="d-none d-sm-none d-md-block">
<a class="chapter-link" href="01-what-is-git.html" rel="prev">
<i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>
Previous: What is Git/GitHub?
</a>
<a class="chapter-link float-end" href="03-sharing.html" rel="next">
Next: Sharing your work...
<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i>
</a>
</div>
<hr></nav><main id="main-content" class="main-content"><div class="container lesson-content">
<h1>Getting started with Git</h1>
<p> Last updated on 2023-04-21 |
<a href="https://github.com/librarycarpentry/lc-git/edit/main/episodes/02-getting-started.md" class="external-link">Edit this page <i aria-hidden="true" data-feather="edit"></i></a></p>
<div class="text-end">
<button role="button" aria-pressed="false" tabindex="0" id="expand-code" class="pull-right"> Expand All Solutions <i aria-hidden="true" data-feather="plus"></i></button>
</div>
<div class="overview card">
<h2 class="card-header">Overview</h2>
<div class="row g-0">
<div class="col-md-4">
<div class="card-body">
<div class="inner">
<h3 class="card-title">Questions</h3>
<ul><li>What are repositories and how are they created?</li>
<li>What do <code>add</code> and <code>commit</code> mean?</li>
<li>How do I check the status of my repository?</li>
</ul></div>
</div>
</div>
<div class="col-md-8">
<div class="card-body">
<div class="inner bordered">
<h3 class="card-title">Objectives</h3>
<ul><li>create a Git repository</li>
<li>track changes to files using the Git repository</li>
<li>query the current status of the Git repository</li>
</ul></div>
</div>
</div>
</div>
</div>
<div class="section level3">
<h3 id="setting-up-git">Setting up Git<a class="anchor" aria-label="anchor" href="#setting-up-git"></a></h3>
<p>When we use Git on a new computer for the first time, we need to
configure a few things. The basic elements of a configuration for Git
are:</p>
<ul><li>your name and email address,</li>
<li>what your preferred text editor is,</li>
<li>the name of your default branch (branches are an important component
of Git that we will cover later).</li>
</ul><p>To start we will check in on our current Git configuration. Open your
shell terminal window and type:</p>
<div class="codewrapper sourceCode" id="cb1">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git config <span class="at">--list</span></span></code></pre>
</div>
<p>On MacOS, without any configuration your output might look like
this:</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code><span><span class="va">credential.helper</span><span class="op">=</span><span class="va">osxkeychain</span></span></code></pre>
</div>
<p>On Windows, without any configuration your output might look like
this:</p>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager-core
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=main</code></pre>
</div>
<p>If you have different output, then you may have your Git configured
already. If you have not configured Git, we will do that together now.
First, we will tell Git our user name and email.</p>
<p>Please note: You need to use the same email address in your Git
configuration in the shell as you entered into GitHub when you created
your GitHub account. Later in the lesson we will be using GitHub and the
email addresses need to match. If you are concerned about privacy,
please review <a href="https://help.github.com/articles/keeping-your-email-address-private/" class="external-link">GitHub’s
instructions for keeping your email address private</a>.</p>
<p>Type these two commands into your shell, replacing
<code>Your Name</code> and the email address with your own:</p>
<div class="codewrapper sourceCode" id="cb4">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git config <span class="at">--global</span> user.name <span class="st">"Your Name"</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git config <span class="at">--global</span> user.email <span class="st">"[email protected]"</span></span></code></pre>
</div>
<p>If you enter the commands correctly, the shell will merely return a
command prompt and no messages. To check your work, ask Git what your
configuration is using the same command as above:</p>
<div class="codewrapper sourceCode" id="cb5">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">git</span> config <span class="at">--list</span></span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>user.name=Your Name
[email protected]</code></pre>
</div>
<p>Let’s also set our default text editor. A text editor is necessary
with some of your Git work, and the default from Git is Vim, which is a
text editor that is hard to learn at first. Therefore, we recommend
setting a simpler text editor in your Git configuration for this
lesson.</p>
<div id="text-editors" class="callout callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="text-editors" class="callout-inner">
<h3 class="callout-title">Text editors<a class="anchor" aria-label="anchor" href="#text-editors"></a>
</h3>
<div class="callout-content">
<p>There are a lot of text editors to choose from, and a lot of people
are enthusiastic about their preferences. Vi and Vim are popular editors
for users of the BASH shell. If you will be using Git or the Shell with
a group of people for a project or for work, asking for recommendations
or preferences can help you pick an editor to get started with. If you
already have your favorite, then you can set it as your default editor
with Git.</p>
</div>
</div>
</div>
<p>Any text editor can be made default by adding the correct file path
and command line options (see <a href="https://help.github.com/articles/associating-text-editors-with-git/" class="external-link">GitHub
help</a>). However, the simplest <code>core.editor</code> values are
<code>"notepad"</code> on Windows, <code>"nano -w"</code> on Mac, and
<code>"nano -w"</code> on Linux.</p>
<p>For example:</p>
<div class="codewrapper sourceCode" id="cb7">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git config <span class="at">--global</span> core.editor <span class="st">"notepad"</span></span></code></pre>
</div>
<div class="codewrapper sourceCode" id="cb8">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git config <span class="at">--global</span> core.editor <span class="st">"nano -w"</span></span></code></pre>
</div>
<p>Lastly, we need to set the name of our default branch to
<code>main.</code></p>
<div class="codewrapper sourceCode" id="cb9">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git config <span class="at">--global</span> init.defaultBranch main</span></code></pre>
</div>
<p>The <code>init.defaultBranch</code> value configures git to set the
default branch to <code>main</code> instead of <code>master</code>.</p>
</div>
<div class="section level3">
<h3 id="creating-a-repository">Creating a repository<a class="anchor" aria-label="anchor" href="#creating-a-repository"></a></h3>
<p>A Git <strong>repository</strong> is a data structure used to track
changes to a set of project files over time. Repositories are stored
within the same directory as these project files, in a hidden directory
called <code>.git</code>. We can create a new git repository either by
using <a href="https://github.com/new" class="external-link">GitHub’s web interface</a>, or
via the command line. Let’s use the command line to create a git
repository for the experiments that we’re going to do today.</p>
<p>First, we will create a new directory for our project and enter that
directory. We will explain commands as we go along.</p>
<div class="codewrapper sourceCode" id="cb10">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> mkdir hello-world</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> cd hello-world</span></code></pre>
</div>
</div>
<div class="section level3">
<h3 id="using-git">Using Git<a class="anchor" aria-label="anchor" href="#using-git"></a></h3>
<p>One of the main barriers to getting started with Git is understanding
the terminology necessary to executing commands. Although some of the
language used in Git aligns with common-use words in English, other
terms are not so clear. The best way to learn Git terminology - which
consists of a number of verbs such as add, commit and push (preceded by
the word ‘git’) - is to use it, which is what we will be doing during
this lesson. We will explain these commands as we proceed from setting
up a new version-controlled project to publishing our own website.</p>
<p>On a command line interface, Git commands are written as
<code>git verb options</code>, where <code>verb</code> is what we
actually want to do and <code>options</code> is additional optional
information which may be needed for the <code>verb</code>. So let’s get
started with our setup.</p>
<p>We will now create an empty git repository to track changes to our
project. To do this we will use the git <strong>init</strong> command,
which is simply short for <em>initialise</em>.</p>
<div class="codewrapper sourceCode" id="cb11">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git init</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>Initialized empty Git repository in <your file path>/hello-world/.git/</code></pre>
</div>
<p>The <code>hello-world</code> directory is now a git repository.</p>
<p>If we run the <code>ls</code> command now (<code>ls</code> lists the
content of the <code>hello-world</code> directory), the repository might
seem empty; however, adding the <code>-a</code> flag for all files via
<code>ls -a</code> will show all hidden files, which in this case
includes the new hidden directory <code>.git</code>. Flags can simply be
thought of as command line options that can be added to shell
commands.</p>
<p>Note that whenever we use git via the command line, we need to
preface each command (or verb) with <code>git</code>, so that the
computer knows we are trying to get git to do something, rather than
some other program.</p>
</div>
<div class="section level3">
<h3 id="displaying-the-current-projects-status">Displaying the current project’s status<a class="anchor" aria-label="anchor" href="#displaying-the-current-projects-status"></a></h3>
<p>We can run the <code>git status</code> command to display the current
state of a project. Let’s do that now.</p>
<div class="codewrapper sourceCode" id="cb13">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git status</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)</code></pre>
</div>
<p>The output tells us that we are on the main branch (more on this
later) and that we have nothing to commit (no unsaved changes).</p>
</div>
<div class="section level3">
<h3 id="two-steps-adding-and-committing">Two steps: Adding and committing<a class="anchor" aria-label="anchor" href="#two-steps-adding-and-committing"></a></h3>
<p>We will now create and save our first project file. This is a
two-step process. First, we <strong>add</strong> any files for which we
want to save the changes to a staging area, then we
<strong>commit</strong> those changes to the repository. This two-stage
process gives us fine-grained control over what should and should not be
included in a particular commit.</p>
<p>Let’s create a new file using the <code>touch</code> command, which
is a quick way to create an empty file.</p>
<div class="codewrapper sourceCode" id="cb15">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> touch index.md</span></code></pre>
</div>
<p>The <code>.md</code> extension above signifies that we have chosen to
use the Markdown format, a lightweight markup language with plain text
formatting syntax. We will explore Markdown a bit later.</p>
<p>Let’s check the status of our project again.</p>
<div class="codewrapper sourceCode" id="cb16">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git status</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
index.md
nothing added to commit but untracked files present (use "git add" to track)</code></pre>
</div>
<p>This status is telling us that git has noticed a new file in our
directory that we are not yet tracking. With colourised output, the
filename will appear in red. To change this, and to tell Git we want to
track any changes we make to index.md, we use <code>git add</code>.</p>
<div class="codewrapper sourceCode" id="cb18">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git add index.md</span></code></pre>
</div>
<p>This adds our Markdown file to the <strong>staging area</strong> (the
area where git checks for file changes). To confirm this we want to use
<code>git status</code> again.</p>
<div class="codewrapper sourceCode" id="cb19">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git status</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.md</code></pre>
</div>
<p>If we are using colourised output, we will see that the filename has
changed colour (from red to green). Git also tells us that there is a
new file to be committed but, before we do that, let’s add some text to
the file.</p>
<p>We will open the file <code>index.md</code> with any text editor we
have at hand (e.g. Notepad on Windows or TextEdit on Mac OSX) and enter
<code># Hello, world!</code>. The hash character is one way of writing a
header with Markdown. Now, let’s save the file within the text editor
and check if Git has spotted the changes.</p>
<div class="codewrapper sourceCode" id="cb21">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git status</span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.md
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: index.md</code></pre>
</div>
<p>This lets us know that git has indeed spotted the changes to our
file, but that it hasn’t yet staged them, so let’s add the new version
of the file to the staging area.</p>
<div class="codewrapper sourceCode" id="cb23">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git add index.md</span></code></pre>
</div>
<p>Now we are ready to <strong>commit</strong> our first changes. Commit
is similar to ‘saving’ a file to Git. However, compared to saving, a
commit provides a lot more information about the changes we have made,
and this information will remain visible to us later.</p>
<div class="codewrapper sourceCode" id="cb24">
<h3 class="code-label">BASH<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="sourceCode bash" tabindex="0"><code class="sourceCode bash"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="ex">$</span> git commit <span class="at">-m</span> <span class="st">'Add index.md'</span></span></code></pre>
</div>
<div class="codewrapper">
<h3 class="code-label">OUTPUT<i aria-hidden="true" data-feather="chevron-left"></i><i aria-hidden="true" data-feather="chevron-right"></i>
</h3>
<pre class="output" tabindex="0"><code>[main (root-commit) e9e8fd3] Add index.md
1 file changed, 1 insertion(+)
create mode 100644 index.md</code></pre>
</div>
<p>We can see that one file has changed and that we made one insertion,
which was a line with the text ‘#Hello, world!’. We can also see the
commit message ‘Add index.md’, which we added by using the
<code>-m</code> flag after <code>git commit</code>. The commit message
is used to record a short, descriptive, and specific summary of what we
did to help us remember later on without having to look at the actual
changes. If we just run <code>git commit</code> without the
<code>-m</code> option, Git will launch nano (or whatever other editor
we configured as <code>core.editor</code>) so that we can write a longer
message.</p>
<p>Having made a commit, we now have a permanent record of what was
changed, and git has also recorded some additional metadata: who made
the commit (you!) and when the commit was made (timestamp). You are
building a mini-history of your process of working with the files in
this directory.</p>
<div id="more-on-the-staging-area" class="callout callout">
<div class="callout-square">
<i class="callout-icon" data-feather="bell"></i>
</div>
<div id="more-on-the-staging-area" class="callout-inner">
<h3 class="callout-title">More on the Staging Area<a class="anchor" aria-label="anchor" href="#more-on-the-staging-area"></a>
</h3>
<div class="callout-content">
<p>If you think of Git as taking snapshots of changes over the life of a
project, <code>git add</code> specifies <em>what</em> will go in a
snapshot (putting things in the staging area), and
<code>git commit</code> then <em>actually takes</em> the snapshot, and
makes a permanent record of it (as a commit). If you don’t have anything
staged when you type <code>git commit</code>, Git will prompt you to use
<code>git commit -a</code> or <code>git commit --all</code>, which is
kind of like gathering <em>everyone</em> for the picture! However, it’s
almost always better to explicitly add things to the staging area,
because you might commit changes you forgot you made. (Going back to
snapshots, you might get the extra with incomplete makeup walking on the
stage for the snapshot because you used <code>-a</code>!) Try to stage
things manually, or you might find yourself searching for “git undo
commit” more than you would like!</p>
</div>
</div>
</div>
<figure><img src="fig/git-staging-area.svg" alt="The Git Staging Area" class="figure mx-auto d-block"></figure><p>At the moment, our changes are only recorded locally, on our
computer. If we wanted to work collaboratively with someone else they
would have no way of seeing what we’ve done. We will fix that in the
next episode by using GitHub to share our work.</p>
<div id="keypoints1" class="callout keypoints">
<div class="callout-square">
<i class="callout-icon" data-feather="key"></i>
</div>
<div class="callout-inner">
<h3 class="callout-title">Keypoints<a class="anchor" aria-label="anchor" href="#keypoints1"></a>
</h3>
<div class="callout-content">
<ul><li>When you initialize a Git repository in a directory, Git starts
tracking the changes you make inside that directory.</li>
<li>This tracking creates a history of the way the files have changed
over time.</li>
<li>Git uses a two-step process to record changes to your files. Changes
to files must first be added to the staging area, then committed to the
Git repository.</li>
</ul></div>
</div>
</div>
</div>
</div> <!-- / div.lesson-content -->
</main><!-- / main#main-content.main-content --><nav class="bottom-pagination mx-md-4" aria-label="Previous and Next Chapter"><div class="d-block d-sm-block d-md-none">
<a class="chapter-link" href="01-what-is-git.html"><i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>Previous</a>
<a class="chapter-link float-end" href="03-sharing.html">Next<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i></a>
</div>
<!-- content for large screens -->
<div class="d-none d-sm-none d-md-block">
<a class="chapter-link" href="01-what-is-git.html" rel="prev">
<i aria-hidden="true" class="small-arrow" data-feather="arrow-left"></i>
Previous: What is Git/GitHub?
</a>
<a class="chapter-link float-end" href="03-sharing.html" rel="next">
Next: Sharing your work...
<i aria-hidden="true" class="small-arrow" data-feather="arrow-right"></i>
</a>
</div>
</nav></div> <!-- / div.primary-content.col-xs-12 -->
<!-- END: inst/pkgdown/templates/content-instructor.html-->
</div><!--/div.row-->
<footer class="row footer mx-md-3"><hr><div class="col-md-6">
<p>This lesson is subject to the <a href="CODE_OF_CONDUCT.html">Code of Conduct</a></p>
<p>
<a href="https://github.com/librarycarpentry/lc-git/edit/main/episodes/02-getting-started.md" class="external-link">Edit on GitHub</a>
| <a href="https://github.com/librarycarpentry/lc-git/blob/main/CONTRIBUTING.md" class="external-link">Contributing</a>
| <a href="https://github.com/librarycarpentry/lc-git/" class="external-link">Source</a></p>
<p><a href="https://github.com/librarycarpentry/lc-git/blob/main/CITATION" class="external-link">Cite</a> | <a href="mailto:[email protected]">Contact</a> | <a href="https://carpentries.org/about/" class="external-link">About</a></p>
</div>
<div class="col-md-6">
<p>Materials licensed under <a href="LICENSE.html">CC-BY 4.0</a> by the authors</p>
<p><a href="https://creativecommons.org/licenses/by-sa/4.0/" class="external-link">Template licensed under CC-BY 4.0</a> by <a href="https://carpentries.org" class="external-link">The Carpentries</a></p>
<p>Built with <a href="https://github.com/carpentries/sandpaper/tree/0.13.0" class="external-link">sandpaper (0.13.0)</a>,
<a href="https://github.com/carpentries/pegboard/tree/0.6.1" class="external-link">pegboard (0.6.1)</a>,
and <a href="https://github.com/carpentries/varnish/tree/0.3.0" class="external-link">varnish (0.3.0)</a>.</p>
</div>
</footer></div> <!-- / div.container -->
<div id="to-top">
<a href="#top">
<i class="search-icon" data-feather="arrow-up" role="img" aria-label="Back to top"></i><br><span class="d-none d-sm-none d-md-none d-lg-none d-xl-block">Back</span> To Top
</a>
</div>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "TrainingMaterial",
"@id": "https://librarycarpentry.github.io/lc-git/02-getting-started.html",
"dct:conformsTo": "https://bioschemas.org/profiles/TrainingMaterial/1.0-RELEASE",
"description": "A Carpentries Lesson teaching foundational data and coding skills to researchers worldwide",
"keywords": "software, data, lesson, The Carpentries",
"name": "Getting started with Git",
"creativeWorkStatus": "active",
"url": "https://librarycarpentry.github.io/lc-git/02-getting-started.html",
"identifier": "https://librarycarpentry.github.io/lc-git/02-getting-started.html",
"dateCreated": "2018-04-12",
"dateModified": "2023-04-21",
"datePublished": "2023-09-19"
}
</script><script>
feather.replace();
</script><!-- Matomo
2022-11-07: we have gotten a notification that we have an overage for our
tracking and I'm pretty sure this has to do with Workbench usage.
Considering that I am not _currently_ using this tracking because I do not
yet know how to access the data, I am turning this off for now.
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setDomains", ["*.preview.carpentries.org","*.datacarpentry.github.io","*.datacarpentry.org","*.librarycarpentry.github.io","*.librarycarpentry.org","*.swcarpentry.github.io", "*.carpentries.github.io"]]);
_paq.push(["setDoNotTrack", true]);
_paq.push(["disableCookies"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="https://carpentries.matomo.cloud/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='https://cdn.matomo.cloud/carpentries.matomo.cloud/matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
End Matomo Code --></body></html><!-- END: inst/pkgdown/templates/layout.html-->