Installing and Setting Up Git

Setting up Git involves installing it on your system and configuring global settings like user name and email for proper commit tracking.

Installing and Setting Up Git

Before you can start tracking changes and collaborating on projects, you need to install Git on your system and configure a few essential settings. This process takes only a few minutes, but doing it correctly from the beginning ensures that your commits are properly identified and your environment is ready for real development work from day one.

Git works on all major operating systems, and the installation process is straightforward on each one. Once installed, a small set of configuration commands personalizes Git to your identity and preferences. If you are completely new to version control, it is worth reading through the Git introduction first to understand what Git does before setting it up.

Check if Git Is Already Installed

Many systems come with Git pre-installed, particularly on macOS and Linux. Before downloading anything, open your terminal or command prompt and run the following command to check whether Git is already available on your machine.

git --version

If Git is installed, you will see the version number printed in the terminal. If the command is not recognized, you will need to install Git using the method appropriate for your operating system. Either way, confirming this first saves you from repeating an installation that may already be complete.

Installing Git on Windows

Windows does not include Git by default, so you need to install it manually. The most reliable way is to download the official Git installer from git-scm.com and run it on your machine. The installer provides a guided setup wizard that walks you through each configuration option step by step.

  1. Download the Git installer from the official website at git-scm.com
  2. Run the downloaded installer file
  3. Accept the default settings unless you have a specific reason to change them
  4. Complete the installation and open Git Bash or your preferred terminal

During installation, you will encounter options for choosing a default text editor, adjusting how Git integrates with your system PATH, and configuring how line endings are handled. If you are unsure about any of these, the default selections work well for most developers and can always be changed later through configuration commands.

Installing Git on macOS

macOS users have a couple of convenient options for installing Git. The simplest method is to install the Xcode Command Line Tools, which includes Git along with other useful developer utilities. Running the following command in the terminal will prompt you to install them if they are not already present.

xcode-select --install

If you prefer to manage your tools with a package manager, Homebrew is a popular choice on macOS. If Homebrew is already installed on your system, you can install Git with a single command.

brew install git

The Homebrew approach has the added benefit of making it easy to update Git to newer versions later by running brew upgrade git. Either method produces a fully functional Git installation that works identically in the terminal.

Installing Git on Linux

Linux distributions include Git in their official package repositories, which makes installation fast and simple through the terminal. The exact command depends on which distribution and package manager your system uses.

For Debian and Ubuntu based systems:

sudo apt update
sudo apt install git

For Fedora and Red Hat based systems:

sudo dnf install git

After the installation completes, run git --version once more to confirm that Git is available and working correctly before moving on to configuration.

Setting Up Your Identity

The most important configuration step after installation is setting your name and email address. Git attaches this identity to every commit you create, so it appears in your project history and is visible to collaborators. Skipping this step means your commits will either use incorrect defaults or fail entirely on some systems.

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

The --global flag applies these settings across all repositories on your system, so you only need to run these commands once. If you work on a specific project that requires a different identity, such as a work project with a professional email, you can override the global setting for that repository alone by running the same commands without the --global flag from inside the project folder.

Verifying Your Configuration

After setting your identity and any other preferences, it is a good habit to verify that everything is saved correctly. The following command displays all your current Git configuration settings in one place.

git config --list

Scan the output to confirm your username and email are correct. If you spot a mistake, simply re-run the configuration command with the correct value and it will overwrite the previous entry. You can also check a single setting directly by specifying its key.

git config user.name

Setting the Default Branch Name

Historically, Git named the default branch master when initializing a new repository. Modern conventions have shifted to using main as the default branch name, and most hosting platforms like GitHub now use main as their default as well. Configuring this globally ensures consistency between your local repositories and the platforms you work with.

git config --global init.defaultBranch main

With this in place, every new repository you initialize will automatically start with a branch named main. You can learn more about how branches work and why they matter in Git branching fundamentals.

Choosing a Default Text Editor

Git opens a text editor in certain situations, most commonly when you run git commit without the -m flag, which opens the editor so you can write a detailed commit message. Git also uses the editor when resolving some types of conflicts and during interactive rebase sessions. By default, Git uses the system's default editor, which is often Vim and can be unfamiliar to new users.

Setting a familiar editor upfront avoids confusion. The following example sets Visual Studio Code as the default editor, which is a popular choice for most developers.

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

The --wait flag tells Git to wait until you close the editor window before proceeding. Without it, Git would continue before you finish writing your message. You can substitute code with the command for any editor you prefer, such as nano, vim, or subl for Sublime Text.

Configuring Line Ending Behavior

Windows and Unix-based systems like macOS and Linux handle line endings in text files differently. Windows uses a carriage return and line feed combination, while macOS and Linux use only a line feed. When developers on different systems collaborate on the same project, these differences can cause unnecessary changes to appear in diffs and cause issues with some tools.

Git can handle line ending conversion automatically if configured correctly. The right setting depends on your operating system.

# Windows: convert line endings automatically on checkout and commit
git config --global core.autocrlf true

# macOS and Linux: normalize line endings on commit without converting on checkout
git config --global core.autocrlf input

Configuring this consistently across your team, ideally enforced through a .gitattributes file in the repository, prevents line ending conflicts from cluttering your project history.

Creating Your First Repository

With Git installed and configured, you are ready to create your first repository. A repository is simply a folder that Git is tracking. The git init command sets up that tracking by creating a hidden .git directory inside your project folder.

mkdir my-project
cd my-project
git init

From this point, any files you add and commit inside the folder will be part of the repository's history. You can also bring an existing project under version control by running git init inside its folder rather than creating a new one. For a full walkthrough of both initializing and cloning repositories, visit creating and cloning repositories.

Connecting to a Remote Repository

A local repository on your machine is fully functional for personal projects, but working with a team or backing up your code online requires connecting your local repository to a remote one. Remote repositories are hosted on platforms like GitHub, GitLab, or Bitbucket and serve as a shared point through which all collaborators synchronize their work.

Once your local repository is set up, you can link it to a remote and start pushing and pulling changes. The process of connecting and syncing with remote repositories is covered in full detail in working with remote repositories.

Common Setup Mistakes to Avoid

A few small errors during setup can cause confusion later. Being aware of them helps you avoid issues before they appear in your workflow.

  • Skipping identity configuration: If you do not set your name and email, commits will carry incorrect or missing author information that cannot be retroactively fixed cleanly in a shared repository.
  • Using the wrong email address: If your email does not match the one linked to your GitHub or GitLab account, your commits will not be associated with your profile on those platforms.
  • Not verifying the installation: Proceeding without confirming Git is working can lead to confusing errors later. A quick git --version check takes only a moment.
  • Ignoring line ending settings: On teams with mixed operating systems, inconsistent line ending configuration is a common source of unnecessary diff noise. Setting this early prevents the problem entirely.

Conclusion

Getting Git installed and configured correctly takes only a few minutes, but those few minutes pay dividends throughout your entire development career. With your identity set, your preferred editor configured, and your default branch name aligned with modern conventions, your environment is ready to support any Git workflow you encounter.

The natural next step after setup is learning how Git is actually used day to day. Start with the basic Git workflow to understand how files move through the stages of tracking, staging, and committing. From there, explore Git core concepts and viewing history and changes to build a solid practical foundation before moving into branching, remote collaboration, and more advanced features.