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 also create one in the online setup) and adds 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 myprojectname.Rproj file (e.g. DCEcology), as this is a user/computer specific file.

Hence, we can ignore the myprojectname.Rproj 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 ....Proj and select Ignore...
  3. Check if the content of .gitignore is correctly updated and click Save.

Solution

Update .gitignore

Update .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