Git Cheatsheet

What is Git Git vs GitHub Installation & Setup Configuration Lifecycle Init Repo Clone Repo Status Add Commit Log Diff Branching Create/Switch Branch Merge Merge Conflicts Rebase Delete Branch Stash Restore Undo Remote Push Pull Fetch Fork & PR Tag .gitignore Aliases Log Customization Hooks Workflows

What is Git?

Intro

Git is a distributed version control system for tracking changes in source code during software development. It enables collaboration, branching, merging, and history tracking.

Git vs GitHub / GitLab

Concept
FeatureGitGitHub/GitLab
TypeVersion ControlHosting Platform
ScopeLocal & RemoteRemote (Cloud)
FeaturesBranch, Merge, TrackPRs, Issues, CI/CD

Git Installation & Setup

Setup
# Windows: Download installer from website
# macOS (Homebrew):
brew install git
# Ubuntu/Debian:
sudo apt update && sudo apt install git

Git Configuration (git config)

Config
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --list

Git Lifecycle

Concept
Git Lifecycle

Initializing a Repository (git init)

Init
git init

Cloning a Repository (git clone)

Clone
git clone https://github.com/user/repo.git

Checking Status (git status)

Status
git status

Adding Changes (git add)

Add
git add file.txt
# Add all changes
git add .

Committing Changes (git commit)

Commit
git commit -m "Describe your changes"

Viewing Commit History (git log)

Log
git log
# One line per commit
git log --oneline

Git Diff (git diff, git diff --staged)

Diff
git diff
# Show staged changes
git diff --staged

Branching in Git

Branch
git branch

Creating & Switching Branches

Branch
git branch feature-x
git checkout feature-x
# Or (Git 2.23+)
git switch feature-x

Merging Branches (git merge)

Merge
git checkout main
git merge feature-x

Resolving Merge Conflicts

Conflicts
# After conflict, edit files, then:
git add conflicted-file.txt
git commit

Git Rebase (Basic Concept)

Rebase
git checkout feature-x
git rebase main

Deleting Branches (git branch -d)

Delete
git branch -d feature-x
# Force delete
git branch -D feature-x

Stashing Changes (git stash)

Stash
git stash
# List stashes
git stash list
# Apply latest stash
git stash apply

Restoring Files (git restore, git checkout)

Restore
git restore file.txt
# Or (older)
git checkout -- file.txt

Undoing Commits (git reset, git revert)

Undo
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a commit (safe for shared repos)
git revert <commit-hash>

Git Remote (git remote add, git remote -v)

Remote
git remote add origin https://github.com/user/repo.git
git remote -v

Pushing to Remote (git push)

Push
git push origin main

Pulling from Remote (git pull)

Pull
git pull origin main

Fetching Changes (git fetch)

Fetch
git fetch origin

Forking & Pull Requests (Conceptual)

Collab

Tagging Releases (git tag)

Tag
git tag v1.0.0
git tag
# Push tags to remote
git push origin v1.0.0

Ignoring Files (.gitignore)

Ignore
# Example .gitignore
node_modules/
.env
*.log
/dist

Git Aliases

Alias
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m"

Git Log Customization

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

Git Hooks (Intro)

Hooks

Common Git Workflows

Workflow
Common Git Workflows

(Above: Feature Branch & Git Flow simplified. Forking workflow: fork → clone → branch → PR.)