Git & GitHub

Fundamentals, commands, workflows, branching, and collaboration.


Git Fundamentals

What is Git?

Git is a distributed version control system. It tracks changes in files so you can create versions of your project, compare changes, restore older states, and work with other developers.

Git tracks three important areas

Working Directory
The files you are currently editing in your project.
Staging Area
The changes you have selected for the next commit.
Repository
The Git database containing commits and project history.

Basic flow


Working Directory
        |
        | git add
        v
Staging Area
        |
        | git commit
        v
Local Repository
        |
        | git push
        v
Remote Repository
        

Git vs GitHub

Git
The version control system installed on your computer.
GitHub
A hosting and collaboration platform built around Git repositories.
Repository
A project tracked by Git. It contains files and Git history.
Remote
A reference to another copy of a Git repository, often hosted on GitHub.

Core Git Vocabulary

Commit
A recorded snapshot of changes.
Branch
An independent line of development.
Merge
Combines changes from one branch into another.
Rebase
Replays commits on top of another base commit.
Clone
Downloads an existing repository to your computer.
Push
Uploads local commits to a remote repository.
Pull
Fetches remote changes and integrates them into the current branch.
Fetch
Downloads remote information without changing your working branch.
HEAD
A reference to the commit or branch currently checked out.

GitHub Fundamentals

What is GitHub?

GitHub hosts Git repositories online and adds collaboration features such as pull requests, issues, reviews, discussions, permissions, releases, and automation.

Repository

A GitHub repository contains your project files, Git history, branches, issues, pull requests, and other project information.

Create a repository

A repository can be created directly on GitHub and then cloned locally.

Clone vs Fork

Clone
Copies a repository from a remote location onto your computer.
Fork
Creates your own GitHub-hosted copy of another repository.

A common open-source workflow is: fork → clone → branch → commit → push → pull request.

Pull Request

A pull request proposes merging changes from one branch into another branch, usually after review.

Typical pull request flow

  1. Create a feature branch.
  2. Make changes.
  3. Commit the changes.
  4. Push the branch to GitHub.
  5. Open a pull request.
  6. Review and discuss the changes.
  7. Merge the pull request.

Issues

GitHub Issues can be used to track bugs, tasks, feature requests, improvements, and project discussions.


Initial Setup

Check Git

git --version

Set Your Identity

Git uses your configured name and email when creating commits.

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

View configuration

git config --global --list

Set the Default Branch

git config --global init.defaultBranch main

Repository Basics

git init Initialize a Repository

Creates a new Git repository in the current directory.

git init

Typical usage

mkdir my-project
cd my-project
git init

git clone Clone a Repository

Creates a local copy of an existing repository.

git clone https://github.com/user/project.git

Clone with a custom directory name

git clone https://github.com/user/project.git my-project

git status Check Repository State

git status

This is one of the most useful Git commands. Run it frequently to understand what Git currently sees.

git log View Commit History

git log

Compact history

git log --oneline

Graph view

git log --oneline --graph --decorate --all

git diff Compare Changes

Shows changes that have not yet been staged.

git diff

View staged changes

git diff --staged

Commits

git add Stage Changes

Moves selected changes into the staging area.

git add file.txt

Stage multiple files

git add file1.txt file2.txt

Stage all changes

git add .

git commit Record Changes

Creates a commit from the current staged changes.

git commit -m "Add user authentication"

Good commit messages

Add login form
Fix validation error
Update README
Remove unused dependency

Good Commit Structure

Prefer small, focused commits that represent one logical change.

BAD
Update everything

BETTER
Add login validation
Fix password error message
Update authentication documentation

Branches

What is a Branch?

A branch is a movable reference to a line of development. Branches allow you to work on features or fixes without directly changing another branch.

git branch List Branches

git branch

List all local and remote branches

git branch -a

git switch Change Branch

git switch main

git switch -c Create and Switch

git switch -c feature/login

This creates the branch and immediately switches to it.

git merge Combine Branches

Suppose you are on main and want to merge feature/login.

git switch main
git merge feature/login

Branch Naming

Consistent branch names make repositories easier to navigate.

feature/login
feature/payment
fix/navbar
fix/api-error
docs/readme
refactor/auth-service

Remote Repositories

What is origin?

origin is the conventional name Git gives to the remote repository created when you clone a repository.

git remote -v

git remote add Add a Remote

git remote add origin https://github.com/user/project.git

git push Upload Commits

git push

Push a new branch and set its upstream

git push -u origin feature/login

After setting the upstream, future pushes can usually use:

git push

git fetch Download Remote Information

git fetch

Fetching updates your knowledge of the remote repository without automatically merging those changes into your current branch.

git pull Fetch and Integrate

git pull

Pull usually performs a fetch followed by an integration step. The exact integration behavior depends on your Git configuration.


GitHub Workflow

Feature Branch Workflow

A common collaborative workflow is to keep the main branch stable and develop features on separate branches.

git switch main
git pull

git switch -c feature/profile-page

# edit files

git status
git add .
git commit -m "Add profile page"

git push -u origin feature/profile-page

Then open a pull request on GitHub.

Pull Request Lifecycle

Feature Branch
      |
      v
   Push
      |
      v
GitHub Pull Request
      |
      v
Code Review
      |
      v
Changes Requested
      |
      v
More Commits
      |
      v
Approved
      |
      v
Merged
      |
      v
Delete Feature Branch

Keeping a Feature Branch Updated

Before opening or updating a pull request, you may need to bring recent changes from the main branch into your feature branch.

git fetch origin
git switch feature/profile-page
git merge origin/main

Undoing Changes

git restore Discard Working Directory Changes

Restores a file to the version recorded by Git and discards unstaged changes to that file.

git restore file.txt

This can permanently discard local changes, so use it carefully.

Unstage a File

Remove a file from the staging area while keeping its working directory changes.

git restore --staged file.txt

git revert Safely Undo a Commit

Creates a new commit that reverses the effect of an earlier commit. This is often appropriate for commits that have already been shared.

git revert <commit-id>

git reset Move HEAD

git reset changes where your branch points and can also modify the staging area or working directory depending on the mode.

Unstage the latest commit but keep changes

git reset --soft HEAD~1

Mixed reset

git reset HEAD~1

Reset can rewrite local history. Be cautious when using it on commits that have already been pushed.


Stashing

git stash Temporarily Store Changes

Stashing temporarily saves local modifications so you can switch branches or perform another task without committing unfinished work.

git stash

git stash list

git stash list

git stash pop

Applies the most recent stash and removes it from the stash list.

git stash pop

Named Stash

git stash push -m "unfinished login work"

Tags

What is a Tag?

A tag is a named reference to a specific Git commit. Tags are commonly used to mark releases.

git tag v1.0.0

List Tags

git tag

Push a Tag

git push origin v1.0.0

Push all tags

git push origin --tags

.gitignore

What is .gitignore?

A .gitignore file specifies files and directories that Git should normally ignore when detecting untracked files.

Example

node_modules/
.env
dist/
build/
*.log
.DS_Store

Why Ignore Files?

Common reasons include avoiding generated files, operating-system metadata, dependency directories, local configuration, and secrets.

Important

Adding a file to .gitignore does not automatically remove a file that Git is already tracking.


Useful Git Commands

git show Inspect a Commit

git show <commit-id>

git blame See Line History

Shows which commit and author last modified each line of a file.

git blame file.txt

git diff Between Branches

git diff main..feature/login

git branch -d Delete a Branch

git branch -d feature/login

Use -D when you intentionally want to force deletion.

git branch -D feature/login

Remote Branches

Remote-Tracking Branches

Git stores references such as origin/main to represent the last known state of the corresponding branch on the remote.

git branch -r

Track a Remote Branch

git switch --track origin/feature/login

Merge Conflicts

What is a Conflict?

A conflict occurs when Git cannot automatically combine changes, often because different branches modified overlapping parts of a file.

Conflict markers

<<<<<<< HEAD
Your current branch
=======
Incoming branch
>>>>>>> feature/login

Resolve a Conflict

  1. Open the conflicted file.
  2. Decide which changes should remain.
  3. Remove the conflict markers.
  4. Save the file.
  5. Stage the resolved file.
  6. Complete the merge or rebase operation.
git add file.txt
git commit

Rebase Fundamentals

git rebase

Rebase moves or replays commits so that your branch is based on another commit.

git switch feature/login
git rebase main

Rebase can produce a cleaner linear history, but it rewrites commit history. Avoid rebasing shared commits unless your team explicitly expects it.

Merge vs Rebase

Merge
Combines histories without rewriting existing commits.
Rebase
Replays commits onto a new base and can create a more linear history.

Git Configuration

View a Configuration Value

git config user.name
git config user.email

Set Aliases

git config --global alias.st status
git config --global alias.co checkout

Modern Git users may prefer switch instead of the older branch-switching behavior of checkout.


GitHub Authentication

HTTPS

GitHub repositories can be accessed using HTTPS remote URLs. Authentication is handled by GitHub-supported credentials or tools.

git remote add origin https://github.com/user/project.git

SSH

SSH can be used to authenticate with GitHub without entering credentials for every Git operation.

git remote add origin git@github.com:user/project.git

Real GitHub Workflow

Starting a New Project

mkdir my-project
cd my-project

git init

git add .
git commit -m "Initial commit"

git branch -M main

git remote add origin https://github.com/user/my-project.git

git push -u origin main

Daily Development Workflow

git switch main
git pull

git switch -c feature/dashboard

# Make changes

git status
git diff

git add .
git commit -m "Add dashboard"

git push -u origin feature/dashboard

After pushing, create a pull request on GitHub.

After the Pull Request Is Merged

git switch main
git pull

git branch -d feature/dashboard

Git + GitHub Mental Model

Think in Layers

Your Files
    |
    v
Git Working Directory
    |
    | git add
    v
Staging Area
    |
    | git commit
    v
Local Git Repository
    |
    | git push
    v
GitHub Repository
    |
    | Pull Request
    v
Code Review
    |
    | Merge
    v
Main Branch

Common Mistakes

Committing Everything Without Checking

Avoid blindly running git add . without checking what changed.

git status
git diff

git add .
git status

git commit -m "Meaningful message"

Committing Secrets

Never commit passwords, API keys, private keys, tokens, or other sensitive credentials.

.env
*.pem
secrets.json

Working Directly on Main

For collaborative development, using short-lived feature branches often makes review and integration safer.

Using Force Push Carelessly

Force-pushing can rewrite remote history and disrupt other contributors.

git push --force

When a force push is necessary, understand exactly which history you are replacing and follow your team's workflow.


Git Cheat Sheet

Repository

git init
git clone <url>
git status

Changes

git diff
git add <file>
git add .
git restore <file>
git restore --staged <file>

Commits

git commit -m "message"
git log
git log --oneline
git show <commit>

Branches

git branch
git switch main
git switch -c feature/name
git merge feature/name
git branch -d feature/name

Remote

git remote -v
git fetch
git pull
git push
git push -u origin branch-name

Temporary Work

git stash
git stash list
git stash pop

Learning Path

Level 1 Git Basics

  1. Understand repositories.
  2. Learn the working directory and staging area.
  3. Create commits.
  4. Read commit history.
  5. Use git status and git diff regularly.

Level 2 Branching

  1. Create branches.
  2. Switch branches.
  3. Merge branches.
  4. Resolve merge conflicts.
  5. Delete finished branches.

Level 3 GitHub

  1. Create repositories.
  2. Clone repositories.
  3. Configure remotes.
  4. Push and fetch changes.
  5. Create pull requests.
  6. Review and merge code.

Level 4 Advanced Git

  1. Learn rebase.
  2. Learn reset and revert.
  3. Use stash effectively.
  4. Use tags and releases.
  5. Understand remote-tracking branches.
  6. Learn interactive rebase.

Essential Workflow to Remember

The Core Cycle

git pull

git switch -c feature/my-change

# edit files

git status
git diff

git add .
git commit -m "Describe the change"

git push -u origin feature/my-change

# Open a Pull Request on GitHub

git switch main
git pull

Three Commands to Know First

git status
git add .
git commit -m "message"

Once these are comfortable, learn branches, remotes, pull requests, merge conflicts, and then the more advanced history-management tools.