How to set up Git on Windows

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

Git is a powerful version control system widely used by developers for tracking changes in source code during software development. Setting up Git on Windows is the first step towards leveraging its capabilities for source code management. This tutorial will guide you through the process of installing and configuring Git on your Windows machine, including some basic to advanced uses, complete with code examples and expected outputs.

Prerequisites

  • A Windows operating system (7/8/10/11)
  • Basic knowledge of Windows Command Prompt or PowerShell
  • An internet connection

Installation

  1. Download the latest version of Git for Windows from the official website.
  2. Run the downloaded executable and follow the on-screen setup instructions.
  3. During installation, select the default options for most steps. However, when prompted to choose the default editor, you might want to choose an editor that you are comfortable with.
  4. In the ‘Adjusting your PATH environment’ step, you can choose ‘Git from the command line and also from 3rd-party software’ to access Git from both Git Bash and the Windows Command line.
  5. Complete the installation process.

After installation, open Git Bash or the Windows Command Prompt and type the following:

git --version

You should see the version of Git that you have installed:

git version 2.31.1.windows.1

Configuring Git

With Git installed, you need to configure your username and email address, which will be associated with your commits:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

To check your configuration settings, use:

git config --list

The output will show your set username and email.

Initializing a Repository

Initiating a repository is your starting point for any project:

git init my-project

You should get the following output:

Initialized empty Git repository in C:/path/to/your/my-project/.git/

Adding Files and Committing Changes

To add files to your repository, use the following command:

git add filename.txt

Or to add all files:

git add .

Then commit your changes:

git commit -m "Initial commit"

The terminal will show a message with the commit ID and stats.

Viewing the Commit History

To see the list of commits:

git log

You will see output similar to:

commit a1b2c3d4...(hash key)
Author: Your Name &[email protected]&gt
Date: Mon Mar 15 15:00:00 2021 +0000

    Initial commit

Working with Branches

Here’s how to create a new branch:

git branch new-feature

To switch to this branch:

git checkout new-feature

Output:

Switched to branch 'new-feature'

Advanced Topics

Here are a few advanced topics that we will skim through:

Merging Branches

git merge new-feature

Git will attempt to auto-merge the changes. If successful, you’ll see:

Merge made by the 'recursive' strategy.

Handling Merge Conflicts

Edit conflicted files manually and add them back:

git add filename.txt
git commit

Your commit will now contain the merged changes.

Using Remotes

Add a remote repository:

git remote add origin https://github.com/username/repo.git

To push your commits to the remote repository for the first time, use:

git push -u origin master

Conclusion

Setting up Git on Windows is straightforward, and once done, it opens up a realm of possibilities for version control and collaboration. With Git, you can manage projects of any size with ease. Take advantage of Git’s features for a more efficient and controlled development lifecycle.