16 Existing project, GitHub first
This is a novice-friendly workflow for bringing an existing R project into the RStudio and Git/GitHub universe.
We do this in a slightly goofy way, in order to avoid using Git at the command line. You won’t want to work this way forever, but it’s perfectly fine as you’re getting started! At first, the main goal is to accumulate some experience and momentum. There is nothing goofy about the GitHub repo that this creates, it is completely standard. Transition to a more elegant process when you’re ready.
We assume you’ve got your existing R project isolated in a directory on your computer. If that’s not already true, make it so. Create a directory and marshal all the existing data and R scripts there. It doesn’t really matter where you do this, but note where the project currently lives.
16.1 Make a repo on GitHub
Go to https://github.com and make sure you are logged in.
Near “Repositories”, click the big green “New” button. Or, if you are on your own profile page, click on “Repositories”, then click the big green “New” button.
How to fill this in:
- Repository template: No template.
- Repository name:
myrepo
or a similarly short name for this existing project. Approach this similar to a variable name, in code: descriptive but brief, no whitespace. Letters, digits,-
,.
, or_
are allowed. - Description: “Analysis of the stuff” or any short description of the project. Write this for humans.
- Public.
- Initialize this repository with: Add a README file.
Click the big green button that says “Create repository”.
Now click the big green button that says “<> Code”.
Copy a clone URL to your clipboard. If you’re taking our default advice, copy the HTTPS URL. But if you’re opting for SSH, then make sure to copy the SSH URL.
16.2 New RStudio Project via git clone
In RStudio, start a new Project:
-
File > New Project > Version Control > Git. In the “repository URL” paste the URL of your new GitHub repository. It will be something like this
https://github.com/jennybc/myrepo.git
. - Be intentional about where you create this Project.
- I suggest you “Open in new session”.
- Click “Create Project” to create a new directory, which will be all of these things:
- a directory or “folder” on your computer
- a Git repository, linked to a remote GitHub repository
- an RStudio Project
- In the absence of other constraints, I suggest that all of your R projects have exactly this set-up.
This should download the README.md
file that we created on GitHub in the previous step.
Look in RStudio’s file browser pane for the README.md
file.
Behind the scenes, RStudio has done this for you:
git clone https://github.com/jennybc/myrepo.git
16.3 Bring your existing project over
Using your favorite method of moving or copying files, copy the files that constitute your existing project into the directory for this new project.
In RStudio, consult the Git pane and the file browser.
- Are you seeing all the files? They should be here if your move/copy was successful.
- Are they showing up in the Git pane with questions marks? They should be appearing as new untracked files.
16.4 Stage and commit
Commit your files to this repo. How?
- Click the “Git” tab in upper right pane
- Check the “Staged” box for all files that you want to commit.
- Default: stage it.
- When to reconsider: this will all go to GitHub. Consider if that is appropriate for each file. You can absolutely keep a file locally, without committing it to the Git repo and sending to GitHub. Just let it sit there in your Git pane, without being staged. No harm will be done. If this is a long-term situation, list the file in
.gitignore
.
- If you’re not already in the Git pop-up, click “Commit”
- Type a message in “Commit message”, such as “Init project XYZ”.
- Click “Commit”
16.5 Push your local changes to GitHub
Click the green “Push” button to send your local changes to GitHub. RStudio will display something like:
>>> /usr/bin/git push origin HEAD:refs/heads/main
To https://github.com/jennybc/myrepo.git
3a2171f..6d58539 HEAD -> main
16.6 Confirm the local change propagated to the GitHub remote
Go back to the browser. I assume we’re still viewing your new GitHub repo.
Refresh.
You should see all the project files you committed there.
If you click on “commits,” you should see one with the message you used, e.g. “Init project XYZ”.
16.7 The end
Now just “lather, rinse, repeat”. Do work somewhere: locally or on GitHub. Commit it. Push it or pull it, depending on where you did the work, but get local and remote “synced up”. Repeat.
Note that in general (and especially in future when collaborating with other developers) you will usually need to pull changes from the remote (GitHub) before pushing the local changes you have made. For this reason, it’s a good idea to try and get into the habit of pulling before you attempt to push.