The Ultimate Git Commands Guide - 2025 Developer’s Essential Cheat Sheet

2024 Stack Overflow Developer Survey Reveals: 87% of developers use Git daily, yet 65% of juniors struggle with advanced commands. This article serves as your ultimate Git command reference, covering essential operations from basic to expert level.

Whether fixing critical bugs or managing large projects, mastering core Git commands can multiply your development efficiency. Based on GitHub’s official documentation and real-world scenarios, this guide compiles golden command collections to help build a complete Git knowledge system.


Table of Contents

  1. Repository Initialization
  2. Daily Operations
  3. Branch Management
  4. Remote Collaboration
  5. Undo Operations
  6. Information Viewing
  7. Advanced Techniques
  8. Quick Reference Table

Repository Initialization

CommandDescriptionExample
git initInitialize new repositorygit init project-name
git cloneClone remote repositorygit clone https://github.com/user/repo.git
git clone --depth=1Shallow clone (latest commit only)git clone --depth=1 https://github.com/user/repo.git

Daily Operations

Basic Workflow

git add <file>        # Stage a single file
git add .            # Stage all changes
git commit -m "msg"  # Commit changes

Commit Amendment

git commit --amend   # Modify last commit (keeps hash)

Cherry-picking

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

Branch Management

OperationCommandDescription
Create branchgit branch feat/new
Switch branchgit checkout main
Create+Switchgit checkout -b hotfix
Merge branchesgit merge featureMerge feature into current branch
Safe deletegit branch -d old-branchPrevents deletion of unmerged branches
Force deletegit branch -D old-branchIrreversible deletion of branches

Remote Collaboration

git remote -v                       # List remote repositories
git remote add origin <repository>  # Add remote repository
git push origin <branch-name>       # Push local commits
git pull origin develop             # Fetch and merge changes
git fetch                           # Sync remote references
git fetch --prune                   # Remove obsolete remote branches

Undo Operations

ScenarioCommandNoteExample
Discard changesgit checkout – Irreversible!
Unstage filegit reset HEAD Equivalent to git reset
Soft resetgit reset –soft HEAD~1Keeps changes staged
Mixed resetgit reset –mixed HEAD~1Returns changes to workspace
Hard resetgit reset –hard HEAD~1Use cautiously
Revert commitgit revert HEADSafe undo method

Information Viewing

git status                  # Current state
git log --oneline --graph   # Visualized history
git diff                    # Compare working directory vs staged
git diff --staged           # Compare staged vs last commit
git diff HEAD~3             # Compare last 3 commits
git diff <b1>..<b2>         # Compare branches
git show abc123             # Inspect commit details

Advanced Techniques

Stashing

git stash        # Temporary save
git stash pop    # Restore most recent stash
git stash apply  # Apply specific stash
git stash list   # List stashes

History Rewriting

git commit --amend -m "New message" # Modify last commit message
git rebase -i HEAD~5  # Interactive rebase for last 5 commits

Tag Management

git tag v1.0.0         # Create lightweight tag
git push --tags        # Push all tags

Configuration

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global core.editor "vim"
git config --global alias.co checkout
git config --list

Debugging with Bisect

git bisect start
git bisect bad
git bisect good abcdefg
git bisect reset

Quick Reference Table

CategoryEssential Commands
Initializationinit, clone
Committingadd, commit, status
Branchingbranch, checkout, merge
Remoteremote, push, pull
Undoreset, revert, checkout –
Inspectionlog, diff, show

Pro Tips:

  • Always check git status before committing
  • Create feature branches for major changes
  • Prefer revert over reset for public commits
  • Master these commands to handle 90% of daily development scenarios

SUBSCRIBE FREE PROMOTIONS


🔒 No spam. Unsubscribe any time.

About Tanya

Tanya

Tanya is a professional editor and writer with a passion for transforming ideas into compelling narratives..

» More about Tanya