Git is the standard version control system for development teams. You do not need to memorize every command; a small set covers most day-to-day work. Here is a practical reference with when to use each.
Checking status and history
git status
See what has changed:
git status
Shows:
- Modified files (not staged)
- Staged files (ready to commit)
- Untracked files (new)
- Current branch
Use it often to know where you are.
git log
View commit history:
git log # Full history
git log --oneline # Compact view
git log -n 5 # Last 5 commits
git log --graph # Branch visualization
git log --author="name" # Filter by author
git log -- path/to/file # History of specific file
git diff
See what changed:
git diff # Unstaged changes
git diff --staged # Staged changes
git diff HEAD~1 # Changes since last commit
git diff branch1..branch2 # Difference between branches
Saving changes
git add
Stage files for commit:
git add file.js # Stage specific file
git add . # Stage all changes
git add -p # Stage interactively (review each change)
Only staged changes are included in the next commit.
git commit
Create a commit:
git commit -m "Add user login feature"
git commit # Opens editor for message
git commit -am "message" # Add all tracked + commit
Write clear, present-tense messages. Describe what the commit does.
git commit --amend
Modify the last commit:
git commit --amend -m "Better message"
git commit --amend --no-edit # Add staged changes to last commit
Only amend commits you have not pushed, or coordinate with your team.
Syncing with remote
git pull
Get and merge remote changes:
git pull # Pull current branch
git pull origin main # Pull specific branch
git pull --rebase # Rebase instead of merge
Run before starting work to avoid conflicts later.
git push
Send commits to remote:
git push # Push current branch
git push origin main # Push specific branch
git push -u origin feature # Set upstream for new branch
git fetch
Download without merging:
git fetch # Fetch all remotes
git fetch origin # Fetch specific remote
Safer when you want to inspect before merging.
Branches
git branch
List and manage branches:
git branch # List local branches
git branch -a # List all (including remote)
git branch feature-x # Create new branch
git branch -d feature-x # Delete branch
git branch -D feature-x # Force delete
git switch / git checkout
Switch branches:
git switch main # Switch to existing branch
git switch -c feature-x # Create and switch
git checkout main # Older syntax (still works)
git checkout -b feature-x # Older create and switch
Use switch (Git 2.23+) for clarity.
git merge
Merge branches:
git merge feature-x # Merge feature-x into current branch
git merge --no-ff feature # Force a merge commit
If conflicts occur:
- Git marks conflicting files
- Edit files to resolve (look for
<<<<<<<,=======,>>>>>>>) git addresolved filesgit committo complete
Undoing changes
git restore
Discard changes (Git 2.23+):
git restore file.js # Discard unstaged changes
git restore --staged file # Unstage (keep changes)
git restore . # Discard all unstaged changes
Older syntax: git checkout -- file.js
git reset
Move HEAD and optionally change staging/working directory:
git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset HEAD~1 # Undo commit, keep changes unstaged
git reset --hard HEAD~1 # Undo commit, discard changes
Be careful with --hard on shared branches.
git revert
Create a new commit that undoes a previous one:
git revert abc123 # Revert specific commit
git revert HEAD # Revert last commit
Safe for shared branches since it adds history instead of rewriting.
Useful combinations
Start a feature
git checkout main
git pull
git switch -c feature/new-thing
Finish a feature
git add .
git commit -m "Add new thing"
git push -u origin feature/new-thing
# Create PR on GitHub/GitLab
Update feature branch with main
git checkout feature
git merge main # Or: git rebase main
# Resolve any conflicts
git push
Quick fix on main
git stash # Save current work
git checkout main
git pull
# Make fix
git commit -am "Fix bug"
git push
git checkout feature
git stash pop # Restore work
Command cheatsheet
| Command | Purpose |
|---|---|
git status |
See current state |
git log --oneline |
View history |
git diff |
See changes |
git add . |
Stage all |
git commit -m "msg" |
Commit |
git push |
Send to remote |
git pull |
Get from remote |
git switch -c name |
New branch |
git merge branch |
Merge |
git restore file |
Discard changes |
git reset --soft HEAD~1 |
Undo commit |
Tips
- Commit often - Small, focused commits are easier to review and revert
- Pull before push - Avoid conflicts by staying updated
- Use branches - Keep main clean, develop on feature branches
- Write good messages - Future you will thank present you
- Use aliases - Set up shortcuts for common commands
Example aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph"
Summary
These commands cover most daily Git work:
- Check status with
git statusandgit log - Save changes with
git addandgit commit - Sync with
git pullandgit push - Branch with
git switchandgit merge - Undo with
git restoreandgit reset
The Git documentation is the authoritative reference for full syntax and options.
