forked from seankross/the-unix-workbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-Git-and-GitHub.Rmd
1651 lines (1322 loc) · 52.7 KB
/
06-Git-and-GitHub.Rmd
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
1000
# Git and GitHub
> Proof rather than argument. - Japanese proverb
## What are Git and GitHub?
Git is a command line program which allows you to track versions of any code or
plain text documents that you create. Like the "track changes" feature of
a word processor Git keeps track of who made particular changes, the time and
date of those changes, and where the changes were made. If a critical file gets
deleted by accident, or if you make a breaking change to your code and you want
to try to figure out where the breaking change was made, you can use Git to
restore the deleted file or find the new bug in your program. Git organizes
groups of files that you're tracking into a **repository**, which is just a
directory where all of the changes to files in that directory are tracked. Git
can also help you collaborate with others when you're writing software. As
[Karl Broman](https://twitter.com/kwbroman) says
(paraphrasing [Mark Holder](https://twitter.com/mtholder)):
"Your closest collaborator is you six months ago, but you don’t reply to
emails."
GitHub is a website that provides remote Git repositories. A remote repository
is just a Git repository that you're able to access over an internet connection.
GitHub allows you to create public remote repositories for free, and anyone can
see your code in these public repositories. If you want to keep your code private
then you can pay GitHub for private remote repositories.
If you're working
on code together with a friend GitHub can help you sync changes to code files
between you and your friend. There's also a social and community aspect to
GitHub, since you can watch other programmers develop their projects. GitHub
also makes it easy to jump in and help somebody with their project. GitHub
offers many other useful features which we will discuss at length.
## Setting Up Git and GitHub
Before setting up Git, go to [GitHub](https://github.com/) and create a free
account. Take note of which email address you use and which username you choose.
To see if you have Git installed open up your terminal and enter the following:
```{r, engine='bash'}
git --version
```
If you don't get a response back telling you the version of Git that you have
installed then you need to install Git. You can find instructions for installing
Git on your operating system
[here](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git).
Open up your shell once you have git installed and run `git --version` again to
make sure that installation succeeded (you may need to restart your shell or
your computer). After Git is installed we need to set up two environmental
variables, but we only need to do this once. The first variable we need to set
up with Git is your GitHub user name, and the second variable is the email
address that you used to create your GitHub account:
```{r, engine='bash', eval=FALSE}
git config --global user.name "myUserName"
git config --global user.email [email protected]
```
## Getting Started with Git
Let's create our first Git repository. First we need to create a directory:
```{r, engine='bash', eval=FALSE}
cd
mkdir my-first-repo
cd my-first-repo
```
"Repo" in this case is just shorthand for "repository." To start tracking files
with Git in a directory enter `git init` into the command line:
```{r, engine='bash', eval=FALSE}
git init
```
```
## Initialized empty Git repository in /Users/sean/my-first-repo/.git/
```
You've just created your first repository! Now let's create a file and start
tracking it.
```{r, engine='bash', eval=FALSE}
echo "Welcome to My First Repo" > readme.txt
```
Now that we've created a file in this Git repository, let's use `git status` to
see what's going on in this repository. We'll be using `git status` continuously
throughout this chapter in order to get information about the status of this Git
repository.
```{r, engine='bash', eval=FALSE}
git status
```
```
## On branch master
##
## Initial commit
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## readme.txt
##
## nothing added to commit but untracked files present (use "git add" to track)
```
As you can see `readme.txt` is listed as an untracked file. In order to let Git
know that you want to track this file we need to use `git add` with the name of
the file that we want to track. Let's start tracking `readme.txt`:
```{r, engine='bash', eval=FALSE}
git add readme.txt
```
Git now knows to track any changes to `readme.txt`. Let's see how the status of
the repository has changed:
```{r, engine='bash', eval=FALSE}
git status
```
```
## On branch master
##
## Initial commit
##
## Changes to be committed:
## (use "git rm --cached <file>..." to unstage)
##
## new file: readme.txt
##
```
Git is now tracking `readme.txt`, or in Git-specific language `readme.txt` is
now **staged**. Between the parentheses in the message above you can see that
`git status` is giving us a tip about how to unstage (or un-track) this file,
which we could do with `git rm --cached readme.txt`. Let's unstage this file
just to see what happens:
```{r, engine='bash', eval=FALSE}
git rm --cached readme.txt
```
```
## rm 'readme.txt'
```
```{r, engine='bash', eval=FALSE}
git status
```
```
## On branch master
##
## Initial commit
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## readme.txt
##
```
Our repository is right back to the way it started with `readme.txt` as an
unstaged file. Let's start tracking `readme.txt` again so we can move on to
cooler Git features.
```{r, engine='bash', eval=FALSE}
git add readme.txt
```
Now that Git is tracking `readme.txt` we need to create a milestone to indicate
the changes that we made to `readme.txt`. In this case, the changes that we made
were creating the file in the first place! This milestone is called a **commit**
in Git. A commit logs the content of all of the currently staged files. Right
now we only have `readme.txt` staged so let's commit the creation of this file.
When making a Git commit, we need to write a commit message which is specified
after the `-m` flag. The message should briefly describe what changes you've
made since the last commit.
```{r, engine='bash', eval=FALSE}
git commit -m "added readme.txt"
```
```
## [master (root-commit) 73e53ca] added readme.txt
## 1 file changed, 1 insertion(+)
## create mode 100644 readme.txt
```
The message above confirms that the commit succeeded and it summarizes the
changes that took place since the last commit. As you can see in the message we
only changed one file, and we only changed one line in that file. Let's run
`git status` again to see the state of our repository after we've made the first
commit:
```{r, engine='bash', eval=FALSE}
git status
```
```
## On branch master
## nothing to commit, working tree clean
```
All of the changes to the files in this repository have been committed! Let's
add a few more files to this repository and commit them.
```{r, engine='bash', eval=FALSE}
touch file1.txt
touch fil2.txt
ls
```
```
## file1.txt
## fil2.txt
## readme.txt
```
While we're at it let's also add a new line of text to `readme.txt`:
```{r, engine='bash', eval=FALSE}
echo "Learning Git is going well so far." >> readme.txt
```
Now that we've added two more files and we've made changes to one file let's
take a look at the state of this repository.
```{r, engine='bash', eval=FALSE}
git status
```
```
## 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: readme.txt
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## fil2.txt
## file1.txt
##
## no changes added to commit (use "git add" and/or "git commit -a")
```
We can see that Git has detected that one file has been modified, and that there
are two files in this directory that it is not tracking. Now we need to tell
Git to track the changes to these files. We could tell Git to track changes to
each file using `git add`, or since all of the files in this repository are
`.txt` files we could use a wildcard and enter `git add *.txt` into the
console. However if we want to track all of the changes to all of the files in
our directory we should use the command `git add -A`.
```{r, engine='bash', eval=FALSE}
git add -A
git status
```
```
## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: fil2.txt
## new file: file1.txt
## modified: readme.txt
##
```
Now the changes to all of the files in this repository are being tracked.
Finally let's commit these changes:
```{r, engine='bash', eval=FALSE}
git commit -m "added two files"
```
```
## [master 53a1983] added two files
## 3 files changed, 1 insertion(+)
## create mode 100644 fil2.txt
## create mode 100644 file1.txt
```
Darn it, now looking at this commit summary I realize that I have a typo in one
of the names of my files! Thankfully we can undo the most recent commit with
the command `git reset --soft HEAD~`:
```{r, engine='bash', eval=FALSE}
git reset --soft HEAD~
git status
```
```
## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: fil2.txt
## new file: file1.txt
## modified: readme.txt
##
```
This repo is now in that exact same state it was right before we made the
commit. Now we can rename `fil2.txt` to `file2.txt`, then let's look at the
status of the repository again.
```{r, engine='bash', eval=FALSE}
mv fil2.txt file2.txt
git status
```
```
## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: fil2.txt
## new file: file1.txt
## modified: readme.txt
##
## Changes not staged for commit:
## (use "git add/rm <file>..." to update what will be committed)
## (use "git checkout -- <file>..." to discard changes in working directory)
##
## deleted: fil2.txt
##
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## file2.txt
##
```
We previously told Git to track `fil2.txt`, and we can see that Git acknowledges
that the file has been deleted. We can bring Git up to speed with what files it
should be tracking with `git add -A`:
```{r, engine='bash', eval=FALSE}
git add -A
git status
```
```
## On branch master
## Changes to be committed:
## (use "git reset HEAD <file>..." to unstage)
##
## new file: file1.txt
## new file: file2.txt
## modified: readme.txt
##
```
Finally we got the file names right! Now let's make the correct commit:
```{r, engine='bash', eval=FALSE}
git commit -m "added two files"
```
```
## [master 12bb9f5] added two files
## 3 files changed, 1 insertion(+)
## create mode 100644 file1.txt
## create mode 100644 file2.txt
```
That looks much better.
### Summary
- Git tracks changes to plain text files (code files and text documents).
- A directory where changes to files are tracked by Git is called a Git
repository.
- Change your working directory, then run `git init` to start a repository.
- You can track changes to a file using `git add [names of files]`.
- You can create a milestone about the state of your files using `git commit -m "message about changes since the last commit"`.
- To examine the state of files in your repository use `git status`.
### Exercises
1. Start a repository in a new directory.
2. Create a new file in your new Git repository. Make sure Git is tracking the
file and then create a new commit.
3. Make changes to the file, and then commit these changes.
4. Add two new files to your repository, but only commit one of them. What is
the status of your repository after the commit?
5. Undo the last commit, add the untracked file, and redo the commit.
## Important Git Features
### Getting Help, Logs, and Diffs
Git commands have their own `man` pages. You can access them with
`git help [name of command]`. For example here's the start of the help page
for `git status`:
```{r, engine='bash', eval=FALSE}
git help status
```
```
GIT-STATUS(1) Git Manual GIT-STATUS(1)
NAME
git-status - Show the working tree status
SYNOPSIS
git status [<options>...] [--] [<pathspec>...]
DESCRIPTION
Displays paths that have differences between the index file and the current HEAD commit,
paths that have differences between the working tree and the index file, and paths in the
working tree that are not tracked by Git (and are not ignored by gitignore(5)). The first
are what you would commit by running git commit; the second and third are what you could
commit by running git add before running git commit.
```
Just like any other help page that uses `less`, you can return to the prompt
with the `Q` key.
If you want to see a list of your Git commits, enter `git log` into the console:
```{r, engine='bash', eval=FALSE}
git log
```
```
## commit 12bb9f53b10c9b720dac8441e8624370e4e071b6
## Author: seankross <[email protected]>
## Date: Fri Apr 21 15:23:59 2017 -0400
##
## added two files
##
## commit 73e53cae75301ce9b2802107b1956447241bb17a
## Author: seankross <[email protected]>
## Date: Thu Apr 20 14:15:26 2017 -0400
##
## added readme.txt
```
If you've made many commits to a repository you might need to press the `Q` key
in order to get back to the prompt. Each commit has its time, date, and commit
message recorded, along with a SHA-1 hash that uniquely identifies the commit.
Git can also help show the differences between unstaged changes to your files
compared to the last commit. Let's add a new line of text to `readme.txt`:
```{r, engine='bash', eval=FALSE}
echo "The third line." >> readme.txt
git diff readme.txt
```
```
## diff --git a/readme.txt b/readme.txt
## index b965f6a..a3db358 100644
## --- a/readme.txt
## +++ b/readme.txt
## @@ -1,2 +1,3 @@
## Welcome to My First Repo
## Learning Git is going well so far.
## +I added a line.
```
As you can see a plus sign shows up next to the added line. Now let's open up
this file in a text editor so we can delete the second line.
```{r, engine='bash', eval=FALSE}
nano readme.txt
# Delete the second line
git diff readme.txt
```
```
## diff --git a/readme.txt b/readme.txt
## index b965f6a..e173fdf 100644
## --- a/readme.txt
## +++ b/readme.txt
## @@ -1,2 +1,2 @@
## Welcome to My First Repo
## -Learning Git is going well so far.
## +I added a line.
```
A minus sign appears next to the line we deleted. Let's take a look at the
status of our directory at this point.
```{r, engine='bash', eval=FALSE}
git status
```
```
## 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: readme.txt
##
## no changes added to commit (use "git add" and/or "git commit -a")
```
If you read the results from `git status` carefully you can see that we can take
this repository in one of two directions at this point. We can either `git add`
the files we've made changes to in order to track those changes, or we can
use `git checkout` in order to remove all of the changes we've made to a file
to restore its content to what was present in the last commit. Let's remove
our changes to see how this works.
```{r, engine='bash', eval=FALSE}
cat readme.txt
```
```
## Welcome to My First Repo
## I added a line.
```
```{r, engine='bash', eval=FALSE}
git checkout readme.txt
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
```
And as you can see the changes we made to `readme.txt` have been undone.
### Ignoring Files
Sometimes you might have files that you never want Git to track, for example
binary files that are generated as by-products of running code (PDFs or images),
or secrets like passwords or API keys. A file in your Git repository called
`.gitignore` can list names of files and sub-folders, or simple regular
expressions (whatever you can use with `ls`) in order to specify files which
should never be tracked. Each line of a `.gitignore` file should specify a file
or group of files that should not be tracked by Git. Let's make a `.gitignore`
file to make sure that we never track image files in this repository:
```{r, engine='bash', eval=FALSE}
touch toby.jpg
git status
```
```
## On branch master
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## toby.jpg
##
## nothing added to commit but untracked files present (use "git add" to track)
```
Now that we've added an image to our repository, let's add a `.gitignore` file
to make sure Git doesn't track these kinds of files.
```{r, engine='bash', eval=FALSE}
echo "*.jpg" > .gitignore
git status
```
```
## On branch master
## Untracked files:
## (use "git add <file>..." to include in what will be committed)
##
## .gitignore
##
## nothing added to commit but untracked files present (use "git add" to track)
```
Now we can see that Git has detected the new `.gitignore` file, but it doesn't
see `toby.jpg`. Let's add and commit our `.gitignore` file:
```{r, engine='bash', eval=FALSE}
git add -A
git commit -m "added gitignore"
```
```
## [master adef548] added gitignore
## 1 file changed, 1 insertion(+)
## create mode 100644 .gitignore
```
Now if we add another `.jpg` file, Git will not see the file:
```{r, engine='bash', eval=FALSE}
touch bernie.jpg
git status
```
```
## On branch master
## nothing to commit, working tree clean
```
```{r, engine='bash', eval=FALSE}
ls
```
```
## bernie.jpg
## toby.jpg
## file1.txt
## file2.txt
## readme.txt
```
### Summary
- `git help` allows you to read the `man` pages for specific Git commands.
- `git log` will show you your commit history.
- `git diff` displays what has changed between the last commit and your current
untracked changes.
- You can specify a `.gitignore` file in order to tell Git not to track certain
files.
### Exercises
1. Look at the help pages for `git log` and `git diff`.
2. Add to the `.gitignore` you already started to include a specific file name,
then add that file to your repository.
3. Create a file that contains the Git log for this repository. Use `grep` to
see which day of the week most of the commits occurred on.
## Branching
Branching is one of the most powerful features that Git offers. Creating
different Git branches allows you to work on a particular feature or set of
files independently from other "copies" of a repository. That way you and a
friend can work on different parts of the same file on different branches, and
then Git can help you elegantly merge your branches and changes together.
You can list all of the available branches with the command `git branch`:
```{r, engine='bash', eval=FALSE}
git branch
```
```
## * master
```
The star (`*`) indicates which branch you're currently on. The default branch
that is created is always called *master*. Usually people use this branch as the
working version of the software that they are writing, while they develop new and
potentially unstable features on other branches.
To add a branch we'll also use the `git branch` command, followed the name of
the branch we want to create:
```{r, engine='bash', eval=FALSE}
git branch my-new-feature
```
Now let's enter `git branch` again to confirm that we've created the branch:
```{r, engine='bash', eval=FALSE}
git branch
```
```
## * master
## my-new-feature
```
We can make `my-new-feature` the current branch using `git checkout` with the
name of the branch:
```{r, engine='bash', eval=FALSE}
git checkout my-new-feature
```
```
## Switched to branch 'my-new-feature'
```
```{r, engine='bash', eval=FALSE}
git branch
```
```
## master
## * my-new-feature
```
If we look at `git status` we can also see that it will tell us which branch
we're on:
```{r, engine='bash', eval=FALSE}
git status
```
```
On branch my-new-feature
nothing to commit, working tree clean
```
We can switch back to the `master` branch using `git checkout`:
```{r, engine='bash', eval=FALSE}
git checkout master
```
```
## Switched to branch 'master'
```
```{r, engine='bash', eval=FALSE}
git branch
```
```
## * master
## my-new-feature
```
Now we can delete a branch by using the `-d` flag with `git branch` and the name
of the branch we want to delete:
```{r, engine='bash', eval=FALSE}
git branch -d my-new-feature
```
```
## Deleted branch my-new-feature (was adef548).
```
```{r, engine='bash', eval=FALSE}
git branch
```
```
## * master
```
Let's create a new branch for adding a section to the `readme.txt` in our
repository. We can create a new branch and switch to that branch at the same
time using the command `git checkout -b` and the name of the new branch we want
to create:
```{r, engine='bash', eval=FALSE}
git checkout -b update-readme
```
```
## Switched to a new branch 'update-readme'
```
Now that we've created and switched to a new branch, let's make some changes to
a file. As you might be expecting right now we'll add a new line to
`readme.txt`:
```{r, engine='bash', eval=FALSE}
echo "I added this line in the update-readme branch." >> readme.txt
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
```
Now that we've added a new line let's commit these changes:
```{r, engine='bash', eval=FALSE}
git add -A
git commit -m "added a third line to readme.txt"
```
```
## [update-readme 6e378a9] added a third line to readme.txt
## 1 file changed, 1 insertion(+)
```
Now that we've made a commit on the `update-readme` branch, let's switch back to the
`master` branch, and then we'll take a look at `readme.txt`:
```{r, engine='bash', eval=FALSE}
git checkout master
```
```
## Switched to branch 'master'
```
Now that we're on the `master` branch let's quickly glance at `readme.txt`:
```{r, engine='bash', eval=FALSE}
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
```
The third line that we added is gone! Don't fret, the line that we added isn't
gone forever. We committed the change to this file while we were on the
`update-readme` branch, so the updated file is safely in that branch. Let's
switch back to that branch just to make sure:
```{r, engine='bash', eval=FALSE}
git checkout update-readme
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
```
And the third line is back! Let's add and commit yet another line while we're
on this branch:
```{r, engine='bash', eval=FALSE}
echo "It's sunny outside today." >> readme.txt
git add -A
git commit -m "added weather info"
```
```
## [update-readme d7946e9] added weather info
## 1 file changed, 1 insertion(+)
```
This is a small example of how to use Git branching, but you can see how you
can make incremental edits to plain text (usually code files) without
affecting the `master` branch (the tested and working copy of your software)
and without affecting any other branches. You can imagine how
this system could be used for multiple people to work on the same codebase at
the same time, or how you could develop and test multiple software features
without them interfering with each other. Now that we've made a couple of
changes to `readme.txt`, let's combine those changes with what we have in the
`master` branch. This is made possible by a Git **merge**. Merging allows you
to elegantly combine the changes that have been made between two branches. Let's
merge the changes we made in the `update-readme` branch with the `master`
branch. Git incorporates other branches into the current branch by default.
When you're merging, the current branch is also called the **base** branch.
Let's switch to the `master` branch so we can merge in the changes from the
`update-readme` branch:
```{r, engine='bash', eval=FALSE}
git checkout master
```
```
## Switched to branch 'master'
```
To merge in the changes from another branch we need to use `git merge` and the
name of the branch:
```{r, engine='bash', eval=FALSE}
git merge update-readme
```
```
## Updating adef548..d7946e9
## Fast-forward
## readme.txt | 2 ++
## 1 file changed, 2 insertions(+)
```
```{r, engine='bash', eval=FALSE}
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's sunny outside today.
```
It looks like you've merged your first branch in Git! Branching is part of what
makes Git so powerful since it enables parallel developments on the same code
base. But what if there are two commits in two separate branches that make
different edits to the same line of text? When this occurs it is called a
**conflict**. Let's create a conflict so we can learn how they can be resolved.
First we'll switch to the `update-readme` branch. Use `nano` to edit the last
line of `readme.txt`, then commit your changes:
```{r, engine='bash', eval=FALSE}
git checkout update-readme
nano readme.txt
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's cloudy outside today.
```
Notice that we changed "sunny" to "cloudy" in the last line.
```{r, engine='bash', eval=FALSE}
git add -A
git commit -m "changed sunny to cloudy"
```
Now that our changes are committed on the `update-readme` branch, let's switch
back to `master`:
```{r, engine='bash', eval=FALSE}
git checkout master
```
Let's change the same line of code using `nano`:
```{r, engine='bash', eval=FALSE}
nano readme.txt
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## It's windy outside today.
```
Now let's commit these changes:
```{r, engine='bash', eval=FALSE}
git add -A
git commit -m "changed sunny to windy"
```
We've now created two commits that directly conflict with each other. On the
`update-readme` branch the last line says `It's cloudy outside today.`, while
on the `master` branch the last line says `It's windy outside today.`. Let's
see what happens when we try to merge `update-readme` into `master`.
```{r, engine='bash', eval=FALSE}
git merge update-readme
```
```
## Auto-merging readme.txt
## CONFLICT (content): Merge conflict in readme.txt
## Automatic merge failed; fix conflicts and then commit the result.
```
Uh-oh, there's a conflict! Let's check the status of the repo right now:
```{r, engine='bash', eval=FALSE}
git status
```
```
## On branch master
## You have unmerged paths.
## (fix conflicts and run "git commit")
## (use "git merge --abort" to abort the merge)
##
## Unmerged paths:
## (use "git add <file>..." to mark resolution)
##
## both modified: readme.txt
##
## no changes added to commit (use "git add" and/or "git commit -a")
```
If you're getting used to reading the result of `git status`, you can see that
it often offers suggestions about what steps you should take next. Git is
indicating that both versions of readme.txt have modified the same text. Let's
take a look at `readme.txt` to see what's going on there:
```{r, engine='bash', eval=FALSE}
cat readme.txt
```
```
## Welcome to My First Repo
## Learning Git is going well so far.
## I added this line in the update-readme branch.
## <<<<<<< HEAD
## It's windy outside today.