<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/version-control-file-sharing-and-collaboration-using-github-and-rstudio/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://geraldbelton.com/version-control-file-sharing-and-collaboration-using-github-and-rstudio/"> R – Gerald Belton</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>This is Part 3 of our “Getting Started with R Programming” series. For previous articles in the series, click here: <a target="_blank" rel="nofollow" href="http://geraldbelton.com/getting-started-with-r-a-beginners-guide-part-1/">Part 1</a>, <a target="_blank" rel="nofollow" href="http://geraldbelton.com/getting-started-with-r-a-beginners-guide-part-2/">Part 2</a>.</em></p>
<p>This week, we are going to talk about using git and GitHub with RStudio to manage your projects.</p>
<p>Git is a version control system, originally designed to help software developers work together on big projects. Git works with a set of files, which it calls a “repository,” to manage changes in a controlled manner. Git also works with websites like <a target="_blank" rel="nofollow" href="https://github.com/">GitHub</a>, <a target="_blank" rel="nofollow" href="https://about.gitlab.com/">GitLab</a>, and <a target="_blank" rel="nofollow" href="https://bitbucket.org/">BitBucket</a>, to provide a home for your git-based projects on the internet.</p>
<p>If you are a hobbyist, and aren’t working on projects with other programmers, why would you want to bother with any of this? Incorporating version control into your workflow might be more trouble than its worth, if you never have to collaborate with others, or share your files with others. But most of us will, eventually, need to do this. It’s a lot easier to do if it’s built into your workflow from the start.</p>
<p>More importantly, there are tremendous advantages to using the web-based sites like GitHub. At the very minimum, GitHub serves as an off-site backup for your precious program files.</p>
<hr>
<figure class="wp-caption"><a target="_blank" rel="nofollow" href="https://www.amazon.com/gp/product/B01LOLJS74/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=B01LOLJS74&linkCode=as2&tag=gerald09-20&linkId=7b49c35bbac12d996dd621e56dbe533e"><img height="234" width="250" src="http://ws-na.amazon-adsystem.com/widgets/q?_encoding=UTF8&MarketPlace=US&ASIN=B01LOLJS74&ServiceVersion=20070822&ID=AsinImage&WS=1&Format=_SL250_&tag=gerald09-20" border="0"></a><br>
<figcaption class="wp-caption-text"><em>Full disclosure: This is an affiliate link. If you click this link and buy this shirt, Amazon pays me.</em></figcaption>
</figure>
<hr>
<p>In addition, GitHub makes it easy to share your files with others. GitHub users can fork or clone your repository. People who don’t have GitHub accounts can still browse your shared files online, and even download the entire repository as a zip file.</p>
<p>And finally, once you learn Markdown (which we will be doing here, very soon) you can easily create a webpage for your project, hosted on GitHub, at no cost. This is most commonly used for documentation, but it’s a simple and easy way to get on the web. Just last week, I met a young programmer who showed me his portfolio, hosted on GitHub.</p>
<p>OK, let’s get started!</p>
<h2>Register a GitHub Account</h2>
<p>First, register a free GitHub account: <a target="_blank" rel="nofollow" href="https://github.com/">https://github.com</a>. For now, just use the free service. You can upgrade to a paid account, create private repositories, join organizations, and other things, later. But one thing you should think about at the very beginning is your username. I would suggest using some variant of your real name. You’ll want something that you feel comfortable revealing to a future potential employer. Also consider that things change; don’t include your current employer, school, or organization as part of your user name.</p>
<p>If you’ve been following along in this series, you’ve already installed R and R Studio. Otherwise, you should do that now. Instructions are in <a target="_blank" rel="nofollow" href="http://geraldbelton.com/getting-started-with-r-a-beginners-guide-part-1/">Part 1</a> of this series.</p>
<h2><strong>Installing and Configuring Git</strong></h2>
<p>Next, you’ll need to install git. If you are a Windows user, install <a target="_blank" rel="nofollow" href="https://git-for-windows.github.io/">Git for Windows</a>. Just click on the link and follow the instructions. Accept any default settings that are offered during installation. This will install git in a standard location, which makes it easy for RStudio to find it. And it installs a BASH shell, which is a way to use git from a command line. This may come in handy if you want to use git outside of R/RStudio.</p>
<p>LINUX users can install git through their distro’s package manager. Mac users can install git from <a target="_blank" rel="nofollow" href="https://git-scm.com/downloads">https://git-scm.com/downloads</a>.</p>
<p>Now let’s tell git who you are. Go to a command prompt (or, in R Studio, go to <em>Tools > Shell</em>) and type:</p>
<pre>git config --global user.name 'Your Name'</pre>
<p>For Your Name, substitute your own name, of course. You could use your GitHub user name, or your actual first and last name. It should be something recognizable to your collaborators, as your commits will be tagged with this name.</p>
<pre>git config --global user.email 'you@whatever.com'</pre>
<p>The email address you put here <strong>must be</strong> the same one you used when you signed up for GitHub.</p>
<p>To make sure this worked, type:</p>
<pre>git config --global --list</pre>
<p>and you should see your name and email address in the output.</p>
<h2>Connect Git, GitHub, and RStudio</h2>
<p>Let’s run through an exercise to make sure you can pull from, and push to, GitHub from your computer.</p>
<p>Go to <a target="_blank" rel="nofollow" href="https://github.com/">https://github.com</a> and make sure you are logged in. Then click the green “New Repository” button. Give your repository a name. You can call it whatever you want, we are going to delete this shortly. For demonstration purposes, I’m calling mine “demo.” You have the option of adding a description. You should click the checkbox that says “Initialize this repository with a README.” Then click the green “Create Repository” button. You’ve created your first repository!</p>
<p>Click the green “Clone or download” button, and copy the URL to your clipboard. Go to the shell again, and take note of what directory you are in. I’m going to create my repository in a directory called “tmp,” so at the command prompt I typed “mkdir ~/tmp” followed by “cd ~/tmp”.</p>
<p>To clone the repository on your local computer, type “git clone” followed by the url you copied from GitHub. The results should look something like this:</p>
<pre>geral@DESKTOP-0HM18A3 MINGW64 ~/tmp
$ git clone https://github.com/gbelton/demo.git
Cloning into 'demo'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.

</pre>
<p>Make this your working directory,  list its files, look at the README file, and check how it is connected to GitHub. It should look something like this:</p>
<pre>geral@DESKTOP-0HM18A3 MINGW64 ~/tmp
$ cd demo

geral@DESKTOP-0HM18A3 MINGW64 ~/tmp/demo (master)
$ ls
README.md

geral@DESKTOP-0HM18A3 MINGW64 ~/tmp/demo (master)
$ head README.md
# demo
geral@DESKTOP-0HM18A3 MINGW64 ~/tmp/demo (master)
$ git remote show origin
* remote origin
  Fetch URL: https://github.com/gbelton/demo.git
  Push URL: https://github.com/gbelton/demo.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

</pre>
<p>Let’s make a change to a file on your local computer, and push that change to GitHub.</p>
<pre>echo &quot;This is a new line I wrote on my computer&quot; >> README.md

git status</pre>
<p>And you should see something like this:</p>
<pre>$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
 (use &quot;git add &lt;file>...&quot; to update what will be committed)
 (use &quot;git checkout -- &lt;file>...&quot; to discard changes in working directory)

 modified: README.md

no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)
</pre>
<p>Now commit the changes, and push them to GitHub:</p>
<pre>git add -A
git commit -m &quot;A commit from my local computer&quot;
git push</pre>
<p>Git will ask you for your GitHub username and password if you are a new user. Provide them when asked.</p>
<p>The -m flag on the commit is important. If you don’t include it, git will prompt you for it. You should include a message that will tell others (or yourself, months from now) what you are changing with this commit.</p>
<p>Now go back to your browser, and refresh. You should see the line you added to your README file. If you click on commits, you should see the one with the message “My first commit from my local computer.”</p>
<p>Now let’s clean up. You can delete the repository on your local computer just by deleting the directory, as you would any other directory on your computer. On GitHub, (assuming you are still on your repository page) click on “settings.” Scroll down until you see the red “Danger Zone” flag, and click on “Delete This Repository.” Then follow the prompts.</p>
<h2>Connecting GitHub to RStudio</h2>
<p>We are going to repeat what we did above, but this time we are going to do it using RStudio.</p>
<p>Once again, go to GitHub, click “New Repository,” give it a name, check the box to create a README, and create the repository. Click the “clone or download” button and copy the URL to your clipboard.</p>
<p>In RStudio, start a new project: <em>File > New Project > Version Control > Git</em></p>
<p>In the “Repository URL” box, paste in the URL that you copied from GitHub. Put something (maybe “demo”) in the box for the Directory Name. Check the box marked “Open in New Session.” Then click the “Create Project” button.</p>
<p>And, just that easy, you’ve cloned your repository!</p>
<p>In the file pane of RStudio, click README.md, and it should open in the editor pane. Add a line, perhaps one that says “This line was added in R Studio.” Click the disk icon to save the file.</p>
<p>Now we will commit the changes and push them to GitHub. In the upper right pane, click the “Git” tab. Click the “staged” box next to README.md. Click “Commit” and a new box will pop up. It shows you the staged file, and at the bottom of the box you can see exactly what changes you have made. Type a commit message in the box at the top right, something like “Changes from R Studio.” Click the commit button. ANOTHER box pops up, showing the progress of the commit. Close it after it finishes. Then click “Push.” ANOTHER box pops up, showing you the progress of your push. It may ask you for a user name and password. When it’s finished, close it. Now go back to GitHub in your web browser, refresh, and you should see your changed README file.</p>
<p>Congratulations, you are now set up to use git and GitHub in R Studio!</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://geraldbelton.com/version-control-file-sharing-and-collaboration-using-github-and-rstudio/"> R – Gerald Belton</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/hJXKFE8-8M4"></a></div>