正在加载,请稍候…

Git Command Cheat Sheet: The Most Useful Commands Explained

A comprehensive Git reference covering everyday commands, branching, undo operations, and advanced workflows.

Git: The Essential Version Control System

Git is the world's most widely used distributed version control system, created by Linus Torvalds in 2005 for Linux kernel development. Understanding Git's core commands and concepts is essential for every developer.

Core Git Concepts

Repository (Repo)

A Git repository is a directory that Git tracks. It contains all project files plus a hidden .git folder with the full version history.

Working Tree, Staging Area, and Repository

Git has three areas where file changes live:

  1. Working tree: Your actual files on disk
  2. Staging area (index): Files marked for the next commit
  3. Repository: Committed history stored in .git

Branches

A branch is a lightweight pointer to a specific commit. The default branch is usually called main (formerly master). Branching enables parallel development without affecting the main codebase.

Commits

A commit is a snapshot of staged files at a point in time. Each commit has a unique SHA-1 hash, author, timestamp, and message.

Essential Git Commands

Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git init                    # Initialize new repo
git clone <url>             # Clone existing repo

Daily Workflow

git status                  # Check file status
git add .                   # Stage all changes
git add <file>              # Stage specific file
git commit -m "message"     # Commit with message
git push origin main        # Push to remote
git pull                    # Fetch and merge from remote

Branching

git branch                  # List branches
git branch feature-name     # Create branch
git checkout feature-name   # Switch to branch
git checkout -b new-branch  # Create and switch
git merge feature-branch    # Merge into current
git branch -d feature-name  # Delete branch

History and Diffs

git log --oneline           # Compact log
git log --graph --all       # Visual branch graph
git diff                    # Unstaged changes
git diff --staged           # Staged changes
git show <commit>           # Show commit details

Undoing Changes

git restore <file>          # Discard working tree changes
git restore --staged <file> # Unstage file
git revert <commit>         # Create undo commit
git reset --soft HEAD~1     # Undo last commit, keep changes staged
git reset --hard HEAD~1     # Undo last commit, discard changes

Remote Operations

git remote add origin <url>    # Add remote
git remote -v                  # List remotes
git fetch origin               # Download without merging
git push --set-upstream origin main  # First push
git pull --rebase              # Rebase instead of merge

Stashing

git stash                   # Save uncommitted work
git stash list              # View saved stashes
git stash pop               # Apply and remove latest stash
git stash apply stash@{1}   # Apply specific stash

Git Workflow Strategies

Feature Branch Workflow

Create a branch for each feature or bug fix:

  1. git checkout -b feature/new-feature
  2. Make commits
  3. Open pull request
  4. Review and merge to main

Gitflow

More structured workflow with main, develop, feature, release, and hotfix branches. Suited for projects with scheduled releases.

Trunk-Based Development

Short-lived feature branches merged to main frequently. Works well with CI/CD and feature flags.

Useful Git Aliases

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --all"

Using the Git Memo Reference

Our Git memo tool provides an interactive reference for:

  1. All common Git commands organized by category
  2. Quick-reference cheat sheet for daily operations
  3. Workflow diagrams for branching strategies
  4. Explanation of Git concepts for beginners
  5. Advanced commands for rebasing, cherry-picking, and bisecting

Keep it open while working for quick command lookups without leaving your terminal.