Version: v1.22.0
Full guide: git-repository-guide.md
Quick-start guide: git-repository-guide-quick-start-guide.md
Cheat sheet: git-repository-guide-cheat-sheet.md
This is a practical quick reference for commonly used Git commands.
It is intentionally command-focused. Use the full guide when you need deeper explanations, examples, troubleshooting, or workflow context.
Recommended standalone filename:
git-command-quick-reference-v1.22.0.md
Recommended stable repository filename, if you prefer non-versioned companion filenames inside an actual Git repository:
git-command-quick-reference.md
Document title casing:
Git Command Quick Reference
Each command includes:
- What it does: short description.
- Common syntax: beginner-friendly example.
- Required parameters: values you must supply.
- Optional parameters: useful flags or arguments.
- Notes: practical warnings or clarifications.
Placeholders such as branch-name, file-name.md, old-tag, and new-tag are variables. Replace them with your actual names.
| Rank | Command | Why it matters |
|---|---|---|
| 1 | git status |
Shows what changed and what is staged. Use constantly. |
| 2 | git add |
Stages changes for the next commit. |
| 3 | git commit |
Saves staged changes into Git history. |
| 4 | git push |
Sends local commits or tags to a remote such as GitHub. |
| 5 | git pull |
Gets remote changes and integrates them into your current branch. |
| 6 | git diff |
Shows exactly what changed. |
| 7 | git log |
Shows commit history. |
| 8 | git branch |
Lists, creates, or renames branches. |
| 9 | git switch |
Switches branches or creates a new branch. |
| 10 | git tag |
Creates or lists version tags. |
| 11 | git restore |
Restores files or unstages staged changes. |
| 12 | git remote |
Manages remote repository connections. |
| 13 | git fetch |
Downloads remote history without integrating it. |
| 14 | git merge |
Combines another branch/history into the current branch. |
| 15 | git mv |
Renames or moves tracked files. |
| 16 | git rm |
Removes tracked files from Git. |
| 17 | git clone |
Copies an existing repository. |
| 18 | git init |
Creates a new local Git repository. |
| 19 | git show |
Shows details for a commit, tag, or object. |
| 20 | git archive |
Exports a repository snapshot, often as a ZIP. |
Commit prefixes are not Git commands. They are a convention used inside commit message text.
Pattern:
type: short imperative summary
Common examples:
git commit -m "docs: document commit message prefixes"
git commit -m "fix: correct canonical domain"
git commit -m "feat: add web applications section"
git commit -m "chore: prepare v2.1.0 release"| Prefix | Meaning | Use when |
|---|---|---|
feat |
Feature | User-visible content, capability, page, endpoint, or section is added or expanded |
fix |
Bug fix | Something wrong, broken, stale, misleading, or misplaced is corrected |
docs |
Documentation | Documentation-only files change |
chore |
Maintenance | Routine maintenance, release prep, metadata refresh, or housekeeping |
refactor |
Restructure | Internal structure changes without intended visible behavior change |
style |
Formatting | Whitespace, indentation, line endings, or formatting-only changes |
test |
Tests | Tests, test data, or validation scripts change |
build |
Build/dependencies | Build scripts, Dockerfiles, package files, or dependencies change |
ci |
CI/CD | GitHub Actions, deployment automation, or validation workflows change |
perf |
Performance | Speed, size, load time, or efficiency improves |
revert |
Revert | A previous commit is intentionally undone |
Optional scope pattern:
type(scope): summary
Examples:
git commit -m "docs(readme): add repository navigation"
git commit -m "fix(metadata): correct canonical domain"
git commit -m "feat(portfolio): add web applications section"Breaking-change pattern:
git commit -m "feat!: replace guide structure" -m "BREAKING CHANGE: The guide section order and anchor names changed."These are quick lookup commands and shortcuts for GitHub files, tags, releases, and commit messages.
| Goal | Shortcut or command |
|---|---|
| Search current GitHub file | Ctrl+F / Cmd+F |
| Open GitHub web editor | . |
| Search a path on GitHub | keyword path:git-repository-guide.md |
| List tags | git tag --list |
| List tags newest-first | git tag --sort=-v:refname |
| Show tag refs and hashes | git show-ref --tags |
| Show tags in history | git log --oneline --decorate --graph --all |
| Show tagged/decorated commits | git log --tags --simplify-by-decoration --oneline |
| Inspect one tag | git show v1.0.0 |
| Nearest reachable tag | git describe --tags |
| List GitHub Releases | gh release list |
| View one GitHub Release | gh release view v1.0.0 |
| Compact commit history | git log --oneline |
| All-ref history graph | git log --all --graph --decorate --oneline |
| Oldest-first history | git log --all --reverse --oneline |
| Complete messages | git log --all --reverse --format="%H%n%B%n" |
| Subjects only | git log --format="%s" |
| Search commit messages | git log --all --grep="keyword" --oneline |
| One-commit summary | git show --stat --summary <commit-hash> |
| One-commit statuses | git show --name-status --find-renames --format= <commit-hash> |
| Complete snapshot paths | git ls-tree -r --name-only <commit-hash> |
| Exit Git pager | q |
Plain git push usually works after upstream tracking is set. For careful versioned updates, git push origin main is often clearer.
Related repositories are usually connected through documentation and GitHub metadata, not by nesting repositories.
| Goal | Recommended method |
|---|---|
| Hub links to examples | README table with full GitHub URLs |
| Example links to hub | README link back to hub |
| Previous/next example navigation | Full GitHub cross-repo links |
| Same-repo file links | Relative links such as CHANGELOG.md |
| Separate-repo links | Absolute GitHub URLs |
| Group repos | Shared GitHub topics |
| Version snapshots | Repo-specific tags |
| Polished release pages | Repo-specific GitHub Releases |
| Technical embedding | git submodule, only when truly needed |
| Copying another repo into a folder | git subtree, only when truly needed |
Submodules and subtree are advanced tools. For beginner-friendly educational examples, prefer README links, topics, descriptions, tags, and releases.
| Goal | Command / action |
|---|---|
| Rename tracked file/folder | git mv old-path new-path |
| Stage manual rename | git add -A |
| Verify staged rename | git diff --cached --summary |
| Show rename status | git diff --cached --name-status --find-renames |
| Rename GitHub repo | Repository Settings > General > Repository name |
| Check remotes | git remote -v |
Update origin |
git remote set-url origin https://github.com/USERNAME/new-repo-name.git |
| Test remote | git fetch origin |
| Search old names | git grep "old-name" |
| PowerShell search | Get-ChildItem -Recurse -File | Select-String -Pattern "old-name" |
Local parent folder renames usually do not require a Git commit. Tracked file/folder renames inside the repo do require a commit.
| Goal | Command |
|---|---|
| Create annotated tag | git tag -a vX.Y.Z -m "Version X.Y.Z" |
| Annotated tag with body | git tag -a vX.Y.Z -m "Version X.Y.Z" -m "Details." |
| Signed tag | git tag -s vX.Y.Z -m "Version X.Y.Z" |
| Push one tag | git push origin vX.Y.Z |
| Verify remote tag | git ls-remote --tags origin vX.Y.Z |
| Goal | Command |
|---|---|
| Show ignored files | git status --ignored |
| Explain ignore rule | git check-ignore -v path/to/file |
| Stop tracking ignored file | git rm --cached path/to/file |
| Stop tracking ignored folder | git rm -r --cached path/to/folder |
| Force-add ignored file | git add -f path/to/file |
| Goal | Command |
|---|---|
| Compact status | git status --short |
| Compact status with renames | git status --short --renames |
| Unstaged summary | git diff --stat |
| Stage current folder and below | git add . |
| Stage all repo changes | git add -A |
| Staged summary | git diff --cached --stat |
| Staged names/status with rename detection | git diff --cached --name-status --find-renames |
| Staged rename summary | git diff --cached --summary |
| Show repo root | git rev-parse --show-toplevel |
Check .gitattributes in PowerShell |
Test-Path .\.gitattributes |
| Create folder in PowerShell | New-Item -ItemType Directory -Force ".\docs\reference" |
Use git add -A when you intentionally want to stage all changes across the repository. Use git add . when you intentionally want the current folder and below.
Preferred wording:
committed locally and pushed to origin
| Goal | Command |
|---|---|
| Confirm working tree | git rev-parse --is-inside-work-tree |
| Show repository root | git rev-parse --show-toplevel |
| Show metadata directory | git rev-parse --git-dir |
| Show branch/upstream | git branch -vv |
| Show exact origin URL | git remote get-url origin |
| Show branch and tag refs | git show-ref --heads --tags |
| Verify object database | git fsck --full |
| Refresh origin refs | git fetch origin --prune |
| Compare branch tips | git rev-parse main and git rev-parse origin/main |
| Compact alignment status | git status -sb |
Do not run git init as an automatic reaction to missing metadata when existing history must be preserved.
| Goal | Command or convention |
|---|---|
| Primary branch | main |
| Primary remote | origin |
| First push with tracking | git push -u origin main |
| Later explicit push | git push origin main |
| Pre-release tag | git tag -a v0.1.0 -m "Pre-release 0.1.0" |
| Stable tag | git tag -a v1.0.0 -m "Version 1.0.0" |
| Push one tag | git push origin vX.Y.Z |
| Show remote details | git remote show origin |
| Rename remote | git remote rename origin github |
| Remove remote | git remote remove origin |
| Renormalize line endings | git add --renormalize . |
| Search tracked files | git grep "search text" |
| Check branch alignment | git log --oneline origin/main..main and git log --oneline main..origin/main |
| Verify remote tag | git ls-remote --tags origin vX.Y.Z |
Use these defaults unless your project has a specific reason to differ.
git commit -m "Release version X.Y.Z"
git tag -a vX.Y.Z -m "Version X.Y.Z"
git show --stat vX.Y.Z
git push origin main
git push origin vX.Y.ZAn unqualified tag command targets the current HEAD. Create and verify the tag before making the next release commit, or name the intended older commit explicitly.
git fetch --all --prune
git log --all --graph --decorate --oneline
git log --all --reverse --format="%H%n%B%n"
git show --stat --summary <commit-hash>
git show --name-status --find-renames --format= <commit-hash>
git ls-tree -r --name-only <commit-hash>Test-Path .git
git rev-parse --is-inside-work-tree
git rev-parse --show-toplevel
git rev-parse --git-dir
git status
git branch -vv
git remote -v
git show-ref --heads --tags
git fsck --full
git fetch origin --prune
git rev-parse main
git rev-parse origin/main
git status -sbgit diff-tree --root --no-commit-id --name-status --find-renames -r HEADgit config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --show-origin --show-scope --listgit diff --cached --check
git diff --cached --stat
git diff --cachedordinary folder: git init -b main, then git status
initialized repository: git status may come first
git archive --format=zip --output aws-hello-world-microservice-v1.1.1.zip v1.1.1
git archive --format=zip --prefix=aws-hello-world-microservice-v1.1.1/ --output aws-hello-world-microservice-v1.1.1.zip v1.1.1Use one archive form. The second adds a top-level extraction folder.
git commit -m "Subject" -m @'
Formatted body.
- First item
- Second item
'@Cross-shell:
git commit -F commit-message.txt| Goal | Command |
|---|---|
| First push and set upstream | git push -u origin main |
| Check tracking | git branch -vv |
| Print current upstream | git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" |
| Set existing remote branch as upstream | git branch --set-upstream-to=origin/main main |
| Remove upstream | git branch --unset-upstream |
| Goal | Command |
|---|---|
| Validate | git check-ref-format "refs/tags/component/v1.0.0" |
| Create | git tag -a component/v1.0.0 -m "Release Component v1.0.0" |
| Push | git push origin component/v1.0.0 |
| List | git tag --list "component/*" --sort=-version:refname |
| Delete local | git tag -d component/v1.0.0 |
| Delete remote | git push origin --delete component/v1.0.0 |
.gitignore shared project rule
.git/info/exclude private one-clone rule
git tag --list v1.0.0
git ls-remote --tags origin v1.0.0
git log --oneline --decorate -10Stages file changes for the next commit.
git add file-name.md
git add .
git add -A| Parameter | Required? | Meaning |
|---|---|---|
Pathspec, such as file-name.md, ., or folder name |
Usually | Tells Git what changes to stage. |
| Option | Meaning |
|---|---|
. |
Stages changes under the current directory. |
-A or --all |
Stages all changes across the whole repository, including additions, modifications, deletions, and renames. |
-p or --patch |
Interactively choose parts of files to stage. |
--renormalize |
Reapplies clean filters and line-ending normalization to tracked files according to .gitattributes. |
Use git status before and after git add.
Use git add . when you intentionally want to stage changes under the current folder. Use git add -A when you intentionally want to stage all changes across the repository.
For rename/delete workflows, prefer:
git add -AAfter changing .gitattributes, renormalize intentionally:
git add --renormalize .Creates an archive file, such as a ZIP, from a commit, branch, or tag.
git archive --format=zip --output project-v1.0.0.zip v1.0.0
git archive --format=zip --prefix=project-v1.0.0/ --output project-v1.0.0.zip v1.0.0| Parameter | Required? | Meaning |
|---|---|---|
Tree-ish, such as v1.0.0, main, or HEAD |
Yes | The repository snapshot to export. |
| Option | Meaning |
|---|---|
--format=zip |
Creates a ZIP archive. |
--output file.zip |
Writes the archive to the specified filename. |
--prefix folder-name/ |
Places archived files under a top-level folder inside the archive. |
Use this when you want a local ZIP of a tagged version. The archive contains the tracked snapshot, not untracked files, later working-tree changes, or the .git directory. Inspect the completed ZIP before publishing it.
Lists, creates, deletes, renames, or configures branches.
git branch
git branch -a
git branch -vv
git branch -M main
git branch -d branch-name
git branch --set-upstream-to=origin/main main
git branch --unset-upstream| Option | Meaning |
|---|---|
-a |
List local and remote-tracking branches. |
-vv |
Show upstream/tracking information and latest subjects. |
-M new-name |
Rename the current branch, forcing if needed. |
-d branch-name |
Delete a fully merged local branch. |
-D branch-name |
Force-delete a local branch. |
--set-upstream-to=<remote>/<branch> |
Set the upstream for the current or named local branch. |
--unset-upstream |
Remove the current branch's upstream relationship. |
Removing an upstream does not delete either branch. Use git switch for branch switching.
Shows whether a path is ignored and which ignore rule matched.
git check-ignore -v path/to/file| Parameter | Required? | Meaning |
|---|---|---|
path/to/file |
Yes | File or folder path to test. |
| Option | Meaning |
|---|---|
-v |
Verbose output showing the matching ignore file, line, and pattern. |
Use this when .gitignore behavior is confusing.
Checks whether a proposed branch, tag, or other Git reference name is valid.
git check-ref-format "refs/tags/component/v1.0.0"Supply the complete reference when validating a tag name. A valid name normally produces no output and returns exit status 0.
Older multi-purpose command for switching branches or restoring files.
git checkout branch-name
git checkout -b new-branch-name| Parameter | Required? | Meaning |
|---|---|---|
| Branch, commit, or path | Usually | What to check out or restore. |
| Option | Meaning |
|---|---|
-b branch-name |
Creates and switches to a new branch. |
This guide generally prefers newer, clearer commands:
git switch branch-name
git restore file-name.mdCopies an existing repository to your local computer.
git clone https://github.com/OWNER/REPO.git| Parameter | Required? | Meaning |
|---|---|---|
| Repository URL | Yes | The remote repository to clone. |
| Option or parameter | Meaning |
|---|---|
| Folder name after URL | Clones into that local folder name. |
--branch branch-or-tag |
Checks out the specified branch or tag after cloning. |
--depth 1 |
Creates a shallow clone with limited history. |
Use git clone when the repository already exists somewhere else.
Use git init when you are starting a new local repository from an existing folder.
Creates a commit from staged changes.
git commit -m "Subject"
git commit -m "Subject" -m "Body paragraph."
git commit -F commit-message.txt
git commit --amendPowerShell multiline body:
git commit -m "Subject" -m @'
Body paragraph.
- First item
- Second item
'@| Option | Meaning |
|---|---|
-m <message> |
Supply a message paragraph. Repeated -m values become separate paragraphs. |
-F <file> |
Read the complete message from a file; -F - reads standard input. |
--amend |
Replace the latest commit with a new commit containing the current staged snapshot and/or corrected message. |
--no-edit |
Reuse the existing message while amending. |
-v |
Show the diff in the editor template. |
A malformed quoted message can leave extra words for Git to interpret as pathspecs. Check git status, then use a here-string, editor, or message file.
Gets, sets, lists, or removes Git configuration values.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --show-origin --show-scope --list
git config --local user.email "you@company.example"
git config --global --unset core.pager| Parameter | Required? | Meaning |
|---|---|---|
| Config key | Required when setting, getting, or removing one value | Example: user.name. |
| Config value | Required when setting | Example: "Your Name". |
| Option | Meaning |
|---|---|
--system |
Uses configuration shared by all users on the computer. |
--global |
Uses configuration for the current operating-system user. |
--local |
Uses configuration for the current repository. |
--worktree |
Uses configuration for one linked worktree when enabled. |
--list |
Lists configuration values. |
--get |
Gets one value. |
--show-origin |
Shows which configuration file supplied each value. |
--show-scope |
Shows the scope of each value. |
--unset |
Removes a setting from the selected scope. |
Set user.name and user.email before committing. These values identify commits; they do not authenticate you to GitHub.
More specific applicable scopes normally override broader scopes for the same key.
Inspect line-ending behavior before changing it:
git config --show-origin --get core.autocrlfKeep .gitattributes as the shared committed repository policy. Do not treat core.autocrlf as a substitute.
Shows a name for the current commit based on the nearest reachable tag.
git describe --tags| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Describes the current commit by default. |
| Option | Meaning |
|---|---|
--tags |
Uses any tag, not only annotated tags. |
Use this when you want to know which tagged version the current commit is closest to.
Shows refs and their hashes.
git show-ref --tags| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Shows refs. |
| Option | Meaning |
|---|---|
--tags |
Limits output to tag refs. |
Use this when you need to verify tag refs and hashes exactly.
Uses GitHub CLI to inspect GitHub Releases.
gh release list
gh release view v1.0.0| Parameter | Required? | Meaning |
|---|---|---|
| Tag name | Required for gh release view |
Identifies the release to view. |
| Option | Meaning |
|---|---|
list |
Lists releases. |
view |
Views one release. |
Git tags and GitHub Releases are related but different. Use git tag commands for Git tags. Use gh release commands or GitHub.com for GitHub Releases.
Shows differences between files, commits, branches, or tags.
git diff
git diff --cached
git diff old-tag..new-tag
git diff --name-status v1.1.0..v1.1.1| Parameter | Required? | Meaning |
|---|---|---|
| None | No | With no arguments, shows unstaged changes. |
| Revision range | Optional | Compares two commits, branches, or tags. |
| Option | Meaning |
|---|---|
--cached or --staged |
Shows staged changes. |
--check |
Warns about introduced whitespace errors and unresolved conflict-marker patterns. |
--stat |
Shows summary statistics. |
--summary |
Summarizes creates, deletes, mode changes, and detected renames. |
--name-only |
Shows only changed filenames. |
--name-status |
Shows changed filenames plus status codes. |
--find-renames |
Enables rename detection. |
--diff-filter=A |
Shows only added files. Other filters include M, D, R, and C. |
Very useful before committing:
git diff
git diff --cached --check
git diff --cached --stat
git diff --cachedgit diff shows unstaged changes. git diff --cached shows the exact staged patch.
Very useful between version tags:
git diff --name-status old-tag..new-tagCompares the tree associated with a commit and reports the paths or changes introduced by that commit.
git diff-tree --root --no-commit-id --name-status --find-renames -r HEAD| Option | Meaning |
|---|---|
--root |
Compares the initial commit with an empty tree |
--no-commit-id |
Suppresses the commit ID header |
--name-status |
Shows status letters and paths |
--find-renames |
Enables rename detection |
-r |
Recurses into subtrees |
Checks connectivity and validity of objects and refs in the local Git object database.
git fsck --fullNo corruption error is the desired outcome. Dangling-object notices are not automatically corruption. This command does not compare local branch tips with a remote.
Downloads remote history without integrating it into your current branch.
git fetch
git fetch origin
git fetch --all
git fetch --prune| Parameter | Required? | Meaning |
|---|---|---|
| Remote name | Optional | Example: origin. If omitted, Git uses the default remote for the current branch. |
| Option | Meaning |
|---|---|
--all |
Fetches from all remotes. |
--prune |
Removes stale remote-tracking branches that no longer exist on the remote. |
--tags |
Fetches tags explicitly. |
Use git fetch when you want to inspect remote changes before merging or rebasing.
Searches tracked files in the repository.
git grep "old-name"
git grep "old-repo-name"| Parameter | Required? | Meaning |
|---|---|---|
| Pattern | Yes | Text to search for. |
Use this after a repository, folder, or project rename to find stale references in tracked files.
Creates a new local Git repository.
git init -b main| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Running git init initializes the current folder as a repository. |
| Option | Meaning |
|---|---|
-b main |
Creates the initial branch with the specified name. |
--initial-branch=main |
Long-form equivalent of -b main. |
Use this inside the folder you want Git to track.
Shows commit history.
git log
git log --oneline
git log --oneline --decorate --graph --all
git log --oneline origin/main..main
git log --oneline main..origin/main
git log --all --grep="Ve0sion" --oneline
git log --format=%B
git log --format=%s
git log --tags --simplify-by-decoration --oneline
git log --follow -- file-name.md| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Shows history for the current branch. |
| Option | Meaning |
|---|---|
--oneline |
Shows each commit on one line. |
--decorate |
Shows branch and tag names. |
--graph |
Shows a text graph of branches/merges. |
--all |
Shows all refs, not only the current branch. |
--grep="pattern" |
Filters commit messages matching a pattern. |
--regexp-ignore-case |
Makes grep matching case-insensitive. |
--format="..." |
Customizes log output. |
--follow -- file |
Follows file history across renames. |
-p |
Shows patches/diffs for commits. |
For a readable history graph:
git log --oneline --decorate --graph --allLists files stored in a tree object, such as a commit or tag snapshot.
git ls-tree -r --name-only HEAD
git ls-tree -r --name-only v1.1.1| Parameter | Required? | Meaning |
|---|---|---|
Tree-ish, such as HEAD, main, or v1.1.1 |
Yes | Snapshot to inspect. |
| Option | Meaning |
|---|---|
-r |
Recursively lists files in subfolders. |
--name-only |
Shows only file paths. |
Use this to answer:
What files are contained in this commit or tag?
Lists files in the index and can show end-of-line information.
git ls-files --eol| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Lists tracked/indexed files. |
| Option | Meaning |
|---|---|
--eol |
Shows index, working-tree, and attribute end-of-line information. |
| Pathspec | Limits output to matching files. |
Use this to inspect LF/CRLF behavior after adding .gitattributes.
Lists references advertised by a remote repository without updating local refs.
git ls-remote origin
git ls-remote --heads origin
git ls-remote --tags origin
git ls-remote --tags origin v1.0.0
git ls-remote --tags origin component/v1.0.0| Option | Meaning |
|---|---|
--heads |
Show branch references. |
--tags |
Show tag references. |
| Pattern | Limit output to matching refs. |
Use targeted --tags queries to verify whether a remote tag exists. Annotated tags may produce a tag-object line and a peeled ^{} target line.
Lists commit objects reachable from a commit, branch, or tag.
git rev-list -n 1 v1.0.0| Parameter | Required? | Meaning |
|---|---|---|
| Revision, branch, or tag | Yes | The starting point to inspect. |
| Option | Meaning |
|---|---|
-n 1 |
Limits output to one commit. |
--max-count=1 |
Long-form style equivalent to limiting count. |
In PowerShell, this safely stores the commit that a tag points to:
$commit = git rev-list -n 1 v1.0.0That is useful when deleting and recreating an older pushed tag without moving it to HEAD.
Combines another branch or commit history into the current branch.
git switch main
git merge branch-name| Parameter | Required? | Meaning |
|---|---|---|
| Branch or commit | Usually | The branch or commit to merge into the current branch. |
| Option | Meaning |
|---|---|
--abort |
Aborts an in-progress merge. |
--continue |
Continues after conflicts are resolved. |
--no-ff |
Creates a merge commit even if fast-forward is possible. |
--ff-only |
Only merges if Git can fast-forward. |
Always know which branch you are currently on before merging:
git statusRenames or moves a tracked file, directory, or symbolic link.
git mv old-name.md new-name.md| Parameter | Required? | Meaning |
|---|---|---|
| Source path | Yes | Existing tracked file or folder path. |
| Destination path | Yes | New file or folder path. |
| Option | Meaning |
|---|---|
-f |
Forces rename/move if the destination exists. Use carefully. |
-k |
Skips move/rename actions that would fail. |
After git mv, still commit the change:
git status
git commit -m "Rename file"For rename workflows with related reference updates, use:
git add -A
git diff --cached --summary
git diff --cached --name-status --find-renamesFetches remote changes and integrates them into the current branch.
git pull
git pull origin main| Parameter | Required? | Meaning |
|---|---|---|
| None | No | If upstream tracking is configured, plain git pull is enough. |
| Remote and branch | Optional | Example: origin main. |
| Option | Meaning |
|---|---|
--ff-only |
Only updates if the result can be a fast-forward. |
--rebase |
Replays local commits on top of the fetched branch. |
--no-rebase |
Uses merge behavior. |
Run git status first if you have local changes.
Updates branches, tags, or other references on a remote and transfers necessary objects.
git push
git push origin main
git push -u origin main
git push origin v1.0.0
git push origin component/v1.0.0
git push origin --delete v1.0.0| Option | Meaning |
|---|---|
-u, --set-upstream |
Explicitly set upstream tracking for a successfully pushed or up-to-date branch. |
--delete <ref> |
Delete the named remote branch or tag reference. |
--tags |
Push all local tags missing from the remote; review first. |
--follow-tags |
Also push reachable annotated tags missing from the remote. |
--force-with-lease |
Safer force update that checks the expected remote state. |
git push origin main explicitly pushes local main; it does not itself include the upstream-setting request. Tracking may already exist or may be established by configuration. Push branches and release tags separately when clarity matters.
Replays commits onto a new base commit.
git switch branch-name
git fetch origin
git rebase origin/main
git rebase -i <bad-commit-sha>~1| Parameter | Required? | Meaning |
|---|---|---|
| Upstream/base | Usually | The branch or commit to replay your commits onto. |
| Option | Meaning |
|---|---|
--continue |
Continues after conflicts are resolved. |
--abort |
Aborts the rebase. |
--skip |
Skips the current patch. |
-i |
Interactive rebase. Advanced. |
--root |
Rebase from the root commit. Useful only for special history edits. |
Rebase rewrites commit IDs. Avoid rebasing shared branches unless everyone agrees.
To fix an older commit message during interactive rebase, change pick to reword for that commit.
Manages named remote repository connections.
git remote -v
git remote add origin https://github.com/OWNER/REPO.git
git remote set-url origin https://github.com/OWNER/REPO.git| Parameter | Required? | Meaning |
|---|---|---|
| Remote name | Required for add/set-url/remove | Commonly origin. |
| Remote URL | Required for add/set-url | GitHub or other Git remote URL. |
| Option or subcommand | Meaning |
|---|---|
-v |
Shows remote names and URLs. |
add name url |
Adds a remote. |
set-url name url |
Changes a remote URL. |
remove name |
Removes a remote. |
show name |
Shows details for a remote. |
rename old-name new-name |
Renames a remote locally. |
origin is a remote name, not a branch.
Use git remote set-url after renaming a GitHub repository.
Shows low-level repository information.
git rev-parse HEAD
git rev-parse v1.0.0
git rev-parse "v1.0.0^{}"
git rev-parse --is-inside-work-tree
git rev-parse --show-toplevel
git rev-parse --git-dir| Parameter | Required? | Meaning |
|---|---|---|
| Option | Usually | Example: --show-toplevel. |
Use git rev-parse --show-toplevel when you need to confirm the repository root before running path-sensitive commands such as git add ..
Moves the current branch or unstages changes, depending on how it is used.
git reset
git reset --hard HEAD| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Plain git reset unstages staged changes. |
| Commit | Optional | Moves/reset to a specific commit, depending on mode. |
| Option | Meaning |
|---|---|
--soft |
Moves HEAD but keeps changes staged. |
--mixed |
Default. Moves HEAD and unstages changes. |
--hard |
Discards working tree changes. Dangerous. |
Be careful with:
git reset --hardIt can discard local work.
Restores files or unstages staged changes.
git restore file-name.md
git restore --staged file-name.md
git restore --source v1.0.0 -- README.md| Parameter | Required? | Meaning |
|---|---|---|
| Path | Usually | File or folder to restore/unstage. |
| Option | Meaning |
|---|---|
--staged |
Unstages a file without changing working-tree content. |
--source commit-or-tag |
Restores from a specific commit, branch, or tag. |
To undo an accidental staged deletion:
git restore --staged file-name.md
git restore file-name.mdRemoves a file from Git's index without deleting the local working-tree file.
git rm --cached path/to/fileFor a folder:
git rm -r --cached path/to/folder| Parameter | Required? | Meaning |
|---|---|---|
path/to/file |
Yes | File to stop tracking. |
| Option | Meaning |
|---|---|
-r |
Required when removing a folder from the index. |
Use this when a file was already tracked but should now be ignored by .gitignore.
Removes tracked files from Git and usually from the working tree.
git rm file-name.md
git rm -r folder-name
git rm --cached file-name.md| Parameter | Required? | Meaning |
|---|---|---|
| Path | Yes | File or folder to remove from tracking. |
| Option | Meaning |
|---|---|
-r |
Recursively removes a tracked folder. |
--cached |
Stops tracking the file but keeps it locally. |
-f |
Forces removal. Use carefully. |
Use git rm --cached when a file should remain on disk but should no longer be tracked.
Shows details about a commit, tag, or object.
git show
git show HEAD
git show v1.0.0
git show --stat --oneline HEAD
git show --no-patch --format=fuller HEAD| Parameter | Required? | Meaning |
|---|---|---|
| Object, commit, or tag | Optional | If omitted, Git shows HEAD. |
| Option | Meaning |
|---|---|
--stat |
Shows file-change statistics. |
--oneline |
Condenses commit display. |
--name-only |
Shows names of changed files. |
--name-status |
Shows names and status codes for changed files. |
--no-patch |
Shows metadata/message without a patch. |
--format=fuller |
Shows fuller commit metadata. |
Useful for inspecting tags:
git show v1.0.0Shows ignored files in status output.
git status --ignoredUse this to confirm that .gitignore rules are matching the files you expect.
Shows the current repository state.
git status
git status --short| Parameter | Required? | Meaning |
|---|---|---|
| None | No | Shows status for the current repository. |
| Option | Meaning |
|---|---|
--short |
Shows compact two-column status. |
--branch |
Shows branch and tracking information. |
--renames |
Detects and reports renames in status output. |
Use this constantly:
git statusEspecially before and after:
git add
git commit
git pushAdds another Git repository as a submodule inside the current repository.
git submodule add https://github.com/OWNER/REPO.git path/to/submodule| Parameter | Required? | Meaning |
|---|---|---|
| Repository URL | Yes | The repository to add as a submodule. |
| Local path | Usually | Where the submodule should appear. |
Submodules are usually not recommended for simple beginner educational example repos. Use them only when there is a true technical dependency.
Copies another repository into a folder while preserving a relationship to the source.
git subtree add --prefix=path/to/folder https://github.com/OWNER/REPO.git main --squash| Parameter | Required? | Meaning |
|---|---|---|
| Prefix path | Yes | The folder where the subtree content should go. |
| Repository URL | Yes | The source repository. |
| Branch | Usually | The branch to import. |
Subtree is more advanced than needed for most simple educational examples. Prefer separate repositories connected by README links and GitHub metadata unless you specifically need subtree behavior.
Switches branches or creates and switches to a new branch.
git switch main
git switch -c new-branch-name
git switch --detach v1.0.0| Parameter | Required? | Meaning |
|---|---|---|
| Branch name | Required when switching branches | The branch to switch to. |
| Option | Meaning |
|---|---|
-c branch-name |
Creates and switches to a new branch. |
--detach commit-or-tag |
Checks out a commit or tag without creating a branch. |
Use git switch instead of older git checkout for clearer branch switching.
Lists, creates, verifies, or deletes tag references.
git tag --list
git tag --list "component/*" --sort=-version:refname
git tag -a v1.0.0 -m "Version 1.0.0"
git tag -a component/v1.0.0 -m "Release Component v1.0.0"
git tag -d v1.0.0
git tag -n99 v1.0.0| Option | Meaning |
|---|---|
-a |
Create an annotated tag. |
-m <message> |
Supply the annotation message. |
-d <tag> |
Delete a local tag reference. |
--list [pattern] |
List tags, optionally filtering by pattern. |
--sort=<key> |
Sort output, including version-aware version:refname. |
-n<number> |
Show up to the requested annotation lines. |
A namespaced name such as component/v1.0.0 is a normal tag name. Deleting a tag does not delete its commit. Push or delete the remote copy separately with git push.
Provide ignore patterns for intentionally untracked paths.
| Source | Scope | Shared? |
|---|---|---|
.gitignore |
Repository/directory | Yes, when committed |
.git/info/exclude |
One clone | No |
core.excludesFile |
One user's repositories | No |
git check-ignore -v path/to/file
git status --ignored
git ls-files --others --ignored --exclude-standardIgnore rules do not stop tracking files already in the index. Use git rm --cached when appropriate.
.gitkeep is not a Git command. It is a placeholder convention for preserving an intentionally empty folder.
Example:
exports/.gitkeep
Then:
git add exports/.gitkeep
git commit -m "chore: keep exports folder placeholder"GitHub topics are repository metadata tags. They are optional.
Example topics:
git
github
documentation
versioning
static-site
New-Item -ItemType Directory -Force ".\docs\reference"Creates a folder safely. The -Force option makes the command tolerant if the folder already exists.
Test-Path .\.gitattributesReturns True if .gitattributes exists at the current path and False if it does not.
| Placeholder | Meaning | Example |
|---|---|---|
OWNER |
GitHub username or organization | octocat |
REPO |
Repository name | hello-world |
origin |
Conventional remote name | origin |
main |
Common primary branch name | main |
branch-name |
Any branch name | update-guide |
file-name.md |
File path | README.md |
old-name.md |
Old file path before rename | old-guide.md |
new-name.md |
New file path after rename | new-guide.md |
old-tag |
Earlier version tag | v1.0.0 |
new-tag |
Later version tag | v1.1.0 |
HEAD |
Current checked-out commit | HEAD |
HEAD~1 |
Parent of current commit | HEAD~1 |
bad-commit-sha |
Commit whose message needs correction | a1b2c3d |
vX.Y.Z |
Version tag placeholder | v1.22.0 |
RELEASES.md |
Optional production-release documentation file | RELEASES.md |
IMPORT-NOTES.md |
Optional historical reconstruction notes file | IMPORT-NOTES.md |
Used by commands such as:
git status --short
git diff --name-status| Code | Meaning |
|---|---|
A |
Added |
M |
Modified |
D |
Deleted |
R |
Renamed |
C |
Copied |
?? |
Untracked file |
| Goal | Command to start with |
|---|---|
| See what changed | git status |
| Stage everything intentionally | git add -A |
| Stage current-folder changes | git add . |
| Save staged changes | git commit -m "Message" |
| Push current branch | git push |
| Push current branch explicitly | git push origin main |
| Push one version tag | git push origin vX.Y.Z |
| Push first branch and set upstream | git push -u origin main |
| Push a version tag | git push origin vX.Y.Z |
| Compare version tags | git diff --name-status old-tag..new-tag |
| List files in a tag | git ls-tree -r --name-only tag-name |
| Rename a tracked file | git mv old-name.md new-name.md |
| Inspect line endings | git ls-files --eol |
| Verify remote tag | git ls-remote --tags origin vX.Y.Z |
| Save commit pointed to by a tag | git rev-list -n 1 vX.Y.Z |
| Search commit messages for typo | git log --all --grep="TYPO" --oneline |
| Fix latest commit message | git commit --amend -m "Corrected message" |
| Need a commit-message prefix | Choose based on change type: docs, fix, feat, chore, style, or refactor |
| Fix older commit message | git rebase -i <bad-commit-sha>~1, then reword |
| Push rewritten private branch | git push --force-with-lease |
| Restore accidental file deletion | git restore file-name.md |
| Unstage a file | git restore --staged file-name.md |
| View history | git log --oneline --decorate --graph --all |
| Check unpushed commits | git log --oneline origin/main..main |