No history yet

Git Configuration

Setting Up Your Identity

Before you make your first commit, Git needs to know who you are. This information is attached to every change you make, providing a clear audit trail. You can set your name and email address globally, which means it will apply to every Git repository on your system.

# Set your username for all repositories
git config --global user.name "Your Name"

# Set your email for all repositories
git config --global user.email "youremail@example.com"

The --global flag saves these settings to a .gitconfig file in your user's home directory. But what if you need to use a different email for a specific project, like a work repository? You can override the global setting with a local configuration. Just navigate into your project's directory and run the same commands without the --global flag.

# Inside your project directory

# Set your username just for this repository
git config user.name "Work Name"

# Set your email just for this repository
git config user.email "work.email@company.com"

This local setting is stored inside the project's own .git/config file. Git always prioritizes local settings over global ones, giving you precise control over your identity for each context.

Securely Connecting to GitHub

While you can clone repositories using HTTPS, pushing changes requires you to enter your username and password every time. A more secure and convenient method is to use SSH keys to establish a trusted connection between your computer and GitHub.

# Generate a new SSH key pair
# The -t flag specifies the encryption algorithm (ed25519 is recommended)
# The -C flag adds a comment, typically your email
ssh-keygen -t ed25519 -C "youremail@example.com"

This command creates two files in a hidden .ssh folder in your home directory: a private key (id_ed25519) and a public key (id_ed25519.pub). The private key must remain secret on your machine. The public key is what you share. You'll need to copy the contents of your public key and add it to your GitHub account under "Settings > SSH and GPG keys". Once added, GitHub will recognize your machine, and you can push changes without typing your password.

Lesson image

The Three States of Git

Every file in your project exists in one of three states. Understanding this separation is the key to using Git effectively. The entire system is managed within a hidden directory in your project's root: the .git directory. This is where Git stores all its metadata and the object database for your project.

  1. Working Directory: This is your project folder with all its files. It's the sandboxed version of your project where you can freely edit, add, or delete files. Any file here is untracked by Git until you explicitly tell it otherwise.

  2. Staging Area (Index): This is an intermediate area that holds information about what will go into your next commit. You use the git add command to move changes from your working directory to the staging area. It's like a loading dock for a shipment; you gather all the items you want to include before sealing the box.

  3. Repository (.git directory): When you run git commit, Git takes the files from the staging area and stores a permanent snapshot of them in your local repository. This snapshot has a unique ID, and it represents a safe, saved version of your project that you can return to at any time.

This three-stage process allows you to carefully craft your commits. You can change ten files in your working directory but only stage and commit the two that are related to a single feature, keeping your project history clean and logical.

Ignoring Files

Not every file in your project folder should be tracked by Git. Log files, build artifacts, and files containing sensitive information (like API keys) should be excluded. You do this using a special file named .gitignore.

This is a plain text file where each line specifies a pattern for files or directories to ignore. You can use wildcards and other patterns to exclude entire groups of files.

# Ignore all files with the .log extension
*.log

# Ignore a specific directory
/node_modules

# Ignore a specific file
.env

# You can also make exceptions to a rule
!important.log

It's a best practice to create a .gitignore file at the start of any new project. Many online services, including GitHub itself, provide pre-made for various programming languages and frameworks. These templates are a great starting point for keeping your repository clean and focused only on the source code that matters.

Now that you have a properly configured environment, you're ready to start tracking your work.

Let's test your understanding of Git configuration.

Quiz Questions 1/5

If you have set your email address both globally and locally for a specific project, which email will Git use for commits made within that project?

Quiz Questions 2/5

What is the primary purpose of the .gitignore file?