1. Setup & Configuration
git config --global user.name "Name"
Description: Set your name globally.
Example: git config --global user.name "Rahul Sharma"
git config --global user.email "email"
Description: Set your email globally.
Example: git config --global user.email "rahul@example.com"
git config --global --list
Description: View all global configurations.
Example: git config --global --list
git --version
Description: Check Git version.
Example: git --version
3. Staging & Committing
git status
Description: Show working tree status.
Example: git status
git add .
Description: Stage all changes.
Example: git add .
git add -p
Description: Interactive staging (patch mode).
Example: git add -p
git commit -m "msg"
Description: Commit with message.
Example: git commit -m "Add user authentication"
git commit --amend
Description: Modify the last commit.
Example: git commit --amend
4. Branching & Merging
git branch
Description: List local branches.
Example: git branch
git branch -a
Description: List all branches (local + remote).
Example: git branch -a
git switch -c <name>
Description: Create and switch to new branch.
Example: git switch -c feature/payment
git merge <branch>
Description: Merge branch into current.
Example: git merge main
git rebase <branch>
Description: Rebase current branch onto another (advanced).
Example: git rebase main
git rebase -i HEAD~3
Description: Interactive rebase (edit history).
Example: git rebase -i HEAD~3
5. Remote & Collaboration
git remote -v
Description: Show remotes.
Example: git remote -v
git push origin <branch>
Description: Push to remote.
Example: git push origin main
git pull --rebase
Description: Pull with rebase.
Example: git pull --rebase origin main
git fetch --all
Description: Fetch from all remotes.
Example: git fetch --all
6. Inspection & History
git log --oneline --graph --all
Description: Beautiful commit history.
Example: git log --oneline --graph --all
git diff
Description: Show unstaged changes.
Example: git diff
git blame <file>
Description: See who changed each line.
Example: git blame app.js
7. Undo & Recovery
git reset --soft HEAD~1
Description: Undo commit (keep changes staged).
Example: git reset --soft HEAD~1
git reset --hard HEAD~1
Description: Hard undo (dangerous).
Example: git reset --hard HEAD~1
git revert <commit>
Description: Safely undo a commit.
Example: git revert a1b2c3d
git reflog
Description: Recover lost commits.
Example: git reflog
git cherry-pick <commit>
Description: Apply specific commit.
Example: git cherry-pick 8f2a9c1
8. Stash, Tag & Advanced
git stash
Description: Save uncommitted changes.
Example: git stash
git stash pop
Description: Apply and remove stash.
Example: git stash pop
git tag -a v1.2.0 -m "msg"
Description: Create annotated tag.
Example: git tag -a v1.2.0 -m "Release 1.2"
git clean -fd
Description: Remove untracked files/folders.
Example: git clean -fd
git bisect start
Description: Binary search for bugs.
Example: git bisect start
git submodule update --init --recursive
Description: Initialize submodules.
Example: git submodule update --init --recursive