Git + RStudio Tutorial
Imagine you have a piece of code, and you’re keen on tracking its changes without losing the original version. The conventional method involves saving scripts as new files, often labeled with indicators like ‘v0’ or a timestamp. Git offers a more seamless way to version your code without the hassle of managing different version files manually. It not only tracks changes made to your files but also equips you with tools to document those changes. While Git’s initial development focused on code versioning, it’s versatile enough to handle versioning of smaller datasets. GitHub and GitLab support various text file formats (e.g., csv, fasta), making them ideal for versioning those as well.
2. Setup
If everything went well we are all set for using git in rstudio by following the following instruction manual: rstudio git setup
3. Git Vocabulary
- Repository (repo): The project folder + full history.
- Commit: A snapshot of your work with a message.
- Stage: Select files you want to include in a commit.
- Push: Upload your local commits to the online repo.
- Pull: Download commits from the online repo.
- Remote: The online copy (e.g., GitHub, Gitlab).
- Branch: a parallel workspace for isolated development.
- Merge: combining that workspace back into the main project.
- Merge conflict: When Git can’t auto-merge differences.
4. Creating a Project with Git
Option A: Start a New Project with Git
- File > New Project
- Choose a new or existing directory
- Check Create a git repository
With this option git is enabled. However, git is just used locally.
Option B: Clone an Existing Repository
- File > New Project > Version Control > Git
- Paste the repo URL
- Select a local location and click Create Project
With this option git is enable and able to interact with the remote.
Exercise: Create Your First Repository
- Create an empty remote git repository (github / gitlab).
- Clone the repository using option B.
- Run
renv::init()to add an r project environment - modify the
.gitignorefile and addrenv/library
5. Basic Git Workflow
Now that we have a git setup for a project we can learn how versioning with git works.
5.1 Pull
Click Pull in RStudio to ensure your local files are up‑to‑date. Pull downloads the latest commits from the repo.

5.2 Stage and Commit
- Tick the file(s) to stage
- Use Diff button to inspect changes
- Write a clear message
- Click Commit


5.3 Push
Send your commits to the remote repository.

Exercise 1: Create Your First Commit
- To your previously created git-enabled project
- Add a
codedirectory and create a README.md - Stage → Commit → Push the new quarto document
- Go to GitHub / Gitlab and check the history
Goal: See your commit appear on GitHub / Gitlab.
Exercise 2: Make Multiple Commits
- Add four titles to the README.md
# introduction
# directory structure
# data and code description
# usage - Commit with meaning description of the change
- Add a placeholder text in the subsection “introduction”
- Commit with meaning description of the change
- Add a placeholder table in the subsection “data and code description”
- Commit with meaning description of the change
- Add an image in the subsection “introduction”
- Commit with meaning description of the change
- Push
Goal: Familiarize with commit → push. Learn that multiple commits can be made before changes are pushed.
Exercise 3: View History Online
- Go to GitHub / Gitlab → Commits tab
- Inspect messages and diffs
- Click the directory / folder icon on the right, to explore the repository at different commits.

Goal: Understand how your work appears remotely. Understand that you have a snapshot of your complete repository for every commit.
6. Protecting Secrets
Never commit:
- passwords
- API keys
- database credentials
- full
config.yml
It might happen that you need to access external or private data repositories or databases with credentials. If that is the case, never save your credentials in your r scripts. Instead, you can create a config.yml file in the root directory.
default:
database1:
driver: 'RPostgres::Postgres()'
server: 'server.address.com'
uid: 'username'
pwd: 'password'
port: 5432
APIKEY:
key: 'api-key'
url: 'api.access.point.com'Then, add the config.yml to .gitignore. This ensures that credentials are not shared in scripts.
Use .gitignore:
config.yml
.Renviron
To use the configurations use the config::get() function.
config::get()
db_credentials <- config::get('database1')
uid <- db_credentials$uid # username
pwd <- db_credentials$pw # passwordExercise 4: Add sensitive files to .gitignore
- Create the above
config.ymlfile in the root of the project directory - Check the Git pane and see if the config.yml file appears
- Edit the
.gitignorefile by addingconfig.ymland save the changes - Verify that
config.ymldisappears from the Git pane
Goal: Learn how to keep credentials out of Git. Learn how to use .gitignore.
7. Branches
A branch is a parallel line of development.
Use them for:
- New features
- Experiments
- Bug fixes
Example Workflow
git checkout -b feature-test
git push -u origin feature-test
# work and commit
git checkout main
git merge feature-test
Exercise 5: Create and Merge a Branch
- Create a branch in rstudio
- Change a file
- Commit the changes
- Go online and make a merge
Goal: Understand isolated development.
Exercise 6: Merge Conflict
- Edit the same line differently on two branches
- Try merging
Goal: See conflict markers and resolve them.
9. Daily workflow
Start of the day: pull
Commit and push changes throughout the day
Make “tags” for significant version changes
End of the day: push
Be careful with:If you make changes in the remote (github / gitlab), make sure to first push local changes
If you work with branches, make sure not to make modifications to the branch in which you want to merge (usually “main”)



