-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-tutorial.Rmd
333 lines (214 loc) · 8.16 KB
/
git-tutorial.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
---
title: "Using Git and GitHub"
subtitle: "A Gentle Introduction"
date: '`r format(Sys.Date(), "%d %B %Y")`'
footer: "An Gentle Introduction to Using Git and GitHub"
output:
slidy_presentation:
smart: no
fig_caption: yes
graphics: yes
---
# Overview
```{r setup, echo=FALSE}
knitr::opts_chunk$set(echo = FALSE)
shiny::includeCSS("css/slidy-flatly.css")
shiny::includeScript("js/jquery.min.js")
shiny::includeScript("js/jkf-scroll.js")
```
## This presentation answers the following questions
- What are Git and GitHub?
- What are version control systems?
- Why should someone use a version control system?
- What is a centralized version control system (CVCS)?
- What is a decentralized (aka distributed) version control system (DVCS)?
- How do I install and configure Git?
- What are the main Git commands and what do these commands do?
- What's inside the `.git` directory?
- What are the different ways to interact with Git?
- What is a Git remote and how do I add one to a repo?
- How do I get started with Github?
- How do I get started using Git and Github?
+ From the command line
+ Within the RStudio IDE
# Overview of Git & Github
## What is Git?
- A <u>decentralized<u/> version control system that's free and open source
- An excellent way to track and coordinate file changes among many people
- A tool invented by Linus Torvalds (and the Linux development community) to develop the Linux kernel in 2005
## Is Git hard to learn?
- New Git users often struggle to understand how to use Git?
- This misunderstanding is driven by the fact that there seems to be an endless number of use cases
- Git's not hard to learn -
+ There are problem
- Before
## What's a decentralized version control system?
<center>
<img src="Git.png" style="width: 400px; background-color: black;">
</center>
# What is GitHub?
- A web-based service for hosting Git repositories
- The single largest host for Git repositories
- A popular way to collaborate on code (no more emails and zip files!!)
+ The development versions of many R packages are hosted on GitHub: https://github.com/tidyverse/ggplot2
+ All the [cool kids](https://github.com/auburngrads) are using it!
<!--##
+ what is Git?
+ Installing Git on your machine
+ basic commands
+ setting up a Git repo on a local machine
- GitHub
- what is GitHub?
- signing up
- connecting your local Git repo to GitHub
- selecting an awesome avatar will not be covered
# Git
-->
## Why Should You use Git/GitHub?
## Git enables a faster, easier workflow
<img src="before-version-control.png" style="float: right; padding: 0px 0px 0px 5px; width:250px;">
<ul style="display:inherit;">
- A typical workflow
* File renaming
* Absurd folder structures
* Zipped folders
* E-mailing files back and forth
* Lost files
* Shared directories (one-at-a-time)
* Ever try merging MS Word files with tracked changes?!
</ul>
## Your workflow - <font color='red'><u>without</u></font> Git
<center></center>
## Your workflow - <font color='green'><u>with</u></font> Git
<center></center>
## Using Git
## Using Git
- There several Methods for using Git
- At the **command line**
- Within an IDE (e.g. RStudio)
- From one of several [Git desktop GUI](https://git-scm.com/downloads/guis)
- <p class='hover'>Super secret hybrid method</p>
## Which method should you choose?
- At the **command line**
+ Simplest (really!) & fastest
+ Works the same on Windows, Mac, and Linux
- Within an IDE (e.g. RStudio)
+ Convenient when building R packages
+ Can be clunky to update many files
- From a Git [desktop GUI](https://git-scm.com/downloads/guis)
## Installing Git
## Installing Git (Mac/linux/unix)
- For Mac, Linux, and Unix users
+ Download Git from https://git-scm.com/downloads.
+ Accept the installation defaults for your particular operating system
## Installing Git (windows)
- For Windows users
+ Download Git from https://git-scm.com/downloads.
+ Accept the installation defaults __EXCEPT__ Choose Use Git Bash
<img src="images/win-git.png" width='50%' style="border: 0px; box-shadow: 0;">
## Setting Git up for the first time
- After Git has installed
+ You need modify the global configuration file to tell Git who you are.
+ Open a terminal or Git Bash (Windows) and enter the following two lines: (with the obvious modifications)
```{shell, echo=TRUE}
$ git config --global user.name "Brandon Greenwell"
$ git config --global user.email greenwell.brandon@gmail.com
```
- Since you passed the option `--global`, you only need to do this once!
## The basic process
- Initialize Git version tracking in a directory
- Add or edit files in the directory
- Check the status of file changes in the directory
- Add changed files to the staging area
- Commit one or changed files to be updated
- Push commits to the master
## The basic commands you'll use/need
- `cd` (change directories)
- `git init` (initialize a folder as a Git repo)
- `git add <file-name>` (start tracking a new file)
- `git commit -m "fix typo"` (commit your changes)
- `git clone` (clone/copy another Git repo [e.g., from GitHub])
- `git push origin master` (push your master branch to your origin server)
- `git pull origin master` (update your local repo)
**Tip\:** Typing `git add --all` will start tracking everything (this is how I almost always use `git add`).
## Using Git commands
- Initialize a repo in an existing directory
+ Create a new folder called `arithmetic` anywhere on your computer (e.g., the Desktop)
+ In this folder, create a new R script file called `add.R`
+ In this new file add the following code
```{r, echo=TRUE}
add <- function(x, y) { x * y }
```
## Initializing a repo in an existing directory
- Next, open a terminal or Git Bash (Windows) and go to the project's root directory
- For example, the code below takes me to the folder called `arithmetic` on my Desktop
```{shell, echo=TRUE}
cd C:/Users/greenweb/Desktop/arithmetic
```
- Then, to initialize this as a Git repo, just type
```{shell, echo=T}
git init
```
**Tip\:** the command line has a history (just like the R console), so you don't need to type as often; just hit the up arrow and make any necessary changes!
## Initializing a repo in an existing directory
If you want to start version controlling all the files, you need to start tracking them. In the terminal, type the following:
```shell
git add --all
```
then type:
```shell
git commit -m "first commit"
```
## How to use Git
* Free book on the Git website: https://git-scm.com/book/en/v2
```{r, out.width = "150px"}
knitr::include_graphics("progit2.png")
```
# GitHub
```{r, out.width = "600px"}
knitr::include_graphics("Octocat.png")
```
## Account setup and configuration
The first thing you need to do is set up an account: https://github.com/
```{r, out.width = "400px"}
knitr::include_graphics("signup.png")
```
**Use the same e-mail address you used when setting up Git!**
## Set up your profile
```{r, out.width = "700px"}
knitr::include_graphics("my-profile.png")
```
## Creating a new repo
```{r, out.width = "600px"}
knitr::include_graphics("new-repo.png")
```
## Creating a new repo
```{r, out.width = "600px"}
knitr::include_graphics("new-repo-2.png")
```
## Creating a new repo
```{r, out.width = "700px"}
knitr::include_graphics("new-repo-3.png")
```
## Creating a new repo
```{r, out.width = "700px"}
knitr::include_graphics("new-repo-4.png")
```
## Creating a new repo
Refresh your browswer and see if it worked!
```{r, out.width = "700px"}
knitr::include_graphics("new-repo-5.png")
```
## Making changes and pushing them to GitHub
Fix the obvious typo in `add.R`. Then, take a snapshot (i.e., `git add/commit`) and push the changes to GitHub (i.e., `git push`)!
```shell
git add --all
git commit -m "fix typo"
git push origin master
```
Refresh your browser to see the changes.
**Tip\:** It's good practice to do this every time you make a key change (e.g., fix a typo, add a new function to an R script, etc.).
\* Make sure you're in the right directory!
## All done!
<center></center>
**Any questions?**