What This Lesson Sets Up
By the end of this lesson you will have Git installed, a terminal that runs the course's commands, and an empty practice folder for the exercises. You will also check that your Git version is new enough, because some commands in this course do not exist in older versions.
Choose Your Terminal
The exercises are written for one kind of terminal program, called a POSIX shell, so the same commands work on every system:
- Windows: use Git Bash, which is installed together with Git for Windows. Command Prompt and PowerShell are different shells, and some exercise commands will not work in them.
- macOS: use the Terminal app, in Applications > Utilities.
- Linux: use your distribution's terminal.
When a lesson says "in the terminal", it means this program.
Install Git
Use the official installation route for your system, listed on the Git website's install page.
Windows. Download Git for Windows from the install page and run the installer. It asks several questions; the default answers work for this course, because you will set the important choices yourself in the next lesson. If you use the Windows package manager instead, the install page gives this command:
winget install --id Git.Git -e --source winget
macOS. Choose one route:
- If you use Homebrew, run
brew install git. - Apple's Xcode Command Line Tools include Git: run
xcode-select --install. This version can be older than the current release, so check it below. - If you use MacPorts, run
sudo port install git.
Linux. Use your distribution's package manager, for example:
sudo apt-get install git # Debian and Ubuntu
sudo dnf install git # Fedora
sudo runs a command with administrator rights and asks for your password. On
a work or school computer you may need an administrator to install software.
If the package manager reports that it cannot find the package, refresh its
package list first, for example with sudo apt-get update on Debian or Ubuntu,
then run the install command again.
Distribution packages can lag behind the newest Git release; the version check
below tells you whether yours is new enough.
Check Your Git Version
Open a new terminal window after installing, so it picks up the new program. Then run:
git --version
Illustrative output:
git version 2.56.0
The number on your computer may differ. On Windows it may be followed by extra
text such as .windows.2. This course needs Git 2.28 or later, because
version 2.28 added the option for naming a new repository's first branch,
which the course uses from the start. The course was planned against Git
2.56.0, the latest release when it was written.
Compare version numbers part by part, not as decimals. In 2.56.0, the first
part is 2 and the second part is 56. The second part is a whole number, so
2.9 is older than 2.28, because 9 is less than 28. Any version
from 2.28.0 upwards, such as 2.39.5 or 2.56.0, is fine.
If your version is older than 2.28, update it through the same route you used to install it, or choose a newer route on the install page.
Terminal Commands the Exercises Use
The exercises rely on a few commands besides Git. ~ means your home folder.
| Command | What it does | Example |
|---|---|---|
pwd | Prints the folder you are in | pwd |
ls | Lists what is in the current folder | ls |
cd | Moves into a folder; cd .. moves up one level | cd ~/git-practice |
mkdir -p | Creates a folder, and any missing parent folders; no error if it already exists | mkdir -p ~/git-practice |
cat | Prints a file's contents | cat notes.txt |
Setup commands also use printf with > to write files, so everyone starts
with identical content:
printf 'First line\nSecond line\n' > notes.txt
printf prints the text between the quotes, where \n means "start a new
line". The > sends that text into the file named after it, replacing the
file's contents if it already exists. The command prints nothing on screen.
Cleanup uses rm -rf, which deletes a folder and everything in it without
asking for confirmation and without a recycle bin. The exercises therefore
always move to ~/git-practice first, list its contents and then remove one
named exercise folder. Never run rm -rf with a path you have not checked.
Exercise: Check Your Setup
This exercise confirms that Git and your terminal work and creates the practice folder used by every later exercise. It creates one file in a temporary exercise folder and then removes that folder.
Starting state: Git is installed and a terminal is open.
-
Check Git:
git --versionYou should see a version of 2.28 or later.
-
Create and enter an exercise folder:
mkdir -p ~/git-practice/check-setup cd ~/git-practice/check-setup pwdIllustrative output; the beginning of the path depends on your system and user name, but it should end with
git-practice/check-setup:/home/asha/git-practice/check-setupOn macOS the path starts with
/Users/; in Git Bash it starts with/c/Users/. -
Write a file and read it back:
printf 'Git practice works.\n' > hello.txt ls cat hello.txtIllustrative output:
hello.txt Git practice works. -
Clean up. Move to the practice folder, check what is there, remove only the exercise folder, then list again:
cd ~/git-practice ls rm -rf check-setup lsThe first
lsshowscheck-setup; the second shows nothing. Keep thegit-practicefolder itself: later exercises use it.
Success: Git reports 2.28 or later; pwd ends with
git-practice/check-setup; cat prints the line you wrote; and after cleanup,
check-setup is gone while ~/git-practice remains.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
git: command not found | Git is not installed, or this terminal opened before installing | Install Git, then open a new terminal; on Windows, use Git Bash |
'git' is not recognized as an internal or external command | You are in Command Prompt or PowerShell | Open Git Bash instead |
| A version older than 2.28 | An old installation | Update Git, then check again |
No such file or directory after cd | The mkdir step did not run, or there is a typo | Run the mkdir -p line again, then cd |
cat: hello.txt: No such file or directory | The file was written in a different folder | Run pwd, return to the exercise folder and rerun the printf line |
Independent task: without copying the commands above, create a folder
called scratch inside ~/git-practice, write a file in it containing your
first name, print the file, then remove the scratch folder and confirm it is
gone. Compare your commands with the answer below.
Check Your Understanding
Two learners run git --version. One sees git: command not found. The other
sees git version 2.25.1. What is wrong in each case, and what should each do?
And which is newer: Git 2.9.5 or Git 2.28.0?
Answer and Reasoning
Independent task
One correct sequence:
mkdir -p ~/git-practice/scratch
cd ~/git-practice/scratch
printf 'Asha\n' > name.txt
cat name.txt
cd ~/git-practice
ls
rm -rf scratch
ls
Any file name works. What matters is that you wrote and printed the file inside
scratch, moved out to ~/git-practice before deleting, and checked that
scratch appeared before removal and was absent after it.
Understanding check
The first learner's terminal cannot find Git: it is not installed, or the terminal was opened before installation, or (on Windows) the learner is not in Git Bash. Installing Git and opening a new Git Bash or terminal window fixes it.
The second learner has Git, but version 2.25.1 is older than the 2.28 minimum, so commands that name a new repository's first branch will not work. Updating Git fixes it.
Git 2.28.0 is newer. Compare the second parts as whole numbers: 28 is greater than 9.