logo

Git Basics

Learn version control with Git

Git Basics

Git is a distributed version control system that helps you track changes in your code.

Basic Commands

# Initialize a repository
git init

# Clone a repository
git clone https://github.com/user/repo.git

# Check status
git status

# Add files to staging
git add .
git add filename.txt

# Commit changes
git commit -m "Your commit message"

# Push to remote
git push origin main

# Pull from remote
git pull origin main

Branching

# Create and switch to new branch
git checkout -b feature-branch

# Switch branches
git checkout main
git checkout feature-branch

# Merge branch
git checkout main
git merge feature-branch

# Delete branch
git branch -d feature-branch

Working with Remotes

# Add remote
git remote add origin https://github.com/user/repo.git

# View remotes
git remote -v

# Fetch from remote
git fetch origin

# Push to remote
git push -u origin main

Useful Commands

# View commit history
git log
git log --oneline

# View differences
git diff
git diff --staged

# Undo changes
git checkout -- filename.txt
git reset HEAD filename.txt

# Stash changes
git stash
git stash pop

Best Practices

  1. Write clear commit messages
  2. Commit often, push regularly
  3. Use branches for features
  4. Review changes before committing
  5. Keep commits focused and atomic

Git is essential for any developer - master these basics and you'll be well on your way!