Version control and Git basics¶
What is version control?¶
Version control is the set of processes and systems used to manage changes to files, programs, and directories over time.
Without it, versions are usually managed by hand, with copies:
flowchart LR
a["inventory.csv"] --> b["inventory_v2.csv"] --> c["inventory_final.csv"] --> d["inventory_final_REALLY.csv"]
Which copy is the right one? What changed between v2 and final, and why? Nobody remembers. A version control system keeps one set of files and records each change you choose to save, with a description, an author, and a date:
%%{init: {"gitGraph": {"showBranches": false, "rotateCommitLabel": false}, "themeVariables": {"git0": "#43a047", "gitInv0": "#ffffff", "tagLabelBackground": "#e8f5e9", "tagLabelColor": "#1b5e20", "tagLabelBorder": "#2e7d32", "commitLabelFontSize": "13px"}}}%%
gitGraph TB:
commit id: "Create inventory"
commit id: "Add NAS"
commit id: "Add web server"
commit id: "Add media server"
You always work on a single inventory.csv, and every earlier version is still available.
What should be version controlled?¶
Version control is useful for anything that:
- changes over time, such as source code, scripts, documentation, configuration files, or analysis notebooks;
- needs to be shared, so that several people can work on the same files without overwriting each other's work.
It is not limited to code. Any project whose files evolve, or that several people work on, benefits from it.
What can version control do?¶
| Capability | What it means in practice |
|---|---|
| Track files in different states | Know which files are new, modified, or ready to be recorded |
| Combine different versions of files | Merge work done separately, for example by two colleagues, into a single version |
| Identify a particular version | Refer to the exact state of the project at a given point, for example "the configuration that was running on Monday" |
| Revert changes | Go back to an earlier version when a change turns out to be wrong |
Why is version control important?¶
Two everyday situations show why:
- Iterating on something. You try a change, keep what works, and need to know what changed between attempts, like a cook adjusting a recipe or an administrator tuning a configuration file.
- Finding and fixing bugs. When something breaks, the history shows which change introduced the problem, when, and by whom, and lets you go back to a working version.
Without version control, you depend on manual copies and memory. With it, every recorded change is kept, labelled, and recoverable.
Introducing Git¶
Git is a version control system. It is:
- popular: widely used for software development, infrastructure, and data projects;
- open source: free to use, with source code that anyone can inspect;
- scalable: it handles small personal projects as well as very large projects with many contributors.
Git is not GitHub
Git is the version control tool that runs on your computer. GitHub is an online platform that hosts Git repositories and adds collaboration features such as pull requests and issues. Everything in this part of the notes uses Git only and works without an Internet connection or a GitHub account.
Benefits of Git¶
- Git keeps every recorded version. Each commit stays in the repository history.
- You can compare files at different times to see exactly which lines changed.
- You can see what changes were made, by whom, and when. Every recorded change carries an author, a date, and a message.
- You can revert to previous versions of files when a change needs to be undone.
Only committed work is protected
Git protects the changes you have committed (recorded). Edits that were never committed are not part of the history, and some commands can discard them. This is covered in Restoring and reverting files.
Using Git from the shell¶
Git commands are run in the shell, also called the terminal. The shell is a program for executing commands, and it can also be used to quickly preview or inspect files and directories.
In the shell, the word directory means the same thing as folder in a graphical file manager. For example, Documents and homelab are both directories.
Useful terminal commands¶
| Command | Function | Syntax |
|---|---|---|
pwd |
Print working directory: show where you are | pwd |
ls |
List the files and directories in the current directory | ls [directory] |
cd |
Change directory: move to another directory | cd <directory> |
Example session:
$ pwd
/home/sam/Documents
$ ls
archive invoices.csv notes.md todo.md
$ cd archive
$ pwd
/home/sam/Documents/archive
pwdshows that the current directory is/home/sam/Documents.lslists its content: anarchivedirectory and three files.cd archivemoves into thearchivesubdirectory. Runningpwdagain confirms the new location.
A few forms of cd that you will use constantly:
| Command | Effect |
|---|---|
cd archive |
Go into the archive subdirectory (relative path) |
cd .. |
Go up one level, to the parent directory |
cd ~ or cd |
Go to your home directory |
cd /home/sam/Documents |
Go to an absolute path |
Tip
ls hides names starting with a dot, such as the .git directory you will meet on the next page. Use ls -a to show them.
Checking the Git version¶
Before using Git, check that it is installed and see which version you have:
$ git --version
git version 2.55.0
The version number depends on your installation; any recent version works for these notes. If the shell answers git: command not found, Git is not installed.
Hands-on labs¶
Three labs, from guided to more autonomous. Each one has its own setup, so you can do them in any order and repeat them as often as you like. Type the commands rather than pasting them, and read every output before moving on: the goal is to build reflexes, not to reach the end.
Lab 1: find your way around the shell¶
Objective: use pwd, ls, and cd to explore and navigate a small directory structure, then check your Git version.
Prerequisites and initial state: a Linux shell with Git installed. Start in your home directory. The lab only creates files under ~/git-practice/shell-basics.
Setup (copy and run as is):
mkdir -p ~/git-practice/shell-basics/Documents/archive
cd ~/git-practice/shell-basics/Documents
touch invoices.csv notes.md todo.md archive/invoices_2025.csv
cd ~
Tasks:
- Move into
~/git-practice/shell-basics/Documentsand print the full path of your current directory. - List the content of this directory.
- Move into the
archivesubdirectory and list its content. - Go back up one level without typing the full path, and check where you are.
- Display your Git version.
Expected result and verification:
- Step 1 prints a path ending in
/git-practice/shell-basics/Documents. - Step 2 lists
archive,invoices.csv,notes.md, andtodo.md. - Step 3 lists
invoices_2025.csv. - After step 4,
pwdprints theDocumentspath again. - Step 5 prints a line starting with
git version.
Solution
cd ~/git-practice/shell-basics/Documents
pwd # /home/<you>/git-practice/shell-basics/Documents
ls # archive invoices.csv notes.md todo.md
cd archive
ls # invoices_2025.csv
cd .. # ".." is the parent directory
pwd # back to .../shell-basics/Documents
git --version
cdaccepts a path relative to the current directory (archive) or an absolute path (~/git-practice/..., where~is your home directory).cd ..always moves to the parent directory, wherever you are.git --versionis a good first check on any machine: if it fails, nothing else in these notes will work.
Clean up when you are done: rm -rf ~/git-practice/shell-basics.
Lab 2: version control without Git¶
Objective: feel what managing versions by hand looks like, and which questions it cannot answer.
Prerequisites and initial state: a Linux shell. The setup creates ~/git-practice/no-git, where someone kept several copies of the same network notes, with modification dates spread over March 2026.
Setup:
mkdir -p ~/git-practice/no-git && cd ~/git-practice/no-git
printf 'router 192.168.1.1\ndns 192.168.1.10\n' > network.txt
printf 'router 192.168.1.1\ndns 192.168.1.10\nnas 192.168.1.20\n' > network_v2.txt
printf 'router 192.168.1.1\ndns 192.168.1.11\nnas 192.168.1.20\nweb 192.168.1.30\n' > network_final.txt
printf 'router 192.168.1.1\ndns 192.168.1.11\nnas 192.168.1.20\n' > network_final_REALLY.txt
cp network_v2.txt network_v2_copy.txt
touch -d '2026-03-01 10:00' network.txt
touch -d '2026-03-08 10:00' network_v2.txt
touch -d '2026-03-15 10:00' network_final.txt
touch -d '2026-03-20 10:00' network_final_REALLY.txt
touch -d '2026-03-22 10:00' network_v2_copy.txt
cd ~
Three new commands for this lab:
| Command | Effect |
|---|---|
cat <file> |
Print the content of a file |
ls -lt |
List files with their details, most recently modified first |
diff <old> <new> |
Show the lines that differ between two files |
Tasks:
- Move into
~/git-practice/no-gitand list its content. How many copies of the network notes are there? - List the files from the most recently modified to the oldest. Is the most recent file the most up-to-date version of the notes?
- Display
network_final.txtandnetwork_final_REALLY.txt. Which one describes the current network? - Show what changed between
network_v2.txtandnetwork_final_REALLY.txt. - Try to answer these questions with the files alone: who changed the DNS server's address, and why? Was the web server removed on purpose, or forgotten?
- For each question of task 5, name the capability of version control (see What can version control do?) that would have answered it.
Expected result and verification:
- Task 1 lists 5 files.
- Task 2 shows
network_v2_copy.txtfirst: it is the most recent file, but only a copy of an old version. - Task 3:
network_final.txthas 4 lines andnetwork_final_REALLY.txthas 3. Nothing tells you which one is right. - Task 4 shows
< dns 192.168.1.10and> dns 192.168.1.11. - Task 5 has no answer: the files store content, not authors or reasons.
Solution
cd ~/git-practice/no-git
ls # 5 files for a single document
ls -lt # network_v2_copy.txt first (22 March)
cat network_final.txt # 4 lines, with the web server
cat network_final_REALLY.txt # 3 lines, no web server
diff network_v2.txt network_final_REALLY.txt
# 2c2
# < dns 192.168.1.10
# ---
# > dns 192.168.1.11
- A modification date says when a file was touched, not whether its content is the latest version: copying an old file makes it look new.
- The file names (
final,final_REALLY) are opinions, not history. Two people can each think their copy is the final one. - Identify a particular version answers "which one is current?": a commit history has exactly one latest version per line of work.
- Track files in different states and the history of each change answer "who, when, and why?": every Git commit records an author, a date, and a message.
- Revert changes would let you bring the web server back if its removal was a mistake, without guessing which copy still has it.
Clean up when you are done: rm -rf ~/git-practice/no-git.
Lab 3: paths and hidden files¶
Objective: move around with relative and absolute paths without getting lost, and reveal hidden files, as you will need to on the next page to see .git.
Prerequisites and initial state: a Linux shell. The setup creates a small homelab tree with two hidden entries, then goes back to your home directory.
Setup:
mkdir -p ~/git-practice/paths/homelab/docs ~/git-practice/paths/homelab/scripts ~/git-practice/paths/homelab/.config
cd ~/git-practice/paths/homelab
touch README.md .env docs/network.md scripts/backup.sh
cd ~
Tasks:
- In a single
cd, go to~/git-practice/paths/homelab/docs, and check where you are. - From
docs, move toscriptswith a singlecdand a relative path. - Without moving, list the content of the parent directory (
homelab). - Move up to
homelab. Compare the output oflsandls -a: which entries were hidden? - Go back to your home directory with the shortest possible command, and check where you are.
- Bonus: run
cd ~/git-practice/paths/homelab/scripts, thencd -, thencd -again. What doescd -do?
Expected result and verification:
- Task 1:
pwdends with/git-practice/paths/homelab/docs. - Task 2:
pwdends with/homelab/scripts. - Task 3 lists
README.md,docs, andscripts, but not.envor.config. - Task 4:
ls -aalso shows.,..,.config, and.env. - Task 5:
pwdprints your home directory, for example/home/sam. - Task 6: each
cd -prints the directory it jumps to, alternating between the two last locations.
Solution
cd ~/git-practice/paths/homelab/docs # absolute path, ~ = your home directory
pwd
cd ../scripts # up to homelab, then down into scripts
pwd
ls .. # README.md docs scripts
cd ..
ls # README.md docs scripts
ls -a # . .. .config .env README.md docs scripts
cd # same as cd ~
pwd
cd ~/git-practice/paths/homelab/scripts
cd - # back to the previous directory (home)
cd - # and back to scripts
..can be combined with a name:../scriptsmeans "thescriptsdirectory next to the current one".lsaccepts a path, so you can look somewhere without going there.- Names starting with a dot are hidden by convention.
.is the current directory and..its parent; they exist in every directory. cd -swaps between the current and the previous directory, handy when you work in two places at once.
Clean up when you are done: rm -rf ~/git-practice/paths.