Getting Started
Basic Commands
Master the essential Git workflow: init, add, commit, status, and log.
The Basic Git Workflow
- Modify files in your working directory
- Stage changes with
git add - Commit staged changes with
git commit
The Three States
- Modified: Changes made but not staged
- Staged: Changes marked for next commit
- Committed: Changes saved to the repository
Useful Commands
git init— create a new repositorygit status— see what's changedgit add— stage changesgit commit— save a snapshotgit log— view commit historygit diff— see changes in detail
Example
bash
# Create a new repository
mkdir my-project
cd my-project
git init
# Check repository status
git status
# Create a file
echo "Hello, Git!" > README.md
# Stage the file
git add README.md
# Stage all changes
git add .
# Stage specific changes interactively
git add -p
# Commit staged changes
git commit -m "Initial commit: Add README"
# Shortcut: stage and commit tracked files
git commit -am "Update README"
# View commit history
git log
git log --oneline # compact view
git log --oneline --graph --all # visual branch graph
# See what changed
git diff # unstaged changes
git diff --staged # staged changes (not yet committed)
git diff HEAD~1..HEAD # between last two commits
# Undo changes
git restore README.md # discard unstaged changes
git restore --staged README.md # unstage a file
git reset HEAD~1 # undo last commit (keep changes staged)
git reset --hard HEAD~1 # undo last commit (discard changes!)Try it yourself — BASH