Git Basics You Must Know
These commands are non-negotiable. You'll use them daily.
The Big Picture
Configuration (One-Time Setup)
# Set your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Enable color output
git config --global color.ui auto
# Set default editor
git config --global core.editor "code --wait" # VS Code
# OR
git config --global core.editor "vim" # Vim
# Verify configuration
git config --listEssential Commands
1. Getting a Repository
# Clone an existing repository
git clone https://github.com/username/repo.git
# Initialize a new repository
git init
# Clone specific branch
git clone -b branch-name https://github.com/username/repo.git2. Checking Status
# See current state
git status
# Compact status
git status -s
# See what's different
git diff
# See staged changes
git diff --stagedStatus output explained:
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
modified: src/app.js ← Changed but not staged
Untracked files:
new-file.txt ← Git doesn't know about this
Changes to be committed:
new file: staged-file.js ← Ready to commit3. Staging Changes
# Stage specific file
git add filename.js
# Stage all changes in current directory
git add .
# Stage all changes everywhere
git add -A
# Stage parts of a file interactively
git add -p filename.js
# Unstage a file
git restore --staged filename.js4. Committing Changes
# Commit with message
git commit -m "Your commit message"
# Commit with detailed message (opens editor)
git commit
# Add all tracked files and commit
git commit -am "Message"
# Amend last commit
git commit --amend -m "New message"5. Viewing History
# View commit history
git log
# Compact log
git log --oneline
# Log with graph
git log --oneline --graph --all
# Show specific commit
git show commit-hash
# Show changes in last 5 commits
git log -5 -pSample log output:
* a1b2c3d (HEAD -> main) feat: add user authentication
* d4e5f6g fix: resolve login bug
* g7h8i9j docs: update README
* j0k1l2m initial commit6. Working with Remotes
# List remotes
git remote -v
# Add remote
git remote add origin https://github.com/you/repo.git
git remote add upstream https://github.com/original/repo.git
# Remove remote
git remote remove origin
# Change remote URL
git remote set-url origin new-url7. Pushing and Pulling
# Push to remote
git push origin branch-name
# Push and set upstream
git push -u origin branch-name
# Pull changes
git pull origin main
# Fetch without merging
git fetch originCommand Flow Diagram
File Lifecycle
Understanding Output
git status
| Symbol | Meaning |
|---|---|
M | Modified |
A | Added |
D | Deleted |
R | Renamed |
?? | Untracked |
git log --oneline
a1b2c3d (HEAD -> feature, origin/feature) Latest commit
d4e5f6g Previous commit
g7h8i9j (origin/main, main) Older commitHEAD= Where you are noworigin/feature= Remote branch positionmain= Local main branch
Common Scenarios
Scenario 1: Starting Fresh
# Fork on GitHub first, then:
git clone https://github.com/YOUR-USERNAME/project.git
cd project
git remote add upstream https://github.com/ORIGINAL/project.git
git checkout -b my-featureScenario 2: Daily Workflow
# Start of day
git checkout main
git pull upstream main
git push origin main
# Start work
git checkout -b feature/new-thing
# Make changes, then:
git add .
git commit -m "feat: add new thing"
git push origin feature/new-thingScenario 3: Oops, Wrong Branch
# Made changes on wrong branch?
git stash # Save changes
git checkout correct-branch # Switch
git stash pop # Restore changesScenario 4: Undo Last Commit
# Keep changes, undo commit
git reset --soft HEAD~1
# Discard changes, undo commit
git reset --hard HEAD~1
# Already pushed? See rewriting-historyQuick Reference Table
| Task | Command |
|---|---|
| Clone repo | git clone <url> |
| Check status | git status |
| Stage files | git add <file> |
| Stage all | git add . |
| Commit | git commit -m "msg" |
| Push | git push origin <branch> |
| Pull | git pull origin <branch> |
| Create branch | git checkout -b <name> |
| Switch branch | git checkout <name> |
| View log | git log --oneline |
| View diff | git diff |
| Stash changes | git stash |
| Apply stash | git stash pop |
.gitignore Basics
Create .gitignore to exclude files:
# Dependencies
node_modules/
vendor/
venv/
# Build outputs
dist/
build/
*.pyc
# Environment files
.env
.env.local
# IDE settings
.vscode/
.idea/
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
logs/Practice Exercises
Exercise 1: Basic Flow
mkdir git-practice && cd git-practice
git init
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"
git logExercise 2: Branching
git checkout -b feature
echo "New feature" > feature.txt
git add feature.txt
git commit -m "Add feature"
git checkout main
git merge featureExercise 3: Remotes
# Create repo on GitHub first
git remote add origin https://github.com/you/repo.git
git push -u origin mainCommon Mistakes
❌ Mistakes to Avoid
# Committing without reviewing
git add . && git commit -m "stuff" # ❌
# Not pulling before pushing
git push # ❌ May cause conflicts
# Committing sensitive data
git add .env # ❌ NEVER!✅ Best Practices
# Review before committing
git status
git diff
git add specific-file.js
git commit -m "descriptive message"
# Always pull first
git pull origin main
git push origin main
# Use .gitignore
echo ".env" >> .gitignoreTroubleshooting
"fatal: not a git repository"
# You're not in a git repo
cd /path/to/your/repo
# OR initialize one
git init"error: failed to push"
# Remote has changes you don't
git pull origin branch-name
# Resolve any conflicts, then push
git push origin branch-name"Changes not staged for commit"
# Need to stage before committing
git add filename
git commit -m "message"Next Steps
Now that you know the basics:
Remember: Practice these commands daily. Within a week, they'll become muscle memory.