Phase 3Local Git

#9 Install and configure Git

git init, git config

What is Git?

Git is a version control system. Think of it as a video game save file for your code: you can save the state of your project at any point, go back if something breaks, and even collaborate with others on the same code without mixing everything up.

Without Git, you end up with folders like project-final, project-final-v2, project-final-REALLY-final... Git solves this problem elegantly.

git init -- Create a repository

To start tracking changes in a project with Git, you initialize a repository in the project folder:

Initialize a Git repository
$ cd my-project
$ git init
Initialized empty Git repository in /home/user/my-project/.git/

This command creates a hidden .git/ folder that contains the entire history and configuration of the repository. Never touch this folder manually!

git config -- Set up your identity

Before you start recording changes, Git needs to know who you are. Every change will be signed with your name and email:

Configure name and email
$ git config --global user.name "Alice"
$ git config --global user.email "alice@email.com"

The --global option applies the configuration to all your projects. Without this option, the configuration only applies to the current repository.

Check the configuration

To see the current configuration, use git config --list:

List configuration
$ git config --list
user.name=Alice
user.email=alice@email.com
core.editor=nano
init.defaultbranch=main

You can also query a specific value:

Query a value
$ git config user.name
Alice

$ git config user.email
alice@email.com

Summary

Summary
git init                              # Initialize a repository
git config --global user.name "..."    # Set your name
git config --global user.email "..."   # Set your email
git config --list                      # View configuration
git config user.name                   # View a value

Your turn

Try initializing a repository and setting up your Git identity in the terminal below.

terminal — bash
user@stemlegacy:~/my-project$