Ignore files from version control

Overview

Teaching: 5 min
Exercises: 5 min
Questions
  • How to exclude files and folders from version control

Objectives
  • Exclude Rproj files, draft folders and sensitive information from version control

.gitignore

When starting a new project in RStudio, it will always add a file .gitignore if it does not already exists (you can actually create one in the online setup) and add some initial files to ignore. A .gitignore file enlists all those files that should not be taken into account by Git (not part of the snapshot). An example is the .Rhistory file, as this file will save your temporary tests, variables, etc.

Hence, we can ignore the .Rhistory file by adding the file to the .gitignore text-file. We can do this inside Rstudio.

Ignore a file

  1. Go to git pane (the tab that says Git),
  2. Right click on .Rhistory and select Ignore...
  3. Check if the content of .gitignore is correctly updated and click Save.

Solution

Update .gitignore

Update .gitignore

The website https://www.gitignore.io from TopTal can help you figure out what files to ignore, look at the R example .gitignore.

Some other examples of files you probably want to ignore:

Ignore all files inside a folder

  1. Create a folder plots/ and add an image to the folder.
  2. Right click on plots/ and select Ignore...
  3. Check if the content of .gitignore is correctly updated and click Save.

Use wildcards to ignore multiple files

Note that you can use wild cards e.g. *.Rproj to exclude a group of files from the version control.

Key Points

  • Adding file names or folder names to .gitignore excludes them from version control