Basic and commonly useful commands for daily work. Pair with your editor’s Git UI: use the CLI for history, partial staging, remotes, and recovery.
See what changed and what is staged.
git status
Unstaged changes (working tree vs index).
git diff
Staged changes (what the next commit will include).
git diff --staged
Stage specific files or every change.
git add path/to/file
git add .
Stage interactively, hunk by hunk—ideal when only part of a file should commit.
git add -p
Unstage a file (keep working tree edits).
git restore --staged path/to/file
Create a commit with a message.
git commit -m "Short, imperative description"
Amend the last commit (message and/or contents). Safe only before push, or if you accept rewriting shared history.
git commit --amend
git push requires a force push if the commit was already on the remote—coordinate with your team.Compact log with branch pointers and a small graph.
git log --oneline --graph --decorate -20
Show one commit (patch and metadata).
git show <commit>
Line-by-line last change (who, when).
git blame path/to/file
List local branches; -a includes remotes.
git branch
git branch -a
Switch branch (modern).
git switch branch-name
Create and switch to a new branch.
git switch -c new-branch-name
git checkout branch-name and git checkout -b new-branch; git switch is clearer for branch changes only.Attach a local repository to a remote (usually named origin). Use from the repo root after git init, or when the project folder never had a remote.
git remote add origin https://github.com/USER/REPO.git
# SSH example:
git remote add origin [email protected]:USER/REPO.git
origin already exists, use git remote set-url below instead of adding again. To use another name: git remote add upstream ….Change where origin points (repo renamed, HTTPS ↔ SSH, or wrong URL).
git remote set-url origin https://github.com/USER/REPO.git
git remote set-url origin [email protected]:USER/REPO.git
List remotes and their fetch/push URLs.
git remote -v
Download remote updates without merging into your branch.
git fetch
Fetch and integrate (merge or rebase per your config).
git pull
Publish commits. First time on a branch, set upstream.
git push -u origin branch-name
git push
Discard uncommitted changes in a file (restore last committed version).
git restore path/to/file
Move branch tip back one commit; keep changes staged / in working tree / discard—pick the mode you need.
git reset --soft HEAD~1 # undo commit, keep staged
git reset --mixed HEAD~1 # undo commit, keep files unstaged (default)
git reset --hard HEAD~1 # undo commit and discard changes
git reset --hard permanently drops uncommitted work on tracked files. Use only when you are sure.Overwrite remote branch (rare; breaks collaborators if misused).
git push --force-with-lease
main without team agreement. Prefer --force-with-lease over --force when you must.Park uncommitted work temporarily (e.g. switch branches).
git stash push -m "WIP: describe context"
git stash list
git stash pop
Apply latest stash but keep it in the list.
git stash apply
Merge another branch into the current one (creates a merge commit unless fast-forward).
git merge other-branch
Replay your commits on top of another branch (linear history). Typical before opening a PR.
git fetch origin
git rebase origin/main
Mark a release point; lightweight tag.
git tag v1.0.0
git push origin v1.0.0
List tags.
git tag -l "v*"
Copy one commit onto the current branch (e.g. hotfix from another branch).
git cherry-pick <commit>
git add -p, rich log, stash, fetch vs pull, blame, and odd states.