You Need Only These 10 Git Commands
You Need Only These 10 Git Commands
Let's be honest: Git can seem complicated. There are hundreds of commands, flags, and options. But here's the truth: You only need 10 commands to use Git effectively.
That's it. Just 10.
Whether you're working solo or collaborating with a team, these 10 commands will handle 99% of everything you need to do with Git. Forget the rest—they're nice-to-know, but not necessary.
Let's cut through the noise and focus on what actually matters.
1. git init
What it does: Initializes a new Git repository in your current directory.
When to use it: When starting a new project or converting an existing project to use Git.
git init
This command creates a hidden .git directory that tracks all your version control information. Once initialized, you can start tracking changes to your files.
Pro tip: You can also initialize a repository in a specific directory:
git init my-project
2. git clone
What it does: Creates a copy of an existing repository from a remote location.
When to use it: When you want to work on an existing project that's hosted on GitHub, GitLab, or another Git hosting service.
git clone https://github.com/username/repository.git
This command downloads the entire repository, including all branches and commit history, to your local machine.
Pro tip: You can clone into a specific directory:
git clone https://github.com/username/repository.git my-folder
3. git status
What it does: Shows the current state of your working directory and staging area.
When to use it: Before committing changes, to see what files have been modified, added, or deleted.
git status
This is one of the most frequently used commands. It tells you:
- Which files have been modified
- Which files are staged for commit
- Which files are untracked
- The current branch you're on
Pro tip: Use git status -s for a shorter, more compact output.
4. git add
What it does: Stages changes for commit.
When to use it: After modifying files, to prepare them for committing.
# Stage a specific file
git add filename.txt
# Stage all modified files
git add .
# Stage all files matching a pattern
git add *.js
Pro tip: Use git add -p (patch mode) to interactively choose which changes to stage. This is great for reviewing changes before committing.
5. git commit
What it does: Saves your staged changes to the repository with a descriptive message.
When to use it: After staging changes, to create a snapshot of your work.
git commit -m "Add user authentication feature"
Pro tip: Write clear, descriptive commit messages. A good commit message explains what changed and why, not just what changed.
# Bad commit message
git commit -m "fix"
# Good commit message
git commit -m "Fix login validation to prevent SQL injection"
6. git push
What it does: Uploads your local commits to a remote repository.
When to use it: After committing changes locally, to share them with your team or backup your work.
# Push to the default remote branch
git push
# Push to a specific remote and branch
git push origin main
Pro tip: On your first push, you might need to set the upstream branch:
git push -u origin main
7. git pull
What it does: Downloads changes from a remote repository and merges them into your current branch.
When to use it: Before starting work or when you want to get the latest changes from your team.
git pull
This is essentially a combination of git fetch (download changes) and git merge (integrate changes).
Pro tip: If you want to see what changes will be pulled without actually merging, use:
git fetch
git log HEAD..origin/main
8. git branch
What it does: Lists, creates, or deletes branches.
When to use it: When you want to work on features, bug fixes, or experiments without affecting the main codebase.
# List all branches
git branch
# Create a new branch
git branch feature/new-feature
# Delete a branch
git branch -d feature/old-feature
# Force delete a branch (use with caution)
git branch -D feature/old-feature
Pro tip: Use descriptive branch names that indicate the purpose:
feature/user-authenticationbugfix/login-errorhotfix/security-patch
9. git checkout / git switch
What it does: Switches between branches or restores files.
When to use it: When you want to work on a different branch or discard changes to a file.
# Switch to a branch (older syntax)
git checkout feature/new-feature
# Switch to a branch (newer, recommended syntax)
git switch feature/new-feature
# Create and switch to a new branch
git switch -c feature/new-feature
# Discard changes to a file
git checkout -- filename.txt
Pro tip: git switch is a newer, more intuitive command specifically for switching branches. Use it instead of git checkout when possible.
10. git log
What it does: Shows the commit history.
When to use it: When you want to see what changes have been made, who made them, and when.
# Basic log
git log
# One-line format
git log --oneline
# Graph view
git log --graph --oneline --all
# Search commits by message
git log --grep="authentication"
# See changes in a commit
git log -p
Pro tip: Create an alias for a prettier log view:
git config --global alias.lg "log --oneline --decorate --graph --all"
Then use git lg for a cleaner view.
11. git diff (Bonus)
What it does: Shows the differences between your working directory and the staging area, or between commits.
When to use it: Before committing, to review exactly what changes you've made.
# See unstaged changes
git diff
# See staged changes
git diff --staged
# Compare two commits
git diff commit1 commit2
Okay, technically this makes it 11 commands, but git diff is so useful for reviewing changes before committing that it deserves a mention. Consider it part of the essential toolkit.
Putting It All Together
Here's a typical workflow using these commands:
# 1. Check what's changed
git status
# 2. Stage your changes
git add .
# 3. Review what you're about to commit
git diff --staged
# 4. Commit with a descriptive message
git commit -m "Add user authentication feature"
# 5. Push to remote
git push
That's All You Need
Seriously. These 10 (okay, 11 with git diff) commands are sufficient for virtually everything you'll do with Git.
You might hear about other commands like git rebase, git stash, git cherry-pick, or git reflog. They're useful in specific situations, but you don't need them to be productive with Git. Most developers go years without touching them.
The Complete Workflow
Here's how these commands work together in real-world scenarios:
Starting a new project:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <url>
git push -u origin main
Daily development:
git pull # Get latest changes
git status # See what you've changed
git add . # Stage your changes
git commit -m "Your message" # Save your work
git push # Share with team
Working on a feature:
git branch feature/new-feature # Create branch
git switch feature/new-feature # Switch to it
# ... make changes ...
git add .
git commit -m "Add feature"
git push origin feature/new-feature
Why Stop at 10?
Git has over 100 commands. But here's the thing: complexity doesn't equal usefulness. The commands we've covered handle:
- ✅ Creating and cloning repositories
- ✅ Tracking and committing changes
- ✅ Working with branches
- ✅ Collaborating with teams
- ✅ Viewing history and changes
What more do you actually need?
Conclusion
Stop trying to learn every Git command. Master these 10, use them consistently, and you'll be more productive than developers who know 50 commands but use them inconsistently.
Git isn't about knowing everything—it's about knowing what you need. And you need these 10 commands.
That's it. Go build something. 🚀