Sling Academy
Home/DevOps/How to set up Git on Windows

How to set up Git on Windows

Last updated: January 27, 2024

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.

Next Article: How to set up Git on Mac

Previous Article: Does a Solo Developer Need Git? Understanding Version Control for Single Dev Projects

Series: Git & GitHub Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide