Introduction to Git
Last updated on 2026-08-10 | Edit this page
Estimated time: 75 minutes
Overview
Questions
- “How do I install and configure Git on my machine?”
- “Why do I need a GitHub account for collaborative workflows?”
- “How do add, status, diff, and commit work together?”
- “How do I inspect what changed and when?”
- “How do branches help me work safely on new changes?”
- “How do I merge branch work back into main?”
Objectives
- “Install Git locally and verify it is available in the terminal.”
- “Create a GitHub account and understand why it is useful for collaboration.”
- “Use core Git commands: add, status, diff, and commit.”
- “Track file changes over time with Git history.”
- “Create and switch branches for feature work.”
- “Merge a feature branch into main.”
Why version control?
Version control helps us keep a reliable history of changes to files. It allows us to:
- Recover earlier versions.
- See who changed what and when.
- Work on new ideas in branches without breaking stable work.
This lesson is a simplified path through key ideas from the Software Carpentry Git novice material.1 It focuses on the minimum skills learners need before collaborative coding and data workflows in later episodes.
If Git is new to you, do not worry about memorising every command immediately. The main goal is to understand the workflow: inspect changes, stage intentionally, and commit meaningful checkpoints.
Core concepts before commands
Before using Git commands, it helps to understand four terms:
- Working directory: the files you are editing right now.
- Staging area: the “next commit” area, where you choose exactly what to record.
-
Repository: the full Git history and metadata
(stored in the
.gitdirectory). - Commit: a named snapshot in history, including author, date, and message.
A beginner mental model:
- Edit in working directory.
- Move selected changes to staging area (
git add). - Save staged snapshot (
git commit).
This two-step save process is one of Git’s strengths because it lets you build clean, logical history.
Install Git locally
Install Git for your operating system using the Carpentries setup page:
After installation, verify in a terminal:
If this prints a version number, Git is installed correctly and available on your terminal path.
If the command is not found, the shell cannot locate Git yet. In that case, complete installation first, then open a new terminal and run the check again.
Set your identity once:
BASH
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Check the values:
These identity settings are written into commit metadata so teammates can see authorship clearly. Use your real name and an email address you want associated with your commits.
If you want to inspect one value only:
Create a GitHub account
Create an account at:
Recommended setup:
- Verify your email.
- Enable two-factor authentication.
- Add a profile name that collaborators can recognise.
For this episode, a GitHub account is mainly needed so learners are ready for remote collaboration in the “Cloud-Native Architectures and Modern Data Formats for Geoscience” course. Git itself works locally without GitHub, but GitHub is the most common place to share repositories, open pull requests, and review changes.
In other words:
- Git tracks versions on your machine.
- GitHub is a hosting and collaboration platform for Git repositories.
Start a repository and track changes
Create a project folder and initialise Git:
git init creates a hidden .git directory
where Git stores project history and metadata.
At this point, your repository has no commits yet. You can verify that with:
which will report that no commits exist.
Create a file and check status:
git status is your main safety command. It tells you
what has changed, what is staged, and what is still untracked.
For beginners, running git status frequently is the best
way to avoid confusion. If you are unsure what state the repository is
in, run git status first.
Stage and commit:
git add places content in the staging area, and
git commit records that staged snapshot in history.
Important detail: git commit only records what is
staged. If you edited a file but did not git add it, that
edit will not be included in the commit.
Check history:
Each commit gets a unique identifier and message, forming a timeline of your project.
The timeline helps with reproducibility: you can always return to a specific commit and understand what changed.
Understand add, status, diff, and commit
These four commands work together as a cycle:
- Edit files in your working directory.
- Inspect changes with
statusanddiff. - Select what to include with
add. - Save a logical checkpoint with
commit.
This helps avoid accidental commits and makes history easier to read.
Think of staging as preparing slides for a presentation: you choose what belongs in this specific story.
Edit the file and inspect changes:
git diff shows unstaged edits line by line, so you can
review before staging.
In git diff output:
- Lines with
-were removed. - Lines with
+were added. - Context lines are shown to help you locate the change.
Stage then inspect staged changes:
git diff --staged confirms exactly what will be
committed next.
This is especially useful before large commits, when it is easy to accidentally stage too much.
Commit staged work:
Good commit messages are short, specific, and action-oriented.
Useful message pattern:
- Start with a verb:
Add,Update,Fix,Refactor. - Describe what changed, not what you did while changing it.
- Keep the first line concise.
Working pattern:
- Edit files.
- Check
git status. - Review with
git diff. - Stage with
git add. - Commit with a clear message.
Exercise 1: First tracked change
- Create a file
workflow.txt. - Add one line describing your workflow.
- Stage and commit the file.
- Confirm the commit appears in the log.
Track changes over time
Tracking changes is one of Git’s biggest advantages for research and engineering work. You can always inspect how a file evolved and recover context for decisions.
This is valuable in scientific work, where you may need to explain how a figure, table, or derived dataset was produced weeks later.
Show what changed between the working tree and last commit:
Show history compactly:
--decorate shows branch and HEAD labels, helping you see
where you are in history.
Show a commit and its patch:
By default, git show displays the most recent commit and
its exact patch. You can also show a specific commit with:
This is the core of change tracking: you can inspect both timeline and content deltas.
Create and use branches
A branch is an independent line of development. Using branches means
you can experiment safely without destabilising main.
In collaborative projects, a common pattern is one branch per feature
or fix. This keeps main stable and reviewable.
Create a feature branch and switch to it:
This creates the branch and moves you to it in one step.
You can list branches at any time with:
The current branch is marked with *.
Make a change and commit it:
BASH
echo "Branch-specific update" >> notes.txt
git add notes.txt
git commit -m "Add branch-specific update"
Switch back to main:
At this point, main does not yet include branch-only
commits until you merge.
This separation is intentional: it allows testing and review before integration.
Merge a branch into main
Merging combines histories. In simple cases Git performs a fast-forward merge automatically; if the same lines changed in both branches, you may need to resolve a conflict manually.
If conflicts occur, Git will pause the merge and mark affected files. Typical workflow is:
- Open conflicted files and edit to the final desired version.
- Stage resolved files with
git add. - Complete merge with
git commit(if required).
Merge your feature branch:
Verify history now includes merged work:
If the project uses master instead of main,
replace main in the commands accordingly.
Many modern repositories default to main, but older
repositories may still use master.
Exercise 2: Branch and merge
- Create a new branch
add-reference. - Add one new line to
notes.txtand commit. - Switch back to
main. - Merge
add-referenceintomain. - Confirm the merged history with
git log --oneline --graph.
- “Install Git locally and configure your identity before starting a project.”
- “Create a GitHub account so you are ready for collaboration and remote repositories.”
- “
git status,git diff,git add, andgit commitform the core local workflow.” - “Git history (
git log,git show) lets you track what changed and when.” - “Branches isolate work; merging brings reviewed changes back into
main.” - “The staging area is central to Git: it lets you build clean, intentional commits.”
- “Frequent
git statuschecks help you stay oriented and avoid mistakes.”
Software Carpentry. Version Control with Git. https://swcarpentry.github.io/git-novice/↩︎