Git logoGit v2.40BEGINNER

Git Commands

Essential Git commands for version control - from basic operations to advanced techniques

8 min read
gitversion-controlgithubcommandsworkflow

Getting Started

Initial Setup

Configure Git for first time use with your identity

bash
💡 Use --global for system-wide settings
📌 Local config overrides global settings
⚡ Set your editor for commit messages and conflicts

Create & Clone Repositories

Start a new project or get an existing one

bash
💡 Clone creates a full copy with history
⚡ Use shallow clone for faster downloads
📌 Init creates a new .git directory

Making Changes

Check Status & Differences

See what has changed in your working directory

bash
💡 Status shows staged, modified, and untracked files
⚡ Use -s for compact status output
📌 Diff shows line-by-line changes

Stage & Commit Changes

Save your work to the repository history

bash
💡 Stage files before committing
⚠️ Amend rewrites history - don't amend pushed commits
✅ Write clear, descriptive commit messages
⚡ Use -p for selective staging

Stashing Changes

Save Work Temporarily

Store uncommitted changes for later without committing

bash
💡 Stash is a stack - newest on top
⚡ Use stash to quickly switch branches
✅ Pop removes stash after applying
📌 Apply keeps stash for reuse

Branching

Branch Management

Create, list, and delete branches for parallel development

bash
💡 Branches are lightweight pointers to commits
⚠️ -D force deletes unmerged branches
✅ Delete branches after merging
⚡ Use descriptive branch names

Merging & Rebasing

Combine changes from different branches

bash
💡 Merge preserves history, rebase rewrites it
⚠️ Never rebase public branches
✅ Use rebase for cleaning local commits
🔧 Resolve conflicts then continue

Working with Remotes

Remote Repositories

Connect and sync with remote repositories

bash
💡 Origin is the default remote name
⚠️ Force push can overwrite others' work
✅ Use --force-with-lease for safer force pushing
⚡ Fetch updates remote tracking branches

Fixing Mistakes

Undo Changes

Revert uncommitted changes in your working directory

bash
⚠️ Hard reset and clean are destructive
💡 Soft reset keeps changes for recommit
✅ Use restore for safer operations
📌 Clean removes untracked files permanently

Revert Commits

Undo committed changes by creating new commits

bash
💡 Revert is safe for public branches
⚠️ Reset rewrites history - avoid on shared branches
✅ Use reflog to recover lost commits
📌 Cherry-pick applies specific commits

Merge Conflicts

Resolve Conflicts

Handle and fix merge conflicts when combining branches

bash
💡 Conflicts show both versions in the file
✅ Always test after resolving conflicts
🔧 Use a merge tool for complex conflicts
⚡ --ours/--theirs for quick resolution

Viewing History

Log & History

Explore repository history and find specific commits

bash
💡 Use --graph for visual branch history
⚡ Combine flags for powerful searches
📌 Blame shows who changed each line
✅ Use shortlog for contribution summary

Tags & Releases

Version Tagging

Mark specific commits as releases or milestones

bash
💡 Use annotated tags for releases
✅ Follow semantic versioning (v1.2.3)
📌 Tags don't push by default
⚡ Lightweight tags for temporary marks

Advanced Techniques

Submodules

Include other Git repositories within your project

bash
💡 Submodules link to specific commits
⚠️ Remember to update submodules after pull
📌 Each submodule is a separate repository

Bisect (Find Bugs)

Binary search through history to find the commit that introduced a bug

bash
💡 Finds bugs using binary search
⚡ Automate with test scripts
✅ Efficiently narrows down problem commits

Worktrees

Work on multiple branches simultaneously in different directories

bash
💡 Each worktree is a separate working directory
⚡ Work on multiple branches without stashing
✅ Great for testing while developing

Quick Reference

Daily Workflow

Common Git workflow for everyday development

bash
💡 Always pull before starting new work
✅ Use descriptive branch names
⚡ Commit often with clear messages
📌 Delete branches after merging

Useful Aliases

Set up shortcuts for common Git commands

bash
💡 Aliases save time on common commands
⚡ Create aliases for your workflow
✅ Share useful aliases with your team