Ignore files from version control
Overview
Teaching: 5 min
Exercises: 5 minQuestions
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
- Go to
git
pane (the tab that says Git),- Right click on
.Rhistory
and selectIgnore...
- Check if the content of
.gitignore
is correctly updated and clickSave
.Solution
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:
- Sensitive information (passwords,…)!
- Binary files. Git works very will with text files (any type of text), but not with binary files such as
.Rdata
. - Files > 50MB. Git is specifically made for code (e.g.
.R
) and does not intend to track all changes in large data files. - A temp/ folder inside your project with ‘disposable’ content, e.g.
draft
,test
ortemp
. Sometimes such a subfolder is convenient, but should not contain anything crucial for your project. - In any programming language, some files are derivatives
Ignore all files inside a folder
- Create a folder
plots/
and add an image to the folder.- Right click on
plots/
and selectIgnore...
- Check if the content of
.gitignore
is correctly updated and clickSave
.
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