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. Instead of keeping copies such as report_final.md, report_final_v2.md, and report_really_final.md, a version control system keeps one set of files and records each change you choose to save.
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 version we sent on Monday" |
| Revert changes | Go back to an earlier version when a change turns out to be wrong |
Why is version control important?¶
The course illustrates this with two situations:
- Iterating on something, such as a cook adjusting a recipe. You try changes, keep what works, and need to know what changed between attempts.
- 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 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 stores everything, so nothing is lost. Every recorded version is kept 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.
Precision
"Nothing is lost" applies to changes you have committed (recorded) in Git. Edits that were never committed are not protected, 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, a Documents folder and a Mental Health in Tech Project folder 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, using the same files as the course:
$ pwd
/home/repl/Documents
$ ls
archive finance.csv finance_data_clean.csv finance_data_modified.csv
$ cd archive
$ pwd
/home/repl/Documents/archive
pwdshows that the current directory is/home/repl/Documents.lslists its content: anarchivedirectory and three CSV files.cd archivemoves into thearchivesubdirectory. Runningpwdagain confirms the new location.
A few forms of cd that you will use constantly (standard shell behaviour, not specific to Git):
| 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/repl/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.46.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.
Challenge: find your way around the shell¶
Objective: use pwd, ls, and cd to create, 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 challenge 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 finance.csv finance_data_clean.csv finance_data_modified.csv archive/finance_2023.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
archiveand the three CSV files. - Step 3 lists
finance_2023.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 finance.csv finance_data_clean.csv finance_data_modified.csv
cd archive
ls # finance_2023.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.