Git CLI reference

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.

Status and diffs

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

Staging

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

Commits

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
Warning: Amending after git push requires a force push if the commit was already on the remote—coordinate with your team.

History and inspection

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

Branches

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
Older tutorials use git checkout branch-name and git checkout -b new-branch; git switch is clearer for branch changes only.

Remotes

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
If 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

Undo and repair

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
Warning: 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
Warning: Force pushing rewrites remote history. Never force-push shared main without team agreement. Prefer --force-with-lease over --force when you must.

Stash

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 and rebase

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
Warning: Rebasing commits that were already pushed (and shared) rewrites history; avoid unless you know the workflow (e.g. “rebase my feature branch only”).

Tags

Mark a release point; lightweight tag.

git tag v1.0.0
git push origin v1.0.0

List tags.

git tag -l "v*"

Cherry-pick

Copy one commit onto the current branch (e.g. hotfix from another branch).

git cherry-pick <commit>

CLI vs editor (quick mental model)