Design.dev design.dev

Git Commands Guide

Essential Git commands organized by workflow. From basic commits to advanced operations, with clear explanations and dangerous commands clearly marked.

Getting Started

Configure Git

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

# Set default editor
git config --global core.editor "code --wait"

# View configuration
git config --list

# Check a specific setting
git config user.name

Initialize Repository

# Create new repository
git init

# Clone existing repository
git clone https://github.com/user/repo.git

# Clone into specific directory
git clone https://github.com/user/repo.git my-folder

# Clone specific branch
git clone -b branch-name https://github.com/user/repo.git

Basic Workflow

The fundamental Git workflow: modify, stage, commit.

Check Status

# See current status
git status

# Short format
git status -s

Stage Changes

# Stage specific file
git add file.txt

# Stage all changes in directory
git add .

# Stage all changes (including deleted files)
git add -A

# Stage only modified files (not new)
git add -u

# Stage parts of a file interactively
git add -p file.txt

Commit Changes

# Commit with message
git commit -m "Add feature X"

# Commit with detailed message
git commit -m "Add feature X" -m "This feature does..."

# Stage and commit tracked files
git commit -am "Update existing files"

# Amend last commit
git commit --amend -m "Updated message"

# Amend without changing message
git commit --amend --no-edit

Unstage Changes

# Unstage specific file
git restore --staged file.txt

# Unstage all files
git restore --staged .

# Old syntax (still works)
git reset HEAD file.txt

Branching & Merging

Work on features in isolation and combine changes.

Branch Management

# List branches
git branch

# List all branches (including remote)
git branch -a

# Create new branch
git branch feature-name

# Create and switch to new branch
git checkout -b feature-name
# OR (modern syntax)
git switch -c feature-name

# Switch branches
git checkout main
# OR
git switch main

# Rename current branch
git branch -m new-name

# Delete branch
git branch -d feature-name

# Force delete unmerged branch
git branch -D feature-name
-D is destructive

Merging

# Merge branch into current branch
git merge feature-name

# Merge with commit message
git merge feature-name -m "Merge feature X"

# Abort merge
git merge --abort

# See what branches are merged
git branch --merged

# See what branches aren't merged
git branch --no-merged

Resolve Conflicts

# After editing conflicted files
git add conflicted-file.txt
git commit

# View conflicts
git diff

# Use theirs for conflicts
git checkout --theirs file.txt

# Use ours for conflicts
git checkout --ours file.txt

Viewing History

Inspect commits and changes.

View Logs

# View commit history
git log

# Compact format
git log --oneline

# Show last N commits
git log -3

# Show with file changes
git log --stat

# Show with full diffs
git log -p

# Graphical view
git log --graph --oneline --all

# Search commits
git log --grep="search term"

# By author
git log --author="John"

# By date range
git log --since="2 weeks ago"
git log --after="2023-01-01" --before="2023-12-31"

# Changes to specific file
git log -- path/to/file.txt

View Differences

# Unstaged changes
git diff

# Staged changes
git diff --staged

# Changes between commits
git diff commit1 commit2

# Changes in specific file
git diff file.txt

# Compare branches
git diff main..feature-branch

# Summary of changes
git diff --stat

Show Commit Details

# Show specific commit
git show commit-hash

# Show last commit
git show HEAD

# Show file at specific commit
git show commit-hash:path/to/file.txt

Undoing Changes

Fix mistakes and revert changes.

Discard Changes

# Discard changes in specific file
git restore file.txt

# Discard all unstaged changes
git restore .

# Old syntax (still works)
git checkout -- file.txt

Undo Commits

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, keep changes unstaged
git reset HEAD~1
# OR
git reset --mixed HEAD~1

# Undo last commit, discard changes
git reset --hard HEAD~1
--hard is destructive

Revert Commits

# Create new commit that undoes changes
git revert commit-hash

# Revert without committing
git revert -n commit-hash

# Revert multiple commits
git revert commit1..commit2

reset vs revert: Use reset to undo local commits. Use revert for pushed commits (creates new commit, preserves history).

Clean Untracked Files

# Preview what will be deleted
git clean -n

# Delete untracked files
git clean -f

# Delete untracked files and directories
git clean -fd

# Include ignored files
git clean -fx
Use -n first to preview

Remote Repositories

Work with remote repositories like GitHub.

Manage Remotes

# List remotes
git remote -v

# Add remote
git remote add origin https://github.com/user/repo.git

# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git

# Remove remote
git remote remove origin

# Rename remote
git remote rename origin upstream

Fetch & Pull

# Fetch changes from remote
git fetch origin

# Fetch all remotes
git fetch --all

# Pull changes (fetch + merge)
git pull

# Pull specific branch
git pull origin main

# Pull with rebase
git pull --rebase

# Pull and prune deleted branches
git pull --prune

Push Changes

# Push to remote
git push origin main

# Push and set upstream
git push -u origin feature-branch

# Push all branches
git push --all

# Push tags
git push --tags

# Delete remote branch
git push origin --delete branch-name

# Force push (use with caution!)
git push --force origin main
--force can destroy history

⚠️ Never force push to main/master: Use --force-with-lease as safer alternative that checks for remote changes first.

Collaboration

Work effectively with team members.

Stashing

# Stash current changes
git stash

# Stash with message
git stash save "work in progress"

# List stashes
git stash list

# Apply last stash
git stash apply

# Apply and remove last stash
git stash pop

# Apply specific stash
git stash apply stash@{2}

# Delete stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

# Stash untracked files too
git stash -u

Tags

# List tags
git tag

# Create lightweight tag
git tag v1.0.0

# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"

# Tag specific commit
git tag v1.0.0 commit-hash

# Push tag
git push origin v1.0.0

# Push all tags
git push --tags

# Delete local tag
git tag -d v1.0.0

# Delete remote tag
git push origin --delete v1.0.0

Cherry Pick

# Apply specific commit to current branch
git cherry-pick commit-hash

# Cherry pick without committing
git cherry-pick -n commit-hash

# Cherry pick range
git cherry-pick commit1..commit2

Advanced Operations

Powerful commands for complex scenarios.

Rebase

# Rebase current branch onto main
git rebase main

# Interactive rebase (last 3 commits)
git rebase -i HEAD~3

# Continue after resolving conflicts
git rebase --continue

# Skip current commit
git rebase --skip

# Abort rebase
git rebase --abort
Don't rebase shared branches

Interactive Rebase Commands:

  • pick - use commit
  • reword - use commit, edit message
  • edit - use commit, stop for amending
  • squash - combine with previous commit
  • fixup - like squash, discard message
  • drop - remove commit

Reflog

# View reference logs (safety net!)
git reflog

# Recover lost commits
git checkout commit-hash

# View specific branch reflog
git reflog show branch-name

Reflog is your safety net: It tracks all HEAD movements. Use it to recover "lost" commits after reset or rebase.

Bisect

# Find commit that introduced a bug
git bisect start
git bisect bad                 # current commit is bad
git bisect good commit-hash    # known good commit

# Mark each commit as good or bad
git bisect good
git bisect bad

# End bisect
git bisect reset

Worktrees

# Create worktree
git worktree add ../feature feature-branch

# List worktrees
git worktree list

# Remove worktree
git worktree remove ../feature

# Prune stale worktrees
git worktree prune

Useful Aliases

# Add aliases to your .gitconfig
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# Complex aliases
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.undo "reset --soft HEAD~1"

# Use them like:
git co main
git lg

Quick Reference

Task Command
Undo last commit, keep changes git reset --soft HEAD~1
Create & switch to branch git switch -c branch-name
Unstage file git restore --staged file.txt
Discard changes in file git restore file.txt
View changes since last commit git diff HEAD
Temporarily save changes git stash
Update branch from main git pull origin main
See who changed each line git blame file.txt
Find commit with message git log --grep="text"
Recover deleted branch git reflog then git checkout hash

Typical Git Workflow:

  1. Pull latest changes: git pull
  2. Create feature branch: git switch -c feature
  3. Make changes and commit: git add .git commit
  4. Push branch: git push -u origin feature
  5. Create Pull Request on GitHub/GitLab
  6. After merge, delete branch: git branch -d feature