# Git Commands: 42 Everyday Commands with Examples

Source: https://srvscripts.com/guides/git-commands/
Updated: 2026-10-09
Publisher: srvScripts (https://srvscripts.com/)

**Short answer:** The Git commands you use every day are `git status`, `git add`, `git commit`, `git diff` and `git log` for local work, `git switch` and `git merge` for branches, and `git fetch`, `git pull` and `git push` to share. To undo safely, use `git restore` for files, `git reset` only on commits nobody else has, `git revert` for pushed commits, and `git push --force-with-lease` instead of `--force`. The tables give syntax, an example and key options for 42 commands.

Every example below was run in a scratch repository with Git 2.43.0 on 9 October 2026. A local bare repository stood in for the remote URLs, and real hashes replaced placeholders such as `a1b2c3d`. The current Git release is 2.56; all commands here work on any Git from 2.30 onwards.

## Setup and config

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git config | Read or set settings for the repository, user or system | git config [--global]   | git config --global user.name "Ann Admin" | --global, --list --show-origin, --unset |
| git help | Show the manual for a command or a guide | git help  | git help push | -g (guides), git push -h (short usage) |
| git --version | Print the installed Git version | git --version | git --version | --build-options |
| git check-ignore | Show which .gitignore rule matches a path | git check-ignore -v  | git check-ignore -v logs/app.log | -v, --no-index, --stdin |

Worth setting once: `git config --global pull.ff only`, so `git pull` never creates surprise merge commits.

## Starting a repository

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git init | Create a new repository | git init [] | git init -b main app | -b , --bare (server-side repository) |
| git clone | Copy a repository with its history | git clone  [] | git clone https://git.example.com/ops/app.git | --depth 1, --branch , --recurse-submodules |
| git submodule | Embed another repository at a fixed commit | git submodule add   | git submodule update --init --recursive | add, update --init, status |
| git worktree | Check out a second branch in another directory | git worktree add  [] | git worktree add ../app-hotfix -b hotfix | list, remove, prune |

## Everyday changes

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git status | Staged, unstaged and untracked files | git status | git status -sb | -s (short), -b (branch), --ignored |
| git add | Stage changes for the next commit | git add  | git add nginx.conf | -p (pick hunks), -A (everything), -u (tracked only) |
| git commit | Record the staged changes | git commit -m "" | git commit -m "Raise worker_connections to 4096" | -a, --amend, -v |
| git diff | Show changes not yet committed | git diff [--staged] [] | git diff --staged | --staged, --stat, --word-diff |
| git rm | Delete a file and stage the deletion | git rm  | git rm --cached .env | --cached (stop tracking, keep the file), -r |
| git mv | Move or rename and stage it | git mv   | git mv nginx.conf conf/nginx.conf | -n (dry run), -f |
| git stash | Shelve uncommitted work | git stash push -m "" | git stash push -m "wip tuning" | list, pop, -u (include untracked) |

`git rm --cached` stops tracking a file such as `.env`, but the secret stays in earlier commits. If it was pushed, rotate the secret.

## Branching and merging

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git branch | List, create, rename or delete branches | git branch [] | git branch -vv | -d/-D, -m (rename), --merged |
| git switch | Change to another branch | git switch  | git switch -c feature/login | -c (create), - (previous branch) |
| git checkout | Older command for branches and files; checking out a tag gives a detached HEAD | git checkout  | git checkout v1.2.0 | -b, --  |
| git merge | Join another branch into the current one | git merge  | git merge --no-ff feature/login | --no-ff, --ff-only, --abort |
| git rebase | Replay your commits on top of another base | git rebase  | git rebase main | -i, --continue/--abort, --onto |
| git cherry-pick | Copy one commit onto the current branch | git cherry-pick  | git cherry-pick -x a1b2c3d | -x (note the source), -n, --abort |
| git tag | Name a commit, usually a release | git tag -a  -m "" | git tag -a v1.2.0 -m "Release 1.2.0" | -a, -l "v1.*", -d |

## Remotes and sharing

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git remote | Manage remote names and URLs | git remote add   | git remote -v | add, set-url, remove |
| git fetch | Download remote commits without touching your files | git fetch [] | git fetch --prune origin | --prune, --all, --tags |
| git pull | Fetch, then integrate into the current branch | git pull [ ] | git pull --ff-only | --ff-only, --rebase |
| git push | Upload commits and tags | git push   | git push -u origin main | -u, --tags, --force-with-lease |
| git ls-remote | List a remote’s branches and tags without cloning | git ls-remote  | git ls-remote --tags origin | --heads, --tags |
| git archive | Export a tree as a tar or zip, without .git | git archive -o   | git archive --format=tar.gz --prefix=app/ -o ../app-v1.2.0.tar.gz v1.2.0 | --format, --prefix, -o |
| git apply | Apply a patch file to the working tree | git apply  | git apply --check ../fix.patch | --check, --stat, -R (reverse) |

## Inspecting history

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git log | Commit history | git log [] [-- ] | git log --oneline --graph --decorate -10 | -p, --since="2 weeks ago", --follow |
| git show | One commit with its diff, or a file at a revision | git show [:] | git show v1.2.0:nginx.conf | --stat, --name-only |
| git blame | Last commit that changed each line | git blame  | git blame -L 10,20 conf/nginx.conf | -L, -w, -C |
| git shortlog | Commit counts per author | git shortlog -sn  | git shortlog -sne --since="1 month ago" HEAD | -s, -n, -e |
| git grep | Search tracked files (or any revision) | git grep  [] | git grep -n "worker_connections" | -n, -i, -l |
| git describe | Name a commit relative to the newest tag | git describe [--tags] | git describe --tags --always | --tags, --always, --dirty |
| git bisect | Binary search for the commit that broke something | git bisect start   | git bisect start HEAD v1.2.0 | run , good/bad, reset |
| git ls-files | List tracked, modified or untracked files | git ls-files | git ls-files --others --exclude-standard | -o, -m, --exclude-standard |

Give `git shortlog` a revision such as `HEAD` in scripts and cron jobs: without one, and without a terminal, it reads a log from standard input and waits. Add `--follow` to `git log -- <file>` to keep the history from before a rename.

## Undoing and fixing

| Command | What it does | Syntax | Example | Useful options |
| --- | --- | --- | --- | --- |
| git restore | Discard file changes or unstage them | git restore [--staged]  | git restore --staged conf/nginx.conf | --staged, --source=, -p |
| git reset | Move the branch to another commit; optionally unstage or discard | git reset [--soft | --mixed | --hard]  | git reset --soft HEAD~1 | --soft, --mixed (default), --hard |
| git revert | New commit that undoes an earlier one | git revert  | git revert --no-edit HEAD | --no-edit, -n, -m 1 (merge commits) |
| git clean | Delete untracked files | git clean -n | git clean -nd, then git clean -fd | -n (dry run), -d, -x (ignored files too) |
| git reflog | Where HEAD has pointed, including after resets | git reflog [] | git reflog -5 | show , -n |

## Safe undo: reset vs revert vs restore

| Situation | Command | Safe after push? |
| --- | --- | --- |
| Throw away edits to one file | git restore conf/nginx.conf | Yes (nothing committed) |
| Unstage a file, keep the edits | git restore --staged conf/nginx.conf | Yes |
| Bring back a file from a release | git restore --source=v1.2.0 nginx.conf | Yes |
| Undo the last commit, keep its changes staged | git reset --soft HEAD~1 | No: rewrites history |
| Fix the last commit’s message or content | git commit --amend | No: rewrites history |
| Undo a commit that is already pushed | git revert  | Yes: adds a new commit |
| Recover after reset --hard | git reflog, then git reset --hard HEAD@{1} | Local rescue |

**reset** moves the branch pointer: `--soft` keeps changes staged, `--mixed` unstaged, and `--hard` also deletes uncommitted work, which reflog cannot recover. **revert** adds an opposite commit, so it is the safe undo on a shared branch. **restore** works on files, not commits.

When you must overwrite a remote branch after a rebase or amend, use `git push --force-with-lease`. It refuses if the remote branch has moved since your last fetch. In the test, a teammate’s push made it fail with `(stale info)`, where `--force` would have deleted their commit. A background `git fetch`, for example from an IDE, defeats that check; adding `--force-if-includes` (Git 2.30+) still rejected the push. Then run `git pull --rebase` and push again.

## Git on servers

**For server admins:** Git works well for versioning `/etc` and deploying configuration.

| Task | Command |
| --- | --- |
| Deploy only fast-forward updates | git pull --ff-only (fails instead of merging if someone edited files on the server) |
| See what a deploy would change | git fetch origin, then git diff HEAD origin/main --stat |
| Ship a release without .git | git archive --format=tar.gz -o app.tar.gz v1.2.0 |
| Check for local edits before deploying | git status --porcelain (empty output means clean) |
| Repository owned by another user | git config --global --add safe.directory /srv/app after checking who owns it |

Keep secrets in `.gitignore` (confirm with `git check-ignore -v`), and never let the web server serve a `.git` directory: anyone could download the history and any committed secrets.

**Official documentation (checked 9 October 2026):** [Git reference documentation](https://git-scm.com/docs) · [git-reset](https://git-scm.com/docs/git-reset) · [git-restore](https://git-scm.com/docs/git-restore) · [git-revert](https://git-scm.com/docs/git-revert) · [git-push (--force-with-lease)](https://git-scm.com/docs/git-push) · [Pro Git book](https://git-scm.com/book/en/v2)

## Git Commands at a glance

In short: A reference of 42 everyday Git commands grouped by task, each with its syntax, a tested example and the most useful options, plus safe ways to undo mistakes.

Git Commands: what this guide covers.

**Related:** [Git Cheat Sheet: Branches, Undo, Remotes and Deploys](https://srvscripts.com/guides/git-cheat-sheet/) · [.gitignore Generator](https://srvscripts.com/tools/gitignore-generator/) · [Terminal Shortcuts Cheat Sheet: Bash, Zsh and tmux](https://srvscripts.com/guides/terminal-cheat-sheet/).
