c-cpp-setup

Git Setup Guide (All Platforms)

This guide will help you install and configure Git on Windows, macOS, and Linux. Git is essential for managing your code and collaborating with others.

What is Git and Why Do You Need It?

Git = A version control system that tracks changes in your code over time. Think of it as “save states” for your projects that you can go back to anytime.

GitHub/GitLab = Online services that store your Git repositories in the cloud, making it easy to share code and collaborate.

Why Git is Essential:

Prerequisites (What You Need First)

Note: You can use tools from the resources folder, but I recommend downloading the latest versions from official websites.


Windows Installation

Step 1: Download Git for Windows

  1. Go to the official Git website:
    • Visit: https://git-scm.com/download/win
    • The download should start automatically
    • If not, click “Click here to download manually”
  2. What you’re downloading:
    • Git Bash (Unix-style terminal for Windows)
    • Git GUI (graphical interface)
    • Git integration for Windows
  3. Alternative: If the download is slow, check if git-*.zip or git-*.tar.gz is available in your resources/git/ folder

Step 2: Install Git

  1. Run the installer:
    • Find the downloaded .exe file (usually in Downloads)
    • Right-click and “Run as administrator” if prompted
    • Click “Yes” if Windows asks for permission
  2. Installation wizard - Important choices:

    Select Components: (Keep defaults, but make sure these are checked)

    • Git Bash Here
    • Git GUI Here
    • Associate .git* configuration files with the default text editor
    • Associate .sh files to be run with Bash

    Choosing the default editor:

    • Recommended: Select “Use Visual Studio Code as Git’s default editor” if you have VS Code
    • Alternative: Select “Use Nano editor” for simplicity
    • Avoid: Vim (unless you already know how to use it)

    Adjusting your PATH environment:

    • Select: “Git from the command line and also from 3rd-party software”
    • This lets you use Git from Command Prompt, PowerShell, and Git Bash

    Choosing HTTPS transport backend:

    • Select: “Use the native Windows Secure Channel library”

    Configuring the line ending conversions:

    • Select: “Checkout Windows-style, commit Unix-style line endings”

    Configuring the terminal emulator:

    • Select: “Use Windows’ default console window”

    Other options: Keep the defaults

  3. Complete installation:
    • Click “Install”
    • Wait for installation to complete (1-3 minutes)
    • Click “Finish”

Step 3: Verify Installation

  1. Open Command Prompt or PowerShell:
    • Press Win + R, type cmd, press Enter
    • OR Press Win + X, select “Command Prompt” or “PowerShell”
  2. Test Git:
    git --version
    

    You should see something like:

    git version 2.41.0.windows.1
    
  3. Open Git Bash (Alternative terminal):
    • Right-click on Desktop → “Git Bash Here”
    • OR Search “Git Bash” in Start menu
    • This gives you a Unix-like terminal on Windows

macOS Installation

Step 1: Install Homebrew (if not already installed)

  1. Open Terminal:
    • Press Cmd + Space, type “Terminal”, press Enter
    • OR Applications → Utilities → Terminal
  2. Install Homebrew:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
    • Enter your password when prompted
    • Press Enter to continue when asked
    • Wait for installation (5-15 minutes)

Step 2: Install Git with Homebrew

brew install git

Wait for installation to complete.

Method 2: Using Xcode Command Line Tools

xcode-select --install

Step 3: Verify Installation

  1. Open Terminal

  2. Test Git:

    git --version
    

    You should see something like:

    git version 2.41.0
    

Linux Installation

Ubuntu/Debian

Step 1: Open Terminal

Step 2: Update System

sudo apt update

Step 3: Install Git

sudo apt install git

Step 4: Verify Installation

git --version

Fedora/RHEL/CentOS

Step 1: Open Terminal

Step 2: Install Git

Fedora:

sudo dnf install git

RHEL/CentOS 7:

sudo yum install git

RHEL/CentOS 8+:

sudo dnf install git

Step 3: Verify Installation

git --version

Arch Linux

sudo pacman -S git

Other Distributions

Most Linux distributions include Git in their repositories:


Initial Git Configuration (All Platforms)

After installing Git, you need to configure it with your information.

Step 1: Set Your Identity

These commands work on all platforms:

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

Examples:

git config --global user.name "Asmin Bhattarai"
git config --global user.email "asminbhattarai@gmail.com"

Important Notes:

Step 2: Set Default Branch Name

Modern best practice is to use main instead of master:

git config --global init.defaultBranch main

Step 3: Set Your Default Editor (Optional)

If you have VS Code:

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

If you prefer nano (simple terminal editor):

git config --global core.editor nano

For Windows users using Notepad++:

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

Step 4: Verify Your Configuration

git config --list

You should see your name, email, and other settings.

Step 5: Configure Line Endings (Important for Cross-Platform Work)

Windows:

git config --global core.autocrlf true

macOS/Linux:

git config --global core.autocrlf input

Testing Your Git Installation

Let’s create a test repository to make sure everything works:

Step 1: Create a Test Directory

Windows (Command Prompt/PowerShell):

cd Desktop
mkdir git-test
cd git-test

macOS/Linux/Windows (Git Bash):

cd ~/Desktop
mkdir git-test
cd git-test

Step 2: Initialize a Git Repository

git init

You should see:

Initialized empty Git repository in /path/to/git-test/.git/

Step 3: Create a Test File

Create a simple text file:

echo "Hello Git!" > README.txt

Windows Command Prompt alternative:

echo Hello Git! > README.txt

Step 4: Add and Commit the File

git add README.txt
git commit -m "My first commit"

You should see something like:

[main (root-commit) a1b2c3d] My first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.txt

Step 5: Check Your Work

git log --oneline
git status

Step 6: Clean Up

cd ..
rm -rf git-test

Windows Command Prompt:

cd ..
rmdir /s git-test

Common Problems and Solutions

All Platforms

Problem: “git: command not found” or “‘git’ is not recognized”

Problem: “Permission denied” when trying to commit

Problem: Text editor opens but you can’t exit

Windows-Specific

Problem: Line ending issues when working with others

Problem: Git Bash doesn’t work

Problem: Can’t use Git from Command Prompt

macOS-Specific

Problem: “xcrun: error: invalid active developer path”

Problem: Homebrew installation fails

Linux-Specific

Problem: “Package not found”

Problem: “Permission denied” for sudo commands


What’s Next?

Now that you have Git installed and configured:

Essential Git Commands to Learn

git init          # Create a new repository
git clone <url>   # Copy a repository from GitHub/GitLab
git add <file>    # Stage changes
git commit -m "message"  # Save changes
git status        # Check what's changed
git log           # See commit history
git push          # Upload to remote repository
git pull          # Download latest changes
  1. Create a GitHub Account:
    • Visit: https://github.com
    • Sign up with the same email you configured in Git
  2. Learn Basic Git Workflow:
    • Practice creating repositories
    • Learn to add, commit, and push changes
    • Understand branching and merging
  3. Set Up SSH Keys (Optional but Recommended):
    • Allows secure connection to GitHub without passwords
    • Follow GitHub’s SSH key guide
  4. Install a Git GUI (Optional):
    • GitHub Desktop - User-friendly interface
    • GitKraken - Professional Git client
    • Sourcetree - Free Git client by Atlassian
  5. Learn Git with Your IDE:
    • VS Code has excellent Git integration
    • Most IDEs have built-in Git support

Useful Resources


Quick Summary

Here’s what you accomplished:

All Users:

  1. Installed Git on your operating system
  2. Configured your identity (name and email)
  3. Set up proper line ending handling
  4. Configured default branch name
  5. Tested Git with a sample repository
  6. Verified everything works correctly

Platform-Specific Achievements:

Windows Users:

macOS Users:

Linux Users:

Your system is now ready for version control with Git!


Pro Tips:


Back to: Home