git config --global user.name "Your Name"
git config --global user.email "you@example.com"15 Appendix: Git in 10 Minutes for RStudio Users
You already know the shell — Git is the natural next step for version-controlling R projects. Everything below also maps to RStudio’s Git pane (Tools -> Version Control).
15.1 Configure once
15.2 The 6-command workflow
| Step | Shell | RStudio Git pane |
|---|---|---|
| 1. Check state | git status |
Diff / status list |
| 2. Stage | git add file.R (or git add -u) |
Tick the Staged box |
| 3. Commit | git commit -m "feat: short message" |
Commit button |
| 4. Sync down | git pull |
Pull button (blue down arrow) |
| 5. Sync up | git push |
Push button (green up arrow) |
| 6. History | git log --oneline -10 |
History button |
15.3 Branches
git branch feature # create
git switch feature # move to it
git switch -c hotfix # create + move in one step
git switch master # back to production branchRule of thumb from this book’s workflow: do work on a feature branch, keep master deployable, and push only at the end of a phase.
15.4 Ignore files
Create a .gitignore so generated files never get committed:
cat .gitignore.Rhistory
.RData
cline/
docs/
This book’s own .gitignore keeps the cline/ working directory and rendered docs/ output out of version control — the same pattern works for your R projects (data/, output/, .Rhistory).
15.5 Undo safely
- Wrong stage:
git restore --staged file.R - Wrong edit (file still uncommitted):
git restore file.R - Never rewrite history on shared branches — prefer a new commit that fixes the mistake.
15.6 Exercises
- In a scratch folder, run
git init, createnotes.R, thengit status,git add notes.R,git commit -m "docs: first notes", andgit log --oneline. You should see exactly one commit. - Edit
notes.R, rungit status(file shows as modified), stage and commit again.git log --onelineshould now show two commits. - Add a
.gitignorecontaining*.html, createreport.html, and rungit status— the HTML file should not appear. Remove it withrm report.html.