DEV Community

Server Scripts
Server Scripts

Posted on Originally published at srvscripts.com

Git Commands: 42 Everyday Commands with Examples

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. (Reviewed October 2026.)

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.

Table of Contents

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] <key> <value> 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 <command> 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 <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

Command What it does Syntax Example Useful options
git init Create a new repository git init [<dir>] git init -b main app -b <branch>, --bare (server-side repository)
git clone Copy a repository with its history git clone <url> [<dir>] git clone https://git.example.com/ops/app.git --depth 1, --branch <name>, --recurse-submodules
git submodule Embed another repository at a fixed commit git submodule add <url> <path> git submodule update --init --recursive add, update --init, status
git worktree Check out a second branch in another directory git worktree add <path> [<branch>] 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 <path> git add nginx.conf -p (pick hunks), -A (everything), -u (tracked only)
git commit Record the staged changes git commit -m "<message>" git commit -m "Raise worker_connections to 4096" -a, --amend, -v
git diff Show changes not yet committed git diff [--staged] [<path>] git diff --staged --staged, --stat, --word-diff
git rm Delete a file and stage the deletion git rm <path> git rm --cached .env --cached (stop tracking, keep the file), -r
git mv Move or rename and stage it git mv <old> <new> git mv nginx.conf conf/nginx.conf -n (dry run), -f
git stash Shelve uncommitted work git 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

Command What it does Syntax Example Useful options
git branch List, create, rename or delete branches git branch [<name>] git branch -vv -d/-D, -m (rename), --merged
git switch Change to another branch git switch <branch> 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 <ref> git checkout v1.2.0 -b, -- <path>
git merge Join another branch into the current one git merge <branch> git merge --no-ff feature/login --no-ff, --ff-only, --abort
git rebase Replay your commits on top of another base git rebase <base> git rebase main -i, --continue/--abort, --onto
git cherry-pick Copy one commit onto the current branch git cherry-pick <commit> git cherry-pick -x a1b2c3d -x (note the source), -n, --abort
git tag Name a commit, usually a release git tag -a <name> -m "<msg>" 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 <name> <url> git remote -v add, set-url, remove
git fetch Download remote commits without touching your files git fetch [<remote>] git fetch --prune origin --prune, --all, --tags
git pull Fetch, then integrate into the current branch git pull [<remote> <branch>] git pull --ff-only --ff-only, --rebase
git push Upload commits and tags git push <remote> <branch> 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 <remote> git ls-remote --tags origin --heads, --tags
git archive Export a tree as a tar or zip, without .git git 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 apply Apply a patch file to the working tree git apply <patch> 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 [<range>] [-- <path>] 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 <rev>[:<path>] git show v1.2.0:nginx.conf --stat, --name-only
git blame Last commit that changed each line git blame <file> git blame -L 10,20 conf/nginx.conf -L, -w, -C
git shortlog Commit counts per author git shortlog -sn <rev> git shortlog -sne --since="1 month ago" HEAD -s, -n, -e
git grep Search tracked files (or any revision) git grep <pattern> [<rev>] 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 <bad> <good> git bisect start HEAD v1.2.0 run <script>, 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] <path> git restore --staged conf/nginx.conf --staged, --source=<rev>, -p
git reset Move the branch to another commit; optionally unstage or discard `git reset [--soft --mixed --hard] `
git revert New commit that undoes an earlier one git revert <commit> 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 [<ref>] git reflog -5 show <branch>, -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 <commit> 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 · 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.


Originally published on srvScripts — tested how-to guides and free tools for people who run servers.

Top comments (1)

Collapse
 
maame-codes profile image
Maame Afua A. P. Fordjour •

This is really useful, thank you! I like that every example was actually tested, because so many cheat sheets just list commands without showing what really happens.