Git with RStudio - ices-taf/doc GitHub Wiki

Create an RStudio Project with Git tracking

In RStudio you can create different projects depending on the purpose and state of your project, you have these options to choose from:

1. New Directory - creates a new project.

Click on it and you will be asked what type of project you want to start (New Project, R Package or Shiny application).

  • Click on File > New Project > New Directory > New Project
  • Insert the Directory name (project name) and choose where to store it.
  • For Git to track this project, tick the “Create a Git repository” box and click “Create Project”.

2. Existing Directory - creates an R project within an existing folder.

This is useful for example if you already have some data files in a folder and want to analyse them in the same folder. The other option is to create a new R project as explained above and simply move your data files to that folder.

  • Click on File > New Project > Existing Directory
  • Insert the project folder location on your computer and press “Create Project”.
  • To get Git to track this project go to the Terminal (flap) in RStudio and type:
    • git init this is to initiate Git.
    • git add <file> or git add --all with this you can list the file(s) you want Git to track or track all files.
    • git commit commits all the above-mentioned files to be tracked by Git.

3. Version Control – creates an R project on your local computer from an existing online stored repository

This is what you typically set up if you need to collaborate with others.

  • Click on File > New Project > Version Control > Git.
  • Enter the repository URL (e.g., from GitHub) or create a new one.
  • Choose a project directory on your local machine.
  • Click "Create Project".

After setting up your project

When you have created a project in RStudio you can now work on that project locally. Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot, as well as any newly staged files; they can be unmodified, modified, or staged. In short, tracked files are files that Git knows about. Untracked files are everything else — any files in your working directory that were not in your last snapshot and are not in your staging area (when you commit the files they are staged).

When you first clone a repository, all your files will be tracked and unmodified because Git just checked them out and you haven’t edited anything. As you edit the files, Git sees them as modified, because you’ve changed them since your last commit. As you work, you selectively stage these modified files and then commit all those staged changes, and the cycle repeats. When you save the changes you have made (making sure they have been tracked by Git), you will first need to Commit these changes (staged). If your project is on GitHub (especially if you are collaborating with others), you will need to Pull to see if your local project (repository) differs from the one online. If this is the case, then you can Commit together with a “commit message” explaining the modifications/additions and then you need to Push these changes to the online repository. If you are not working with GitHub, you will still need to Commit these changes for Git to track them.

⚠️ **GitHub.com Fallback** ⚠️