Emergency server help: get in touch

Git Commands: 42 Everyday Commands with Examples

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.

Published 8 min read

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

CommandWhat it doesSyntaxExampleUseful options
git configRead or set settings for the repository, user or systemgit config [--global] <key> <value>git config --global user.name "Ann Admin"--global, --list --show-origin, --unset
git helpShow the manual for a command or a guidegit help <command>git help push-g (guides), git push -h (short usage)
git --versionPrint the installed Git versiongit --versiongit --version--build-options
git check-ignoreShow which .gitignore rule matches a pathgit check-ignore -v <path>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

CommandWhat it doesSyntaxExampleUseful options
git initCreate a new repositorygit init [<dir>]git init -b main app-b <branch>, --bare (server-side repository)
git cloneCopy a repository with its historygit clone <url> [<dir>]git clone https://git.example.com/ops/app.git--depth 1, --branch <name>, --recurse-submodules
git submoduleEmbed another repository at a fixed commitgit submodule add <url> <path>git submodule update --init --recursiveadd, update --init, status
git worktreeCheck out a second branch in another directorygit worktree add <path> [<branch>]git worktree add ../app-hotfix -b hotfixlist, remove, prune

Everyday changes

CommandWhat it doesSyntaxExampleUseful options
git statusStaged, unstaged and untracked filesgit statusgit status -sb-s (short), -b (branch), --ignored
git addStage changes for the next commitgit add <path>git add nginx.conf-p (pick hunks), -A (everything), -u (tracked only)
git commitRecord the staged changesgit commit -m "<message>"git commit -m "Raise worker_connections to 4096"-a, --amend, -v
git diffShow changes not yet committedgit diff [--staged] [<path>]git diff --staged--staged, --stat, --word-diff
git rmDelete a file and stage the deletiongit rm <path>git rm --cached .env--cached (stop tracking, keep the file), -r
git mvMove or rename and stage itgit mv <old> <new>git mv nginx.conf conf/nginx.conf-n (dry run), -f
git stashShelve uncommitted workgit stash push -m "<message>"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

CommandWhat it doesSyntaxExampleUseful options
git branchList, create, rename or delete branchesgit branch [<name>]git branch -vv-d/-D, -m (rename), --merged
git switchChange to another branchgit switch <branch>git switch -c feature/login-c (create), - (previous branch)
git checkoutOlder command for branches and files; checking out a tag gives a detached HEADgit checkout <ref>git checkout v1.2.0-b, -- <path>
git mergeJoin another branch into the current onegit merge <branch>git merge --no-ff feature/login--no-ff, --ff-only, --abort
git rebaseReplay your commits on top of another basegit rebase <base>git rebase main-i, --continue/--abort, --onto
git cherry-pickCopy one commit onto the current branchgit cherry-pick <commit>git cherry-pick -x a1b2c3d-x (note the source), -n, --abort
git tagName a commit, usually a releasegit tag -a <name> -m "<msg>"git tag -a v1.2.0 -m "Release 1.2.0"-a, -l "v1.*", -d

Remotes and sharing

CommandWhat it doesSyntaxExampleUseful options
git remoteManage remote names and URLsgit remote add <name> <url>git remote -vadd, set-url, remove
git fetchDownload remote commits without touching your filesgit fetch [<remote>]git fetch --prune origin--prune, --all, --tags
git pullFetch, then integrate into the current branchgit pull [<remote> <branch>]git pull --ff-only--ff-only, --rebase
git pushUpload commits and tagsgit push <remote> <branch>git push -u origin main-u, --tags, --force-with-lease
git ls-remoteList a remote’s branches and tags without cloninggit ls-remote <remote>git ls-remote --tags origin--heads, --tags
git archiveExport a tree as a tar or zip, without .gitgit archive -o <file> <ref>git archive --format=tar.gz --prefix=app/ -o ../app-v1.2.0.tar.gz v1.2.0--format, --prefix, -o
git applyApply a patch file to the working treegit apply <patch>git apply --check ../fix.patch--check, --stat, -R (reverse)

Inspecting history

CommandWhat it doesSyntaxExampleUseful options
git logCommit historygit log [<range>] [-- <path>]git log --oneline --graph --decorate -10-p, --since="2 weeks ago", --follow
git showOne commit with its diff, or a file at a revisiongit show <rev>[:<path>]git show v1.2.0:nginx.conf--stat, --name-only
git blameLast commit that changed each linegit blame <file>git blame -L 10,20 conf/nginx.conf-L, -w, -C
git shortlogCommit counts per authorgit shortlog -sn <rev>git shortlog -sne --since="1 month ago" HEAD-s, -n, -e
git grepSearch tracked files (or any revision)git grep <pattern> [<rev>]git grep -n "worker_connections"-n, -i, -l
git describeName a commit relative to the newest taggit describe [--tags]git describe --tags --always--tags, --always, --dirty
git bisectBinary search for the commit that broke somethinggit bisect start <bad> <good>git bisect start HEAD v1.2.0run <script>, good/bad, reset
git ls-filesList tracked, modified or untracked filesgit ls-filesgit 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

CommandWhat it doesSyntaxExampleUseful options
git restoreDiscard file changes or unstage themgit restore [--staged] <path>git restore --staged conf/nginx.conf--staged, --source=<rev>, -p
git resetMove the branch to another commit; optionally unstage or discardgit reset [--soft | --mixed | --hard] <commit>git reset --soft HEAD~1--soft, --mixed (default), --hard
git revertNew commit that undoes an earlier onegit revert <commit>git revert --no-edit HEAD--no-edit, -n, -m 1 (merge commits)
git cleanDelete untracked filesgit clean -ngit clean -nd, then git clean -fd-n (dry run), -d, -x (ignored files too)
git reflogWhere HEAD has pointed, including after resetsgit reflog [<ref>]git reflog -5show <branch>, -n

Safe undo: reset vs revert vs restore

SituationCommandSafe after push?
Throw away edits to one filegit restore conf/nginx.confYes (nothing committed)
Unstage a file, keep the editsgit restore --staged conf/nginx.confYes
Bring back a file from a releasegit restore --source=v1.2.0 nginx.confYes
Undo the last commit, keep its changes stagedgit reset --soft HEAD~1No: rewrites history
Fix the last commit’s message or contentgit commit --amendNo: rewrites history
Undo a commit that is already pushedgit revert <commit>Yes: adds a new commit
Recover after reset --hardgit 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.

TaskCommand
Deploy only fast-forward updatesgit pull --ff-only (fails instead of merging if someone edited files on the server)
See what a deploy would changegit fetch origin, then git diff HEAD origin/main --stat
Ship a release without .gitgit archive --format=tar.gz -o app.tar.gz v1.2.0
Check for local edits before deployinggit status --porcelain (empty output means clean)
Repository owned by another usergit 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 · git-reset · git-restore · git-revert · git-push (--force-with-lease) · Pro Git book

Git Commands at a glance

Git Commands – short answer
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
Git Commands: what this guide covers.

Related: Git Cheat Sheet: Branches, Undo, Remotes and Deploys · .gitignore Generator · Terminal Shortcuts Cheat Sheet: Bash, Zsh and tmux.

Reading a lot of our guides? Go ad-free with srvScripts Pro for $6 a month. It also pays for new guides. Cancel any time.

Share this guide

Free website test

Is your website set up right?

Check SSL, security headers, redirects, robots.txt, sitemap, llms.txt and security.txt in one test. It takes about 30 seconds.