Skip to main content

Git Fundamentals

Master the core concepts that make Git powerful: repositories, commits, and the staging area.

Installing and Configuring Git

# Download from git-scm.com or use winget
winget install Git.Git

# Verify installation
git --version

First-Time Configuration

# Set your identity (required)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Set default branch name
git config --global init.defaultBranch main

# Set default editor
git config --global core.editor "code --wait"  # VS Code
# or
git config --global core.editor "vim"

# View all settings
git config --list

Creating Your First Repository

Method 1: Initialize a New Repository

# Create a new directory
mkdir my-project
cd my-project

# Initialize Git repository
git init

# This creates a hidden .git directory
ls -la
The .git directory contains all Git metadata: commits, branches, configuration, and the entire history.

Method 2: Clone an Existing Repository

# Clone from GitHub
git clone https://github.com/username/repo.git

# Clone with a different name
git clone https://github.com/username/repo.git my-folder

# Clone a specific branch
git clone -b develop https://github.com/username/repo.git

The Git Workflow

Git Workflow

1. Working Directory

Your actual files where you make changes.
# Create a new file
echo "# My Project" > README.md

# Edit files with your editor
code index.html

2. Staging Area (Index)

Prepare changes for commit. This is Git’s “shopping cart.”
# Stage a single file
git add README.md

# Stage all changes
git add .

# Stage specific files
git add file1.txt file2.txt

# Stage all .js files
git add *.js

# Interactive staging
git add -p  # Choose which changes to stage
Why a staging area? It lets you craft precise commits. You can modify 10 files but commit only 3 related changes.

3. Repository

Committed snapshots stored permanently.
# Commit staged changes
git commit -m "Add README and initial HTML"

# Commit with detailed message
git commit  # Opens editor for multi-line message

# Stage and commit in one step (tracked files only)
git commit -am "Update homepage content"

Understanding Commits

A commit is a snapshot of your entire project at a point in time.

Anatomy of a Commit

commit abc123def456  # SHA-1 hash (unique identifier)
Author: John Doe <[email protected]m>
Date:   Mon Dec 2 10:30:00 2024 +0500

    Add user authentication feature
    
    - Implement login/logout
    - Add password hashing
    - Create user session management

Writing Good Commit Messages

git commit -m "feat: add user authentication"
git commit -m "fix: resolve memory leak in image processor"
git commit -m "docs: update API documentation"
git commit -m "refactor: simplify database query logic"
Commit Message Best Practices:
  • Use present tense: “Add feature” not “Added feature”
  • Be specific: “Fix login timeout” not “Fix bug”
  • Keep first line under 50 characters
  • Add details in the body if needed
  • Reference issue numbers: “Fix #123: resolve login issue”

Checking Status and History

Git Status

# See what's changed
git status

# Short format
git status -s
Output explained:
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   index.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
        modified:   style.css

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        script.js

Git Log

# View commit history
git log

# One line per commit
git log --oneline

# With graph
git log --oneline --graph --all

# Last 5 commits
git log -5

# Commits by author
git log --author="John"

# Commits in date range
git log --since="2 weeks ago"

# Show changes in each commit
git log -p

Viewing Changes

Git Diff

# Changes in working directory (not staged)
git diff

# Changes in staging area
git diff --staged

# Changes between commits
git diff abc123 def456

# Changes in a specific file
git diff README.md

# Word-level diff
git diff --word-diff

Ignoring Files

Create a .gitignore file to exclude files from version control.
# .gitignore

# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.exe
*.dll

# Environment files
.env
.env.local

# IDE files
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Temporary files
*.tmp
temp/
Use gitignore.io to generate .gitignore templates for your tech stack.

Undoing Changes

Unstage Files

# Unstage a file (keep changes in working directory)
git restore --staged file.txt

# Unstage all files
git restore --staged .

Discard Changes

# Discard changes in working directory (DANGEROUS!)
git restore file.txt

# Discard all changes
git restore .
git restore without --staged permanently deletes uncommitted changes!

Amend Last Commit

# Fix the last commit message
git commit --amend -m "New message"

# Add forgotten files to last commit
git add forgotten-file.txt
git commit --amend --no-edit

Practical Example: First Project

Let’s create a complete example:
# 1. Create project
mkdir my-website
cd my-website
git init

# 2. Create files
echo "# My Website" > README.md
echo "<!DOCTYPE html><html><body>Hello</body></html>" > index.html

# 3. Check status
git status

# 4. Stage files
git add README.md index.html

# 5. Commit
git commit -m "Initial commit: add README and index page"

# 6. Make changes
echo "<p>Welcome!</p>" >> index.html

# 7. See what changed
git diff

# 8. Stage and commit
git add index.html
git commit -m "Add welcome message to homepage"

# 9. View history
git log --oneline

Common Workflows

Daily Development

# Start your day
git status                    # See where you left off
git pull origin main          # Get latest changes

# Work on feature
git checkout -b feature/new-ui
# ... make changes ...
git add .
git commit -m "feat: redesign homepage UI"

# Push your work
git push origin feature/new-ui

Quick Fixes

# Oops, typo in last commit
git add typo-file.txt
git commit --amend --no-edit

# Discard all uncommitted changes
git restore .
git clean -fd  # Remove untracked files

Key Takeaways

Three States

Modified → Staged → Committed

Commit Often

Small, logical commits are better than large ones

Good Messages

Future you will thank present you

Check Status

Run git status frequently

Practice Exercises

1

Exercise 1: Basic Workflow

  1. Create a new repository
  2. Add 3 files
  3. Make 5 commits
  4. View the log with --oneline --graph
2

Exercise 2: Staging

  1. Modify 3 files
  2. Stage only 2 of them
  3. Commit the staged files
  4. Commit the remaining file separately
3

Exercise 3: Undoing

  1. Make a commit with a typo in the message
  2. Amend the commit
  3. Make changes to a file
  4. Discard those changes

Next: Git Branching & Merging →