Web Tools

📖 Git Commands Cheat Sheet

Comprehensive reference for Git version control commands

💡 Pro Tip: Use git help <command> or man git-<command> for detailed documentation on any Git command.

🚀

Setup & Init

git config --global user.name "Your Name"

Set your name for commit attribution

git config --global user.email "your@email.com"

Set your email for commit attribution

git config alias.st status

Create command shortcuts (e.g., git st for git status)

git init

Initialize a new Git repository in current directory

git clone <url>

Download a repository and its complete version history

📦

Stage & Snapshot

git status

Display working directory and staging area state

git add <file>

Add specific file changes to staging area

git add .

Stage all modified and new files

git add -p

Interactively select file portions to stage

git reset <file>

Remove specific file from staging area

git commit -m "message"

Record staged changes with inline message

git commit -am "message"

Stage and commit all modified files

git commit --amend

Revise commit message or add forgotten changes

🌿

Branch & Merge

git branch

Display all local branches

git branch <name>

Create a new branch

git switch <name>

Switch to specified branch

git switch -c <name>

Create and switch to new branch

git branch --sort=-committerdate

List branches by most recent activity

git merge <branch>

Integrate specified branch into current branch

git merge --squash <branch>

Combine branch changes into single commit

git branch -d <name>

Delete branch safely (prevents data loss)

git branch -D <name>

Force delete branch

🔍

Inspect & Compare

git log

Show commit history for current branch

git log --oneline

Condensed commit listing (one line per commit)

git log --graph

Display branching structure visually

git log <file>

Show all commits affecting specific file

git log --follow <file>

Include commits before file was renamed

git log -G "pattern"

Find commits adding/removing specific text

git diff

Show unstaged modifications

git diff --staged

Display only staged modifications

git diff HEAD

Show all staged and unstaged differences

git show <commit>

Display specific commit and its changes

git blame <file>

Identify who modified each line

📝

Tracking Path Changes

git rm <file>

Remove file from repository and working directory

git rm --cached <file>

Stop tracking file without deleting it

git mv <old> <new>

Rename or relocate a file

🌐

Share & Update

git remote add <name> <url>

Register new remote repository

git remote -v

List all remote connections

git fetch <remote>

Download changes without modifying branches

git pull

Fetch and merge changes from tracking remote

git pull --rebase

Fetch and rebase current branch

git push

Upload commits to tracking remote branch

git push -u origin <branch>

Upload new branch and set tracking

git push --force-with-lease

Override remote with local history (safer than --force)

git push --tags

Upload all tags to remote

⏮️

Rewrite History

git reset HEAD^

Undo most recent commit, preserve changes

git reset --hard <commit>

Restore to specific commit (discards changes)

git rebase <branch>

Reapply commits on top of another branch

git rebase -i HEAD~6

Interactively reorganize last 6 commits

git cherry-pick <commit>

Apply single commit to current branch

git reflog

View branch history and recover lost commits

💾

Temporary Commits

git stash

Temporarily save all uncommitted changes

git stash list

List all stashed changesets

git stash pop

Restore most recent stash and remove from list

git stash apply

Restore stash without removing it

git stash drop

Delete most recent stash

🗑️

Discard Changes

git restore <file>

Discard unstaged changes in working directory

git restore --staged <file>

Unstage file but keep changes

git checkout HEAD <file>

Remove all changes to file (staged and unstaged)

git reset --hard

Discard all staged and unstaged modifications

git clean -fd

Delete untracked files and directories

🚫

Ignoring Patterns

*.log
build/
temp-*

Save patterns in .gitignore to exclude files from tracking. System-wide patterns go in ~/.gitignore

git config --global core.excludesfile ~/.gitignore

Configure global gitignore file location

🎯 Ways to Refer to a Commit

  • Branch name: main, feature-branch
  • Tag: v1.0.0, release-2024
  • Commit hash: 3e887ab (short) or full SHA-1
  • Remote branch: origin/main
  • Current commit: HEAD
  • Previous commits: HEAD^ (parent), HEAD~3 (3 commits back), HEAD^^^ (3 parents back)
  • Range: main..feature (commits in feature not in main)

📁 Important Git Files

  • .git/config - Local repository settings
  • ~/.gitconfig - User-level global settings
  • .gitignore - Specifies files to exclude from tracking
  • .git/hooks/ - Scripts that run on Git events
  • ~/.gitignore - Global ignore patterns