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
2022-04-01

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:

usethis::git_sitrep()

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

usethis::git_vaccinate()

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/:

usethis::use_git_ignore("secrets/")

Potential issues with networks/VPNs

If git_vaccinate has and error starting with

libgit2::git_config_set_string

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

usethis::edit_git_config()

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

[user]
    email = <Email>
    name = <GitHub Account name>
[core]
    excludesFile = C:/Users/<own folder>/.gitignore
[init]
    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.

Corrections

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

Citation

For attribution, please cite this work as

Turner (2022, April 1). CDU data science team blog: Using {usethis} to set up .gitignore. Retrieved from https://cdu-data-science-team.github.io/team-blog/posts/2022-04-01-using-usethis-to-set-up-gitignore/

BibTeX citation

@misc{turner2022using,
  author = {Turner, Zoë},
  title = {CDU data science team blog: Using {usethis} to set up .gitignore},
  url = {https://cdu-data-science-team.github.io/team-blog/posts/2022-04-01-using-usethis-to-set-up-gitignore/},
  year = {2022}
}