Git is the undisputed standard for distributed version control in modern software engineering. Powers open-source projects, enterprise codebases, automated CI/CD pipelines, and team collaboration across millions of repositories worldwide.
Despite using Git daily, many developers rely on a small subset of basic commands (git add, git commit, git push) and panic when merge conflicts occur, commits are accidentally wiped, or rebase operations diverge.
In this exhaustive technical guide, we will break down the internal architecture of Git and provide a definitive reference guide for 25 essential Git commands, organized by real-world workflow categories.
Check code diffs and compare file revisions instantly using our free Text & Code Diff Checker.
The Four Stages of Git Architecture
Before executing commands, it is crucial to understand where your file modifications reside within Git's four internal architecture environments:
- Working Directory: The actual local filesystem files you edit in your IDE.
- Staging Area (Index): The preview environment holding modifications staged for the next commit.
- Local Repository (
.git): Committed snapshot history stored on your local disk. - Remote Repository (GitHub / GitLab): The central server repository shared with team members.
Working Directory Staging Index Local Repository Remote Repository
┌─────────────────┐ ┌──────────────┐ ┌──────────────────┐ ┌──────────────────┐
│ Modified Code │─>│ git add . │──>│ git commit │──>│ git push │
└─────────────────┘ └──────────────┘ └──────────────────┘ └──────────────────┘Category 1: Environment Setup & Configuration
1. git config
Configures user identity, line endings, text editors, and default branch names.
# Set global committer name and email
git config --global user.name "Alex Dev"
git config --global user.email "alex.com"
# Set default main branch name for git init
git config --global init.defaultBranch main
# Set VS Code as default git editor
git config --global core.editor "code --wait"2. git init & git clone
Initializes a new Git repository or downloads an existing remote repository.
# Initialize local repo
git init
# Shallow clone with limited depth for fast CI builds
git clone --depth 1 https://github.com/org/repo.git
# Clone single branch with submodules
git clone --branch main --recurse-submodules https://github.com/org/repo.gitCategory 2: Staging, Status & File History
3. git status
Inspects modified, staged, or untracked files across your working tree.
# Short formatted status output
git status -s4. git add
Stages modified files into the Index environment.
# Stage specific file
git add src/App.tsx
# Interactive patch mode: review and stage individual line chunks
git add -p5. git commit
Saves staged snapshot into local repository commit history.
# Commit staged changes with message
git commit -m "feat(auth): implement OAuth token refresh"
# Amend last commit without changing commit message
git commit --amend --no-edit6. git diff
Compares file changes between working tree, index, and commits.
# Compare working directory against staging index
git diff
# Compare staged changes against last commit
git diff --staged
# Word-level diff inspection
git diff --word-diff7. git log
Displays commit history graph.
# Concise visual commit graph
git log --oneline --graph --decorate --all -n 15Category 3: Branching & Merging
8. git branch
Lists, creates, renames, or deletes branches.
# List all local and remote branches
git branch -a
# Create feature branch
git branch feature/user-profile
# Delete branch safely (fails if unmerged)
git branch -d feature/user-profile9. git switch / git checkout
Switches active working branch or resets files.
# Modern command to create and switch branch
git switch -c feature/payment-gateway10. git merge
Combines branch histories into active branch.
# Non-fast-forward merge preserving explicit merge commit node
git merge --no-ff feature/payment-gateway11. git rebase
Replays feature branch commits sequentially on top of target branch.
# Interactive rebase last 4 commits (squash, reword, drop)
git rebase -i HEAD~412. git cherry-pick
Applies changes from specific commits onto current branch.
git cherry-pick <commit-sha>Category 4: Undoing Mistakes & Disaster Recovery
13. git stash
Temporarily shelves uncommitted working changes.
# Stash with descriptive name including untracked files
git stash push -u -m "WIP header layout"
# Pop last stashed snapshot
git stash pop14. git restore
Restores working tree files or un-stages files.
# Unstage file from index
git restore --staged src/App.tsx
# Discard all local working directory edits
git restore .15. git reset
Resets current HEAD to specified commit state.
--soft: Moves HEAD, retains changes staged in index.--mixed(Default): Moves HEAD, un-stages changes into working directory.--hard: Moves HEAD, discards all working edits.
git reset --soft HEAD~116. git revert
Creates a new commit reversing changes of a prior commit without modifying historical commits.
git revert <commit-sha>17. git reflog
Logs every single HEAD pointer movement on your local machine. Ultimate recovery tool!
git reflog
# Recover deleted branch
git checkout -b recovered-branch <reflog-sha>Category 5: Remote Synchronization & Advanced Utilities
18. git fetch
Downloads commits and refs from remote repository without merging.
git fetch origin --prune19. git pull
Fetches and merges remote changes.
# Rebase local commits on top of incoming remote changes
git pull --rebase origin main20. git push
Uploads local commits to remote repository.
# Safe force push verifying remote branch state
git push --force-with-lease origin feature/auth21. git bisect
Performs binary search across commit history to locate exact commit that introduced a regression.
git bisect start
git bisect bad # Current HEAD has bug
git bisect good v1.0.0 # Known good release tag
# Git checks out intermediate commit for testing!
git bisect reset22. git blame
Inspects author and commit info for each line in a file.
git blame -L 40,60 src/server.ts23. git clean
Removes untracked files and directories.
# Dry run preview
git clean -nd
# Force delete untracked files and folders
git clean -fd24. git tag
Creates annotated tags for release milestones.
git tag -a v2.0.0 -m "Release version 2.0.0"
git push origin v2.0.025. git worktree
Manages multiple linked working trees attached to the same repository simultaneously!
# Add worktree directory for hotfix while keeping current branch open
git worktree add ../hotfix-dir hotfix/login-bugCompare file revisions and code diffs instantly using our free Diff Checker.
Frequently Asked Questions
Q1. What is the difference between git reset --soft, --mixed, and --hard?
git reset --soft moves HEAD to a target commit while keeping changes staged in the index. --mixed (default) keeps changes in the working directory but un-stages them. --hard discards all uncommitted changes in both index and working directory.
Q2. Why should I use git push --force-with-lease instead of git push --force?
git push --force blindly overwrites the remote branch even if a team member pushed new commits in the interim. --force-with-lease checks that your local ref matches the remote tracking branch, aborting if remote changes would be accidentally wiped.
Q3. How does git rebase differ from git merge?
git merge combines two branch histories by creating a new merge commit preserving exact chronological branch history. git rebase rewrites linear history by replaying your feature branch commits sequentially on top of the target branch.
Q4. How do I recover a deleted commit or branch in Git?
Run git reflog to view the chronological log of all HEAD position changes. Find the commit SHA prior to deletion, then run git checkout -b recovered-branch <commit-sha>.
Compare Code with Diff Checker
Need to see exactly what changed between two versions of a file? Use our professional-grade Diff Checker to visualize deletions, additions, and line-level modifications.
Open Diff Checker