Why Version Control?
- Version control records every meaningful change to your project.
- Git’s three-step workflow is: edit → stage → commit.
- Version control facilitates collaboration, provenance, and backup.
Configuring Git
-
git config
writes and reads Git’s preference files. - Three levels exist: system, global, local; the closest level wins.
- Setting your name and email globally ensures commits are attributed correctly.
- Colour, editor, and autocorrect options improve usability.
Recording Changes Locally
-
git init
turns a directory into a repository by creating.git
. -
git status
gives you a report on your git repository. - Stage files with
git add
; record them permanently withgit commit -m
. - Use
git diff
to inspect unstaged or staged changes. -
git log
reveals the history; hashes uniquely identify commits. - Empty directories are not tracked—add a file such
as
.gitkeep
. -
git restore
returns a file to a known state.
Working with Remote Repositories
- A remote is a named reference to a repository hosted elsewhere.
-
git clone URL
copies a remote repository and sets up origin. - Publish an existing repo with
git remote add
followed bygit push -u origin <branch>
. -
git pull
equalsgit fetch
+git merge
; use--rebase
to avoid unnecessary merges. - A push may be rejected if your branch is behind; pull (or fetch & merge) then push again.
Using Git with a Graphical User Interface RStudio
- Git GUIs offer a visual way to use Git—perfect for beginners and for visualising changes.
- RStudio’s Git integration supports cloning, staging, committing, and pushing.
- Always configure your name/email before making your first commit.
- You can verify success by checking your changes online.
Collaborating
- Git allows multiple collaboration models; shared access is fastest but requires trust.
- GitLab permissions and protected branches must be configured for collaborators.
- Always pull before pushing to avoid rejection.
- Merge conflicts are normal and can be resolved with manual editing.
Exploring History
-
git log
is used to inspect the commit history, with options for concise, graphical, or filtered views. -
git show
displays the details and changes of a specific commit or the content of a file at a past revision. -
git diff
compares changes between the working directory, staging area, and different commits. -
git restore
can discard unstaged changes, unstage changes, or restore files from previous commits. - RStudio provides a graphical interface for exploring Git history.
-
git blame
helps identify who last modified each line of a file.
Ignoring Things
- Use
.gitignore
to prevent tracking of unnecessary or sensitive files - Edit
.gitignore
early in your project to keep your history clean - Use
.git/info/exclude
for machine-specific ignores - Files already tracked by Git won’t be ignored retroactively