Installing and Configuring Git

Installing and Configuring Git

Installing and Configuring Git

Introduction

Before you can start using Git to track your code, you need to install it on your system and configure it with your personal information. This lesson will guide you through downloading, installing, and setting up Git on Windows, macOS, and Linux systems.

Downloading Git

Git is free and open source software available for all major operating systems. The official source for Git downloads is the Git website.

Where to Download

Visit git-scm.com to download the latest version of Git. The website automatically detects your operating system and presents the appropriate download option prominently on the homepage.

Always download from the official Git website or your operating system's official package manager to ensure you're getting legitimate, safe software.

Installing Git on Different Systems

Windows Installation

Step 1: Download the Installer Navigate to git-scm.com and click the download button for Windows. This will download an executable installer file.

Step 2: Run the Installer Double-click the downloaded installer file. Windows may ask for administrator permission—click "Yes" to proceed.

Step 3: Installation Wizard The installer will present several configuration screens. For most users, the default options are appropriate. Here are the key screens you'll encounter:

  • Select Components: The defaults are fine for most users. Keep "Git Bash Here" and "Git GUI Here" checked for convenience.
  • Default Editor: Choose your preferred text editor. If you're unsure, Vim is the default, but you might prefer Visual Studio Code, Notepad++, or Nano if you're new to command-line editors.
  • PATH Environment: Choose "Git from the command line and also from 3rd-party software" (recommended). This makes Git available in Command Prompt, PowerShell, and other applications.
  • Line Ending Conversions: The default "Checkout Windows-style, commit Unix-style line endings" is recommended for Windows users.
  • Terminal Emulator: Use MinTTY for Git Bash. It provides a better experience than the default Windows console.

Step 4: Complete Installation Click through the remaining options with their defaults and wait for the installation to complete. Click "Finish" when done.

Step 5: Verify Installation Open Command Prompt, PowerShell, or Git Bash and type:

git --version

You should see the installed Git version number displayed.

macOS Installation

There are multiple ways to install Git on macOS:

Method 1: Using Homebrew (Recommended)

If you have Homebrew package manager installed, open Terminal and run:

brew install git

If you don't have Homebrew, you can install it first by visiting brew.sh and following the installation instructions, or use one of the methods below.

Method 2: Official Installer

Download the macOS installer from git-scm.com. Open the downloaded file and follow the installation prompts. You may need to adjust your security settings to allow the installer to run by going to System Preferences > Security & Privacy.

Method 3: Xcode Command Line Tools

macOS can install Git as part of the Xcode Command Line Tools. Open Terminal and type:

git --version

If Git isn't installed, macOS will prompt you to install the Command Line Tools. Follow the prompts to complete installation.

Verify Installation

Regardless of which method you used, verify the installation by opening Terminal and typing:

git --version

Linux Installation

Git installation on Linux varies by distribution, using the system's package manager.

Ubuntu/Debian:

sudo apt update
sudo apt install git

Fedora:

sudo dnf install git

Arch Linux:

sudo pacman -S git

CentOS/RHEL:

sudo yum install git

Verify Installation

After installation, verify by opening your terminal and typing:

git --version

Initial Configuration

Once Git is installed, you need to configure it with your personal information. This is crucial because every Git commit uses this information to identify who made the changes.

Setting Your Identity

Open your terminal (Git Bash on Windows, Terminal on macOS/Linux) and set your username and email:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Important: Use your real name and a valid email address. If you plan to use GitHub, GitLab, or similar platforms, use the same email address you'll register with those services.

The --global flag means these settings apply to all repositories on your computer. You can override these for specific projects if needed by running the same commands without --global inside a repository.

Configuring Your Default Editor

Git needs to know which text editor to open when you write commit messages. Configure your preferred editor:

For Visual Studio Code:

git config --global core.editor "code --wait"

For Nano (simple, beginner-friendly):

git config --global core.editor "nano"

For Vim (Git's default):

git config --global core.editor "vim"

For Notepad++ (Windows):

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

For Sublime Text:

git config --global core.editor "subl -n -w"

Setting the Default Branch Name

Recent versions of Git allow you to configure the default name for the initial branch. Many projects now use "main" instead of "master":

git config --global init.defaultBranch main

Configuring Line Endings

Line endings differ between operating systems. Configure Git to handle them appropriately:

For Windows:

git config --global core.autocrlf true

For macOS/Linux:

git config --global core.autocrlf input

This prevents issues when collaborating across different operating systems.

Enabling Colored Output

Make Git's command-line output easier to read with colors:

git config --global color.ui auto

Viewing Your Configuration

To see all your Git configuration settings, use:

git config --list

To see where each setting is defined (global, system, or local):

git config --list --show-origin

To check a specific setting:

git config user.name
git config user.email

Configuration Files

Git stores configuration in three levels:

System Level - Applies to every user on the computer. Located in Git's installation directory.

Global Level - Applies to all repositories for your user account. Stored in your home directory as .gitconfig (Linux/macOS) or .gitconfig (Windows).

Local Level - Applies only to a specific repository. Stored in .git/config inside that repository.

Settings at more specific levels override more general levels. Local overrides global, global overrides system.

You can manually edit these configuration files with a text editor if you prefer, though using git config commands is generally safer.

Common Configuration Options

Here are additional useful configurations:

Set Default Pull Behavior:

git config --global pull.rebase false

Enable Helpful Command Output:

git config --global help.autocorrect 1

Set Up Aliases (Shortcuts):

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit

Now you can type git st instead of git status, git co instead of git checkout, and so on.

Verifying Everything Works

Create a test repository to ensure everything is configured correctly:

mkdir git-test
cd git-test
git init

You should see a message indicating an empty Git repository was initialized. Create a test file:

echo "Hello Git" > test.txt
git add test.txt
git commit -m "Initial commit"

If this completes without errors, your Git installation and configuration are working correctly! You can delete the test directory afterward.

Troubleshooting Common Issues

Git Command Not Found

If you get a "command not found" error, Git might not be in your system PATH. On Windows, restarting your terminal or computer after installation often resolves this. On macOS/Linux, you may need to restart your terminal or add Git's location to your PATH variable.

Permission Denied Errors

On Linux/macOS, if you encounter permission issues during installation, make sure you're using sudo with the installation commands. Don't use sudo with git config commands, though—those should run with your regular user permissions.

SSL Certificate Problems

If you encounter SSL certificate errors, especially on corporate networks, you might need to configure Git's SSL verification settings. However, only do this if necessary and you understand the security implications.

Configuration Not Saving

Make sure you're using the correct syntax with quotes around values that contain spaces. Double-check that you're not inside a Git repository if you intend to set global configuration.

Next Steps

With Git installed and configured, you're ready to start using version control. Your configuration will persist across terminal sessions and computer restarts. You can always adjust your settings later using the same git config commands.

In the next lesson, you'll learn the essential Git commands to create repositories, track changes, and manage your project history. The foundation you've built here will make those operations seamless and correctly attributed to you.

Remember: you only need to do this initial setup once per computer. Git will remember your settings for all future projects.