Installing and setting up Git
Before you can use Git, you need to install it and tell it who you are. This only takes a few minutes, and you only ever do it once per computer.
Check if you already have it
Git might already be on your computer. Open the terminal in VS Code (Ctrl+`) and type:
git --version
If you see something like git version 2.47.1, Git is installed and you can skip ahead to "Tell Git who you are". The exact number doesn't matter much, as long as it starts with 2.
If you see an error like "command not found", you need to install it. Read on.
Installing on Windows
- Go to git-scm.com and click the download button for Windows
- Open the installer you downloaded
The installer asks a lot of questions. The defaults are fine for almost all of them, but two are worth changing:
- Choosing the default editor used by Git: pick Use Visual Studio Code as Git's default editor. Otherwise Git opens an old-school editor called Vim, which is famously hard to get out of.
- Adjusting the name of the initial branch: pick Override the default branch name and make sure it says
main. You'll learn what branches are in a later lesson.mainis the name GitHub uses, so this keeps things consistent.
Click through the rest with the defaults. When it's done, close VS Code completely and open it again, so its terminal picks up the newly installed Git. Then run git --version to check.
Installing on Mac
On a Mac, the easiest way is to just run the version command:
git --version
If Git isn't installed, macOS shows a pop up offering to install the command line developer tools. Click Install, accept the license and wait a few minutes. Git is included in those tools. When it's done, run git --version again to check.
You just installed Git on Windows, but the terminal in VS Code still says it can't find git. What's the most likely fix?
Select one answer
Tell Git who you are
Every commit you make is labeled with your name and email, so others can see who made which change. Set them once with these two commands, using your own name and email:
git config --global user.name "Alex Andersson" git config --global user.email "alex@example.com"
--global means the setting applies to every project on this computer, so you never have to do it again.
Use the same email address that you'll use for your GitHub account in a couple of lessons. That way GitHub can connect your commits to your profile.
A note on privacy: your commit email becomes visible to anyone who looks at your public projects. If you'd rather keep your email private, GitHub gives you a special private address. You'll find it in your GitHub settings under Emails once you've created an account, and you can update the setting then.
Name your main branch main
If you're on a Mac, or skipped that question in the Windows installer, run this as well:
git config --global init.defaultBranch main
It makes sure new projects start with a branch called main, which matches GitHub.
Check your settings
To see what you've set, run:
git config --global --list
You should see your name, your email and the default branch in the list. If something is wrong, just run the same git config command again with the right value.
What does the --global part of git config --global user.name do?
Select one answer
Why should your Git email match your GitHub account?
Select one answer
Get Git ready on your computer:
- Run
git --versionand install Git if it's missing - Set your name with
git config --global user.name - Set your email with
git config --global user.email - Set the default branch to
mainif you didn't do it in the installer - Run
git config --global --listand check that everything looks right
That's it. Git is ready. In the next lesson you'll make your very first commit.