-
Notifications
You must be signed in to change notification settings - Fork 3
/
Git-RStudio.Rmd
133 lines (89 loc) · 7.01 KB
/
Git-RStudio.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
---
title: "Notes on Git and RStudio"
output:
html_document:
toc: true
include:
after_body: footer.html
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Using Git in RStudio
You can read a whole book about Git and R [Happy Git with R](http://happygitwithr.com/index.html).
Using Git in RStudio can be hard to set-up and hard to debug if you run into problems. If you want to use it, [read these instructions](https://cfss.uchicago.edu/git07.html).
In particular, read the part about how to set-up so you don't have to enter your username and password for every commmit.
## Creating a local repository and then getting it on GitHub (or GitLab)
Note. Creating the repo locally first often leads to misery in my experience. Often safer to start by making a blank repo on GitHub or GitLab.
### Let's create a local repository
1. Create a RStudio project (upper right with the blue cube with R). New project... > New Directory > New Project
2. Check the box that says `Create a git repository`. You have made a local repository.
3. Add a file. File > New File > R Script
4. Type something and then save the file.
5. Click the `Git` tab in the upper right.
6. Click the changes that you want to commit (do all). And click `Commit`. Add a comment: first line is subject, newline, description (options).
7. Repeat 4-6 a few times.
8. Now look at the history. The little clock-like icon (or History in the Git window).
* Click on `filter by file` to see just the changes to one file.
* Do a search in the upper right.
* Click on an early commit and click `View file @...`
## Now let's get this on GitHub (similar strategy if you are working on GitLab)
1. Go to GitHub and create a repo with the same name
2. Don't select add Readme or anything
3. A page that tells you how to get started should show up. Look for this code.
**…or push an existing repository from the command line**
```
git remote add origin https://github.com/<youraccount>/<reponame>.git
git push -u origin master
```
4. Copy the code and go back to RStudio.
5. Click the cog on the Git tab and click Shell
6. Paste the code into the command line.
7. If you see something about Keychain, try Deny. It will hopefully ask you for your GitHub account info on the command line.
8. If it all works, you should now be able to push.
## Methods for downloading repositories
If you want to copy someone's repository on GitHub or make a copy of one of your own, there are a few approaches that you can take. I am going to use `https://github.com/RVerse-Tutorials/Test` as the repository that you want to copy and we will put the repository at `Documents/GitHub/Test`.
### Method #1 Fork it
1. Navigate to `https://github.com/RVerse-Tutorials/Test` on GitHub and click the 'fork' button in the upper right. Follow the instructions.
Now you need to get it on your computer.
2. From RStudio, go to the menu option
'File->New Project...'. Then from the resulting dialog, choose
"Version Control". Then choose "Git". Then it asks for a "repository URL".
Supply this: `https://github.com/<your github account>/Test` and
leave the "Project Directory Name" empty. And then choose a directory
in which to put the new repository and click OK.
If you are using GitHub Desktop, you need to add it to that too.
3. From GitHub Desktop, go to the menu option
'File->Add Local Repository'. Then navigate to `~/Documents/GitHub/Test` and add.
The problem with forking is that you can't fork your own repositories and if you fork someone else's repository, your fork will always show theirs as a 'parent'---which doesn't really have a bad effect but I rarely want my copy of someone's repo to be linked to theirs in any way. Normally forking is used when you are contributing to the parent repository and need a copy to work on.
### Method #2 Copying my OWN repos (not someone else's)
This method is pretty failsafe and I find easy to remember. This how I normally copy one of my own folders and make a new GitHub repo. I don't use Git at the command line very often and rather than looking up how to do it, getting something wrong 3x and then researching how to debug the problem, and then finding I've spent the morning with a simple task.... I just do the following now.
1. Make a new repo (say `Test`) on GitHub and click the button to add a Readme.md file.
2. Download and link to the new repo using Step 2 above (in Method #1).
3. Add the repo to GitHub Desktop using Step 3 above (in Method #1)
4. Copy the files (on my computer) from the repo I want to copy into the new folder. Note this works because I already have the files I want on my computer.
5. Push these changes to GitHub, though the Git tab in RStudio or GitHub Desktop (my preference).
### Method #4
1. From RStudio, go to the menu option
'File->New Project...'. Then from the resulting dialog, choose
"Version Control". Then choose "Git". Then it asks for a "repository URL".
Supply this: `https://github.com/RVerse-Tutorials/Test` and
leave the "Project Directory Name" empty. And then choose a directory
in which to put it and click OK.
This method will have your new repository associated with the repository from where you downloaded. If it is not your own GitHub repository, you don't want this because you will not be able to push changes to GitHub.
2. Click on the 'More' link in the Git Window of RStudio, and click 'Shell'. Then issue this command.
```
git remote rm origin
```
This detaches the cloned repository from the remote repository on GitHub from where you cloned it.
3. Add the new repository to GitHub Desktop and then publish to GitHub. From GitHub Desktop, go to File>Add Local Repository and then navigate to `~/Documents/GitHub/Test` and add.
4. Publish to GitHub. Once you do step 3, a 'Publish to GitHub' button will appear. Click that to publish to GitHub.
Note, there are Git commands you could use to do steps 3 and 4, but in this workshop we are using GitHub Desktop.
### Alternate Method #5
Another way is to download the repository as a zip file and unzip. [Watch a video that shows you how to do this](https://youtu.be/bYySjso7vRo)
Go to https://github.com/RVerse-Tutorials/Test and click 'Clone or download' and chose 'Download Zip'. Unzip and you'll probably want to remove 'master' added to the end of the repository name.
If you chose, 'Clone in Desktop', you'll need to open a terminal window, navigate to the new folder you just downloaded, and run the git code `git remote rm origin` from within the terminal to detach the repository from the RVerse-Tutorials GitHub account.
## Packages with C++ code
We will not be doing this in this short course, but often you will want to install packages with C++ code. To do that, you will need Rtools (Windows) or Xcode (Mac)
* **Rtools** Windows users [Rtools](https://cran.r-project.org/bin/windows/Rtools/) also so you can build packages with C++ code if needed. See comments here about changing path. [Rtools](http://stat545.com/packages01_system-prep.html#windows-system-prep)
* **Xcode** Mac users Open terminal and type the following command `xcode-select --install`