Essential Git Commands
A quick reference for the most frequent Git commands you’ll use in everyday development.
1. Initialize a Repository
# Create a new local repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
2. Checking Status and Adding Files
# See which files have changed
git status
# Add a specific file to staging
git add filename.txt
# Add all changed files to staging
git add .
Pro tip:
It’s generally better to use git add -p to stage changes interactively. This lets you review what you’re committing line-by-line!
3. Committing Changes
# Commit staged changes with a message
git commit -m "Add new feature"
# Add all tracked files AND commit in one step
git commit -am "Update existing features"
4. Branching
# List all local branches
git branch
# Create a new branch and switch to it
git checkout -b new-feature-branch
# Switch back to main
git checkout main
5. Syncing with Remote
# Fetch latest changes without merging
git fetch origin
# Fetch and merge latest changes
git pull origin main
# Push your local commits to the remote branch
git push origin new-feature-branch