Git
Git is a distributed version control system that tracks changes in source code during software development. It enables collaboration, maintains history, and allows multiple developers to work on the same project simultaneously without conflicts.
Why Git?
- Version Control - Track every change, revert mistakes, view history
- Collaboration - Multiple developers working on the same codebase
- Branching - Experiment without affecting main code
- Distributed - Every developer has complete history locally
- Industry Standard - Used by virtually every software team
Interactive Git Workflow Simulator
Simulate common Git operations and see how your repository state changes:
Git Commands
Repository State
Command Output
Essential Git Commands
Setup & Configuration
# Configure Git (first time setup)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Check configuration
git config --list
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/username/repo.git Basic Workflow
# Check status of your files
git status
# Add files to staging area
git add filename.txt # Add specific file
git add . # Add all files
git add *.js # Add all .js files
# Commit changes
git commit -m "Your commit message"
# Commit with detailed message
git commit -m "Short description" -m "Longer explanation of changes"
# View commit history
git log
git log --oneline # Condensed view
git log --graph --all # Visual branch graph Branching & Merging
# Create a new branch
git branch feature-name
# Switch to a branch
git checkout feature-name
# Create and switch in one command
git checkout -b feature-name
# Modern alternative (Git 2.23+)
git switch feature-name
git switch -c feature-name
# List branches
git branch # Local branches
git branch -a # All branches (local + remote)
# Merge a branch
git checkout main
git merge feature-name
# Delete a branch
git branch -d feature-name # Safe delete
git branch -D feature-name # Force delete Working with Remotes
# Add a remote repository
git remote add origin https://github.com/username/repo.git
# View remotes
git remote -v
# Push to remote
git push origin main # Push main branch
git push -u origin main # Push and set upstream
# Pull from remote
git pull origin main
# Fetch changes without merging
git fetch origin
# Push a new branch
git push -u origin feature-name Undoing Changes
# Undo changes in working directory
git checkout -- filename.txt # Discard changes to file
# Unstage a file
git reset filename.txt
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Amend last commit
git commit --amend -m "New message"
# Revert a commit (creates new commit)
git revert commit-hash
# Stash changes temporarily
git stash
git stash list
git stash apply
git stash pop Advanced Commands
# Cherry-pick a specific commit
git cherry-pick commit-hash
# Rebase (reapply commits on top of another branch)
git rebase main
# Interactive rebase (edit commit history)
git rebase -i HEAD~3
# Show changes
git diff # Working directory changes
git diff --staged # Staged changes
git diff main feature # Compare branches
# Search commits
git log --grep="bug fix" # Search commit messages
git log -S "function name" # Search code changes
# Tags
git tag v1.0.0 # Create lightweight tag
git tag -a v1.0.0 -m "Version 1.0.0" # Annotated tag
git push origin v1.0.0 # Push tag Git Workflow Strategies
Feature Branch Workflow
- Create a branch for each feature
- Merge back to main when complete
- Keep main always deployable
Gitflow
main- Production-ready codedevelop- Integration branchfeature/*- New featuresrelease/*- Release preparationhotfix/*- Emergency fixes
GitHub Flow
- Main branch is always deployable
- Create descriptive branches for changes
- Open pull request for review
- Merge after approval and CI passes
Best Practices
- Commit Often - Small, frequent commits are easier to understand and revert
- Write Good Messages - Clear, descriptive commit messages explain why, not what
- Use Branches - Never commit directly to main for features
- Pull Before Push - Always pull latest changes before pushing
- Review Diffs - Check what you're committing with git diff
- Don't Commit Secrets - Never commit passwords, API keys, or sensitive data
- Use .gitignore - Exclude build artifacts, dependencies, and system files
Commit Message Guidelines
Follow the conventional commits specification:
feat: add user authentication
fix: resolve login redirect bug
docs: update API documentation
style: format code with prettier
refactor: simplify validation logic
test: add unit tests for auth service
chore: update dependencies
# Detailed example:
feat: implement password reset functionality
- Add password reset email template
- Create reset token generation
- Add expiration handling for tokens
- Update user model with resetToken field
Closes #123 Common Pitfalls to Avoid
- Large Commits - Avoid committing hundreds of changes at once
- Vague Messages - "fix stuff" or "update" don't help anyone
- Committing Build Files - Use .gitignore for generated files
- Force Pushing to Main - Never use
git push --forceon shared branches - Not Pulling First - Always pull before starting work
- Ignoring Merge Conflicts - Resolve conflicts carefully, don't just accept all changes
Useful Git Aliases
Add these to your ~/.gitconfig to save time:
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = log --graph --oneline --all
aliases = config --get-regexp alias Resources
- Official Docs - git-scm.com/doc
- GitHub Guides - guides.github.com
- Interactive Learning - learngitbranching.js.org
- Cheat Sheet - GitHub Git Cheat Sheet
- Pro Git Book - Free online book