# git commands
git status - show working tree state
Shows the state of the working directory and the staging area: which files are modified, which are staged for the next commit, and which are untracked. The go-to command before any git add or git commit.
In the short format (-s), the first column is the staging area, the second is the working tree. A = added, M = modified, D = deleted, ?? = untracked.
git pull - fetch and merge from remote
Fetches changes from the remote and merges them into the current branch. It is shorthand for git fetch followed by git merge.
Use --rebase to keep a linear history instead of creating a merge commit. Good practice for feature branches that diverged from main.
git push - upload commits to remote
Pushes local commits to the remote repository. The first push of a new branch requires setting the upstream tracking reference with -u.
Prefer --force-with-lease over --force. It aborts if someone else has pushed in the meantime, preventing accidental overwrites.
git commit - save staged changes to history
Records staged changes as a new commit in the repository history. Always stage files first with git add unless using -a for already-tracked files.
--amend rewrites the last commit — only use it before pushing. If already pushed, amending requires a force push.
git checkout - switch branch or restore files
Switches between branches or restores files from a commit. Git 2.23+ introduced the split commands git switch (for branches) and git restore (for files).
git switch and git restore do the same things but with clearer intent. New scripts should prefer them over the overloaded git checkout.
git revert - undo a commit non-destructively
Creates a new commit that undoes the changes from a previous commit. Non-destructive — it does not rewrite history, making it safe for shared branches.
Unlike git reset, revert preserves history and is safe to use on branches others are working on. Use git reset only for local, unpushed commits.
git merge - merge remote branch into current branch
To integrate changes from a remote branch (e.g. a colleague's feature branch), fetch the latest remote state and then merge the target branch.
Merge preserves branch history with a merge commit. Rebase produces a linear history but rewrites commits — avoid rebasing shared branches.
git stash - temporarily save uncommitted changes
Temporarily saves uncommitted changes so you can switch context without losing work. Stashes are stored in a stack — you can have multiple stashes and apply them selectively.
git stash pop = apply + delete. Use git stash apply if you want to keep the stash for other branches. Untracked files are not stashed unless you add -u.