Complete Git Commands Tutorial – Master Version Control
Version: Git 2.x+ | Difficulty: Beginner to Advanced | Type: Version Control System
Git is the most widely used distributed version control system in software development today. From basic commits to advanced rebasing and conflict resolution, mastering Git commands is essential for every developer. This comprehensive tutorial covers everything you need to know to work efficiently with Git. Explore advanced Git workflows.
Essential Git Command Categories
- Status & Information: Check repository state, view history, and inspect changes.
- Committing & Staging: Stage files, create commits, and manage commit history.
- Branching & Merging: Create branches, switch between them, and merge changes.
- Remote Operations: Push, pull, fetch, and sync with remote repositories.
- Undoing Changes: Revert commits, discard changes, and reset repository state.
- Stashing: Temporarily save uncommitted changes for later use.
- Rebasing: Reapply commits on top of another base branch for cleaner history.
- Conflict Resolution: Handle merge conflicts and resolve differences between branches.
Common Git Commands Examples
Check Repository Status
# View current status
git status
# View changes in working directory
git diff
# View staged changes
git diff --staged
# View commit history
git log --oneline --graph --all
Stage and Commit Changes
# Stage specific file
git add filename.txt
# Stage all changes
git add .
# Commit with message
git commit -m "Add new feature"
# Stage and commit in one command (tracked files only)
git commit -am "Quick commit"
Branch Management
# Create and switch to new branch
git checkout -b feature-branch
# Switch branches
git checkout main
git switch main # Newer syntax
# List all branches
git branch -a
# Delete branch
git branch -d feature-branch
Sync with Remote Repository
# Fetch latest changes
git fetch origin
# Pull changes (fetch + merge)
git pull origin main
# Push changes
git push origin feature-branch
# Set upstream and push
git push -u origin feature-branch
Revert and Clean Changes
# Discard changes to tracked files
git restore .
git restore filename.txt
# Remove untracked files
git clean -fd
# Hard reset to match remote
git fetch origin
git reset --hard origin/main
Git Commands Quick Reference
| Command | Description |
|---|---|
git status |
Show working directory status |
git add . |
Stage all changes for commit |
git commit -m "message" |
Create a commit with message |
git push |
Push commits to remote repository |
git pull |
Fetch and merge remote changes |
git branch |
List/create/delete branches |
git checkout |
Switch branches or restore files |
git merge |
Merge branches together |
git rebase |
Reapply commits on top of another branch |
git stash |
Temporarily save uncommitted changes |
Git Workflow Best Practices
Effective Git Workflow involves understanding when to use different commands and following best practices for collaboration.
- Commit Often: Make small, focused commits with clear messages describing what and why.
- Branch Strategy: Use feature branches for new work, keep main/master stable.
- Pull Before Push: Always fetch/pull latest changes before pushing to avoid conflicts.
- Review Before Merge: Review changes with
git diffbefore merging branches. - Write Good Messages: Use descriptive commit messages following conventional commit format.
Example: Feature Branch Workflow
# 1. Start from latest main
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/new-feature
# 3. Make changes and commit
git add .
git commit -m "feat: add new feature"
# 4. Push feature branch
git push -u origin feature/new-feature
# 5. After code review, merge to main
git checkout main
git pull origin main
git merge feature/new-feature
git push origin main
Ready to master advanced Git? Explore advanced Git workflows and collaboration patterns — learn Git hooks, submodules, and more!
Merge vs Rebase are two different ways to integrate changes from one branch into another, each with distinct advantages.
✅ Git Merge:
# Creates a merge commit combining both branches
git checkout main
git merge feature-branch
Pros: Preserves complete history, non-destructive, safe for shared branches. Cons: Creates merge commits, can clutter history.
✅ Git Rebase:
# Reapplies commits on top of target branch
git checkout feature-branch
git rebase main
Pros: Cleaner linear history, easier to follow. Cons: Rewrites commit history, can be dangerous on shared branches.
🔁 When to Use Each:
- Merge: Use for shared/public branches, when you want to preserve exact history
- Rebase: Use for local feature branches before merging, to clean up history
Undoing Changes in Git depends on what stage your changes are at.
✅ Discard Unstaged Changes:
# Discard changes to specific file
git restore filename.txt
# Discard all unstaged changes
git restore .
This reverts files to their last committed state.
✅ Unstage Files:
# Unstage specific file
git restore --staged filename.txt
# Unstage all files
git reset HEAD
Files remain modified but are no longer staged for commit.
✅ Undo Last Commit (Keep Changes):
# Soft reset - keeps changes staged
git reset --soft HEAD~1
# Mixed reset - keeps changes unstaged (default)
git reset HEAD~1
✅ Undo Last Commit (Discard Changes):
# Hard reset - discards all changes
git reset --hard HEAD~1
Warning: This permanently deletes changes!
🔒 Best Practices:
- Use
git restorefor unstaged changes - Use
git resetcarefully - it rewrites history - Use
git revertfor commits already pushed (creates new commit) - Always check
git statusbefore undoing changes
git revert is safer than git reset for commits that have been pushed, as it creates a new commit rather than rewriting history.
Syncing a Fork involves adding the original repository as an upstream remote and pulling its changes.
✅ Step-by-Step Process:
# 1. Add upstream remote (if not already added)
git remote add upstream https://github.com/ORIGINAL_ORG/repo.git
# 2. Fetch latest changes from upstream
git fetch upstream
# 3. Checkout your main branch
git checkout main
# 4. Merge upstream changes (Option A - Merge)
git merge upstream/main
# OR rebase upstream changes (Option B - Rebase)
git rebase upstream/main
# 5. Push updated main branch
git push origin main
# 6. Update your feature branch
git checkout feature-branch
git rebase main
git push origin feature-branch --force-with-lease
This keeps your fork synchronized with the original repository while preserving your local changes.
🔁 Key Commands:
git remote add upstream [URL]- Add original repo as upstreamgit fetch upstream- Download changes without merginggit merge upstream/main- Merge upstream changesgit rebase upstream/main- Rebase your branch on upstream
--force-with-lease instead of --force when pushing rebased branches. It's safer and prevents overwriting others' work.
Git Stash allows you to temporarily save uncommitted changes so you can switch branches or pull updates without committing incomplete work.
✅ Basic Stash Operations:
# Save current changes
git stash
git stash save "Work in progress on feature X"
# List all stashes
git stash list
# Apply most recent stash (keeps stash)
git stash apply
# Apply and remove stash
git stash pop
# Apply specific stash
git stash apply stash@{0}
# Drop a stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
✅ Advanced Stash Options:
# Stash including untracked files
git stash -u
# Stash including ignored files
git stash -a
# Create a branch from stash
git stash branch new-branch-name stash@{0}
🔁 Common Use Cases:
- Switching branches: Stash changes before switching to another branch
- Pulling updates: Stash local changes, pull latest, then reapply stash
- Testing clean state: Stash changes to test code without your modifications
git stash save "message" to identify stashes later. Stashes are stored locally and not pushed to remote repositories.
Frequently Asked Questions
Q1. What's the difference between git pull and git fetch?
A: git fetch downloads changes from remote without merging, while git pull is equivalent to git fetch followed by git merge. Use git fetch when you want to review changes before merging.
Q2. How do I resolve merge conflicts?
A: When conflicts occur, Git marks conflicted files. Edit the files to resolve conflicts (look for <<<<<<< markers), then stage resolved files with git add and complete the merge with git commit or git rebase --continue.
Q3. Should I use merge or rebase?
A: Use merge for shared/public branches to preserve history. Use rebase for local feature branches before merging to maintain a cleaner, linear history. Never rebase commits that others might be using.
Q4. How do I completely reset my repository to match remote?
A: Use git fetch origin && git reset --hard origin/main (replace 'main' with your branch name). This discards all local changes and matches the remote exactly. Use with caution!
Q5. What does "detached HEAD" state mean?
A: Detached HEAD occurs when you checkout a specific commit instead of a branch. Your commits won't be associated with any branch. Create a new branch with git checkout -b new-branch to save your work.

