Git Commands

Essential Guide for Version Control Mastery

Basic Git Workflow

1
Initialize Repository
git init
Creates a new Git repository in your current directory
2
Check Status
git status
Shows which files are staged, modified, or untracked
3
Stage Changes
git add .
Stages all changes for the next commit
4
Commit Changes
git commit -m "message"
Saves staged changes with a descriptive message
5
Push to Remote
git push origin main
Uploads your commits to the remote repository
6
Pull Updates
git pull
Downloads and merges changes from remote repository

Essential Commands by Category

📁
Setup & Configuration
git config --global user.name "Your Name"
Set your username globally
git config --global user.email "email@example.com"
Set your email globally
git clone <repository-url>
Copy a repository from remote to local
📝
Making Changes
git add <filename>
Stage specific file for commit
git add -A
Stage all changes (including deletions)
git commit -am "message"
Stage and commit tracked files in one step
git diff
Show unstaged changes
🌿
Branching
git branch
List all local branches
git branch <branch-name>
Create a new branch
git checkout <branch-name>
Switch to existing branch
git checkout -b <branch-name>
Create and switch to new branch
git merge <branch-name>
Merge branch into current branch
🔄
Remote Operations
git remote -v
Show remote repository URLs
git fetch
Download changes without merging
git push -u origin <branch>
Push branch and set upstream
git pull origin <branch>
Pull specific branch from remote
📊
History & Information
git log
Show commit history
git log --oneline
Show condensed commit history
git show <commit-hash>
Show details of specific commit
git blame <filename>
Show who changed each line
🔀
Rebasing & History
git rebase <branch>
Reapply commits on top of another branch
git rebase -i HEAD~3
Interactive rebase for last 3 commits
git rebase --continue
Continue rebase after resolving conflicts
git rebase --abort
Cancel rebase and return to original state
git pull --rebase
Pull changes and rebase instead of merge
🔧
Undoing Changes
git reset HEAD <file>
Unstage a file
git checkout -- <file>
Discard changes in working directory
git reset --soft HEAD~1
Undo last commit, keep changes staged
git revert <commit-hash>
Create new commit that undoes changes

💡 Pro Tips

Write Good Commit Messages
Use present tense and be descriptive: "Add user authentication" not "Added auth"
Check Before You Commit
Always run 'git status' and 'git diff' before committing to avoid surprises
Use .gitignore
Create a .gitignore file to exclude files like node_modules, .env, and build artifacts
Commit Often
Make small, focused commits. It's easier to track changes and debug issues
Pull Before Push
Always pull the latest changes before pushing to avoid merge conflicts
Use Rebase for Clean History
Use 'git pull --rebase' to avoid unnecessary merge commits and keep history linear
Interactive Rebase for Cleanup
Use 'git rebase -i' to squash, reorder, or edit commits before pushing to shared branches
Never Rebase Public Commits
Don't rebase commits that have been pushed to shared branches - it rewrites history