Preparation
Step 1: Create a RStudio Project
Create a new RStudio Project File > New Project > New Directory > New Project. This will create a RStudio Project without Git version control (and consequently without a Github connection). This is a typical situation for a project that you started without version control in mind. Choose the following settings:
- Directory name: Choose a directory name that suits your structure
- Create project as a subdirectory of: Choose a parent directory that suits your folder structure
- Create a git repository: not checked (we will do this manually in the next step)
- Use renv with this project: not checked
Step 2: Activate Git Version Control
Activating Git Version Control for your project is one line of code. In your shell terminal, type the following command:
# explicitly specify the branch name to be "main",
# this leads to less confusion later on
git init -b main
You should get a message, saying Initialized empty Git repository in C:/path/to/your/directory/.git/
. You will see this folder (named .git
) in your project’s root directory (check your “Files” pane ). If you don’t see it there, click Refresh file listing (refresh symbol to the very right of the files pane). If you still don’t see it, make hidden files visible (Files pane > More > Show hidden files)
To see the “Git” Pane in RStudio, reload RStudio either by restarting it or clicking on the name of your RStudio project in the top right corner of RStudio and selecting your project from the project list).
Step 3: Create a Github Repository
Now create a Github Repository following the instructions from last week. This time however, don’t check Add a README file
.
Copy the https URL to your Github repo, which should look something like this: https://github.com/GITHUB-USERNAME/cma-week4.git
Report the URL of your new repo back to us via Moodle!
Step 4: Connect to Github
To connect your (local) RStudio Project to Github, we have to set up our Github repo to be our so called “remote” repository. We could have multiple remotes, which is why we need to name it, and the convention is to call it origin
. To create a remote named origin
, type the following command in your shell terminal:
git remote add origin https://github.com/GITHUB-USERNAME/cma-week4.git
Now, before you can push anything to the remote repository, you need to commit something first. Last week, you used the Git Pane from RStudio to commit your files. Since we are already in the terminal, let’s use the terminal for this and in this way get to know git a little bit better.
There is one little that we glanced over till now. Before committing a file, you selected them first. This process is called adding to the staging area. To add your cma-week4.Rproj
to your git repo, type the following command:
# Adjust the filename accordingly
git add cma-week4.Rproj
To commit this file, type the following command
# The text after -m is your commit message
git commit -m "my initial commit"
The first time we push to this remote repository, we need to specify the an upstream, so that future git push
will be directed to the correct remote branch. We can to this with the --set-upstream
(or -u
)
Assuming your branch is called main
, type the following command. You can check the name of your branch with command git branch
(the default branch name is either main
or master
).
git push --set-upstream origin main
This command prints a couple of messages, ending with the following statement: Branch 'main' set up to track remote branch 'main' from 'origin'.
. Now that the upstream (i.e.) tracking branch is correctly set up, you can also push via the Git pane in RStudio (you might need to refresh the Git pane first).