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
- Repository Initialization
- Daily Operations
- Branch Management
- Remote Collaboration
- Undo Operations
- Information Viewing
- Advanced Techniques
- Quick Reference Table
Repository Initialization
Command | Description | Example |
---|
git init | Initialize new repository | git init project-name |
git clone | Clone remote repository | git clone https://github.com/user/repo.git |
git clone --depth=1 | Shallow 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
Operation | Command | Description |
---|
Create branch | git branch feat/new | |
Switch branch | git checkout main | |
Create+Switch | git checkout -b hotfix | |
Merge branches | git merge feature | Merge feature into current branch |
Safe delete | git branch -d old-branch | Prevents deletion of unmerged branches |
Force delete | git branch -D old-branch | Irreversible 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
Scenario | Command | Note | Example |
---|
Discard changes | git checkout – | Irreversible! | |
Unstage file | git reset HEAD | Equivalent to git reset | |
Soft reset | git reset –soft HEAD~1 | Keeps changes staged | |
Mixed reset | git reset –mixed HEAD~1 | Returns changes to workspace | |
Hard reset | git reset –hard HEAD~1 | Use cautiously | |
Revert commit | git revert HEAD | Safe undo method | |
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
Category | Essential Commands |
---|
Initialization | init, clone |
Committing | add, commit, status |
Branching | branch, checkout, merge |
Remote | remote, push, pull |
Undo | reset, revert, checkout – |
Inspection | log, 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