Using {usethis} to set up .gitignore

Open source GitHub

How to ensure that certain files cannot be accidentally committed to GitHub (or any other version controlled area).

Zoë Turner true

Use .gitignore

.gitignore is used to prevent committing certain files or folders to GitHub and can be set both locally in a project and globally.

Using the {usethis} package a lot of the set up of these files can be done from the RStudio Console command line.

Set up

These slides from Forwards are for package development but are fantastic to follow to get your computer set up with GitHub.

Check the Git connections are set up with:


If Vaccinated: FALSE which it is most likely to be if this is the first time {usethis} was used type:


This adds .DS_Store, .Rproj.user, .Rdata, .Rhistory, and .httr-oauth to the global .gitignore which can be checked with:

usethis::edit_git_ignore(scope = "user")

This open file can be altered directly and saved.

Changing project .gitignore

To view and edit directly:

usethis::edit_git_ignore(scope = "project")

Or use the following to add individual files and folders like secrets/:


Potential issues with networks/VPNs

If git_vaccinate has and error starting with


and then a reference to a lock to a private network drive (usually when off the VPN/Network) then type:


and copy the following code, with <> changed as appropriate:

    email = <Email>
    name = <GitHub Account name>
    excludesFile = C:/Users/<own folder>/.gitignore
    defaultBranch = main

Overwriting a global .gitignore

This was a question that came out of a team Code Review and we found the following blog from Scott Radcliff with the answer that yes, it is possible to overwrite a global setting if this is ever required.

Not tested but examples using the ! to exempt:

.rda and .RData files

We also discussed .rda and .Rdata files which are save R data files. gitvaccinate() adds the .Rdata but doesn’t mention .rda files so be aware of that.

The difference in the types of files are that .Rdata can store single or multiple R objects but .rda can only save single.

It might be worthy of a blog/team review on its own but there are issues with saving multiple R objects to one place if they are updated regularly. It’s very easy to not re-save/update an R object and affect the overall data workflow and so I, particularly as I’ve got into this very muddle, only save one R object per .Rdata file.


If you see mistakes or want to suggest changes, please create an issue on the source repository.


For attribution, please cite this work as

Turner (2022, April 1). CDU data science team blog: Using {usethis} to set up .gitignore. Retrieved from

BibTeX citation

  author = {Turner, Zoë},
  title = {CDU data science team blog: Using {usethis} to set up .gitignore},
  url = {},
  year = {2022}