Install Git
The course needs no installation: the sandbox runs real Git in your browser. Install Git when you want to use it in your own projects. The commands in this course need Git 2.23 or newer (git switch and git restore), and the output shown comes from Git 2.52, so newer is better.
Do you already have Git?
Open a terminal (on Windows: Git Bash or PowerShell) and type:
git --version
If it answers with a version number, Git is installed. If the version is older than 2.23, update it with one of the methods below.
Windows
Download Git for Windows from git-scm.com/downloads/win and run the installer. The default choices are good. When it asks for an editor, pick one you know, for example Visual Studio Code. The installer also gives you Git Bash, a terminal with the same commands as in this course, and Git Credential Manager, which handles signing in to GitHub and GitLab.
With the Windows Package Manager it also works from PowerShell:
winget install --id Git.Git -e --source winget
macOS
Type git --version in Terminal. If you do not have Git, macOS offers to install Apple's command line tools, which include a slightly older Git. For the newest version, use Homebrew:
brew install git
Linux
Use your distribution's package manager. Stable distributions may ship an older Git; on Ubuntu, sudo add-apt-repository ppa:git-core/ppa gives the newest version.
sudo apt install git # Debian, Ubuntu
sudo dnf install git # Fedora
sudo pacman -S git # Arch
First-time setup
Git writes your name and email address into every commit. Set them once per machine, together with the name of the first branch and the editor Git should open:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
Use the same email address as on GitHub or GitLab, so your commits are linked to your account. If you would rather not show it, both services give you a noreply address. code --wait means VS Code; leave the line out to keep the default editor. Check the result with git config --global --list.
Connect to GitHub or GitLab
Git itself has no account. Signing in is only needed when you push to or fetch from a service, and GitHub does not accept your normal password over HTTPS. Choose one of two routes:
- HTTPS with Git Credential Manager (included in Git for Windows): the first
git pushopens a browser window where you sign in. On macOS and Linux you can use the GitHub CLI instead:gh auth login. - SSH key: create a key with
ssh-keygen -t ed25519 -C "you@example.com", add the contents of~/.ssh/id_ed25519.pubunder SSH keys in your account settings, and test withssh -T git@github.com. Then use the SSH address (git@github.com:user/project.git) when you clone.
Per-Arne's Git Good!