DevOps Reference
Git Commands Reference
Git commands fall into a handful of jobs: recording work (add, commit), moving between lines of work (branch, checkout, merge, rebase), syncing with a remote (fetch, pull, push), reading history (log, diff, blame) and undoing things (restore, reset, revert). This reference lists each command with what it does and a working example.
Browse by Category
Setup & Config
10Identity, defaults and per-repository settings.
Staging & Committing
16Recording work into the repository.
Branching & Merging
20Working on parallel lines of development.
Remotes & Syncing
13Exchanging commits with other copies of the repo.
History & Inspection
14Reading what happened and who changed what.
Undoing Changes
11Backing out work safely — and unsafely.
Stashing & Cleanup
8Parking work in progress and clearing junk.
Advanced & Rewriting
18Submodules, worktrees, bisect and history surgery.
All Git Commands (110)
Setup & Config (10)
Identity, defaults and per-repository settings.
| Command | Does | Description | Example |
|---|---|---|---|
git init | Create a repo | Turns the current directory into a Git repository by creating a .git folder. Existing files are left untracked until you add them. | git init |
git clone | Copy a repo | Downloads a full repository including all history, and sets the source up as the remote named origin. | git clone [email protected]:user/repo.git |
git clone --depth | Shallow clone | Clones only the most recent commits. Much faster on large repositories, at the cost of an incomplete history. | git clone --depth 1 <url> |
git config --global user.name | Set your name | Sets the author name recorded on every commit you make. Without --global it applies to the current repository only. | git config --global user.name "Ada" |
git config --global user.email | Set your email | Sets the author email on your commits. This is what hosting platforms match against to attribute the commit to your account. | git config --global user.email [email protected] |
git config --list | Show settings | Prints every configuration value in effect, merged from the system, global and repository-level files. | git config --list --show-origin |
git config --global alias.<name> | Make an alias | Defines a shorthand for a longer command, so a workflow you run twenty times a day becomes two characters. | git config --global alias.st status |
git config core.editor | Set the editor | Chooses the editor Git opens for commit messages and interactive rebases. | git config --global core.editor "code --wait" |
git config --global init.defaultBranch | Default branch name | Sets the branch name used by git init for new repositories, commonly main. | git config --global init.defaultBranch main |
git help | Read the manual | Opens the full documentation for any command — the authoritative source for the flags this table only samples. | git help rebase |
Does
Create a repo
Description
Turns the current directory into a Git repository by creating a .git folder. Existing files are left untracked until you add them.
Example
git init
Does
Copy a repo
Description
Downloads a full repository including all history, and sets the source up as the remote named origin.
Example
git clone [email protected]:user/repo.git
Does
Shallow clone
Description
Clones only the most recent commits. Much faster on large repositories, at the cost of an incomplete history.
Example
git clone --depth 1 <url>
Does
Set your name
Description
Sets the author name recorded on every commit you make. Without --global it applies to the current repository only.
Example
git config --global user.name "Ada"
Does
Set your email
Description
Sets the author email on your commits. This is what hosting platforms match against to attribute the commit to your account.
Example
git config --global user.email [email protected]
Does
Show settings
Description
Prints every configuration value in effect, merged from the system, global and repository-level files.
Example
git config --list --show-origin
Does
Make an alias
Description
Defines a shorthand for a longer command, so a workflow you run twenty times a day becomes two characters.
Example
git config --global alias.st status
Does
Set the editor
Description
Chooses the editor Git opens for commit messages and interactive rebases.
Example
git config --global core.editor "code --wait"
Does
Default branch name
Description
Sets the branch name used by git init for new repositories, commonly main.
Example
git config --global init.defaultBranch main
Does
Read the manual
Description
Opens the full documentation for any command — the authoritative source for the flags this table only samples.
Example
git help rebase
Staging & Committing (16)
Recording work into the repository.
| Command | Does | Description | Example |
|---|---|---|---|
git status | What has changed | Lists staged, unstaged and untracked files, plus how far ahead or behind the branch is from its remote. | git status -sb |
git add | Stage a file | Copies the current state of a file into the staging area, marking it for inclusion in the next commit. | git add src/app.ts |
git add . | Stage everything | Stages every change under the current directory, including new files. Check git status first — this is how secrets get committed. | git add . |
git add -p | Stage hunk by hunk | Walks through each change and asks whether to stage it, so one file can be split across two commits. | git add -p src/app.ts |
git add -u | Stage tracked only | Stages modifications and deletions to files Git already tracks, but ignores new untracked files. | git add -u |
git commit | Record a commit | Saves the staged snapshot to the repository with a message. Nothing unstaged is included. | git commit -m "Fix login redirect" |
git commit -a | Stage and commit | Stages every tracked file that changed and commits in one step. Untracked files are still skipped. | git commit -am "Tidy imports" |
git commit --amend | Fix the last commit | Replaces the previous commit with a new one including whatever is staged. Rewrites history, so avoid after pushing. | git commit --amend --no-edit |
git commit --fixup | Mark a fix-up | Creates a commit labelled to be squashed into an earlier one by a later autosquash rebase. | git commit --fixup a1b2c3d |
git rm | Delete and stage | Removes a file from the working tree and stages the deletion in one step. | git rm old-file.txt |
git rm --cached | Untrack, keep file | Stops tracking a file without deleting it — the fix after committing something that should have been ignored. | git rm --cached .env |
git mv | Rename and stage | Renames or moves a file and stages the change. Equivalent to mv plus git add plus git rm. | git mv old.ts new.ts |
git diff | Unstaged changes | Shows what you have changed but not yet staged, line by line. | git diff |
git diff --staged | Staged changes | Shows what is staged and would go into the next commit — the review to do before every commit. | git diff --staged |
git diff <a>..<b> | Compare commits | Shows the difference between any two commits, branches or tags. | git diff main..feature |
git diff --stat | Change summary | Prints only the files touched and how many lines changed in each, rather than the full patch. | git diff --stat main |
Does
What has changed
Description
Lists staged, unstaged and untracked files, plus how far ahead or behind the branch is from its remote.
Example
git status -sb
Does
Stage a file
Description
Copies the current state of a file into the staging area, marking it for inclusion in the next commit.
Example
git add src/app.ts
Does
Stage everything
Description
Stages every change under the current directory, including new files. Check git status first — this is how secrets get committed.
Example
git add .
Does
Stage hunk by hunk
Description
Walks through each change and asks whether to stage it, so one file can be split across two commits.
Example
git add -p src/app.ts
Does
Stage tracked only
Description
Stages modifications and deletions to files Git already tracks, but ignores new untracked files.
Example
git add -u
Does
Record a commit
Description
Saves the staged snapshot to the repository with a message. Nothing unstaged is included.
Example
git commit -m "Fix login redirect"
Does
Stage and commit
Description
Stages every tracked file that changed and commits in one step. Untracked files are still skipped.
Example
git commit -am "Tidy imports"
Does
Fix the last commit
Description
Replaces the previous commit with a new one including whatever is staged. Rewrites history, so avoid after pushing.
Example
git commit --amend --no-edit
Does
Mark a fix-up
Description
Creates a commit labelled to be squashed into an earlier one by a later autosquash rebase.
Example
git commit --fixup a1b2c3d
Does
Delete and stage
Description
Removes a file from the working tree and stages the deletion in one step.
Example
git rm old-file.txt
Does
Untrack, keep file
Description
Stops tracking a file without deleting it — the fix after committing something that should have been ignored.
Example
git rm --cached .env
Does
Rename and stage
Description
Renames or moves a file and stages the change. Equivalent to mv plus git add plus git rm.
Example
git mv old.ts new.ts
Does
Unstaged changes
Description
Shows what you have changed but not yet staged, line by line.
Example
git diff
Does
Staged changes
Description
Shows what is staged and would go into the next commit — the review to do before every commit.
Example
git diff --staged
Does
Compare commits
Description
Shows the difference between any two commits, branches or tags.
Example
git diff main..feature
Does
Change summary
Description
Prints only the files touched and how many lines changed in each, rather than the full patch.
Example
git diff --stat main
Branching & Merging (20)
Working on parallel lines of development.
| Command | Does | Description | Example |
|---|---|---|---|
git branch | List branches | Lists local branches and marks the current one. Add -a to include remote-tracking branches. | git branch -a |
git branch <name> | Create a branch | Creates a branch pointing at the current commit without switching to it. | git branch feature/login |
git branch -d | Delete a branch | Deletes a branch, refusing if it holds commits not merged anywhere. -D forces it through. | git branch -d feature/login |
git branch -m | Rename a branch | Renames a branch. With one argument it renames the branch you are on. | git branch -m old-name new-name |
git branch --merged | Find merged branches | Lists branches fully contained in the current one — the safe candidates for deletion. | git branch --merged main |
git switch | Change branch | Moves to another branch. The modern, single-purpose replacement for git checkout. | git switch main |
git switch -c | Create and switch | Creates a branch and moves onto it in one step. | git switch -c feature/api |
git switch - | Previous branch | Jumps back to the branch you were on before, like cd - in a shell. | git switch - |
git checkout | Switch (legacy) | The older command that both switches branches and restores files, which is why switch and restore were split out of it. | git checkout main |
git merge | Merge a branch | Joins another branch into the current one, creating a merge commit unless it can fast-forward. | git merge feature/login |
git merge --no-ff | Force a merge commit | Always creates a merge commit even when a fast-forward is possible, keeping the branch visible in history. | git merge --no-ff feature/login |
git merge --squash | Squash merge | Stages the combined result of a branch without committing it, so the whole branch lands as one commit. | git merge --squash feature/login |
git merge --abort | Cancel a merge | Restores the state from before the merge started. The escape hatch when conflicts are worse than expected. | git merge --abort |
git rebase | Replay commits | Moves your commits so they start from the tip of another branch, producing linear history and new hashes. | git rebase main |
git rebase -i | Interactive rebase | Opens an editor listing recent commits so you can reorder, reword, squash or drop them before they are replayed. | git rebase -i HEAD~5 |
git rebase --onto | Move a range | Transplants a specific range of commits onto a new base — the tool for a branch built on the wrong parent. | git rebase --onto main old-base feature |
git rebase --continue | Resume a rebase | Carries on after you have resolved a conflict and staged the result. | git rebase --continue |
git rebase --abort | Cancel a rebase | Returns the branch to exactly where it was before the rebase began. | git rebase --abort |
git cherry-pick | Copy one commit | Applies the change from a single commit onto the current branch as a new commit. | git cherry-pick a1b2c3d |
git cherry-pick -n | Pick without commit | Applies the change and stages it but leaves committing to you, so several picks can be combined. | git cherry-pick -n a1b2c3d |
Does
List branches
Description
Lists local branches and marks the current one. Add -a to include remote-tracking branches.
Example
git branch -a
Does
Create a branch
Description
Creates a branch pointing at the current commit without switching to it.
Example
git branch feature/login
Does
Delete a branch
Description
Deletes a branch, refusing if it holds commits not merged anywhere. -D forces it through.
Example
git branch -d feature/login
Does
Rename a branch
Description
Renames a branch. With one argument it renames the branch you are on.
Example
git branch -m old-name new-name
Does
Find merged branches
Description
Lists branches fully contained in the current one — the safe candidates for deletion.
Example
git branch --merged main
Does
Change branch
Description
Moves to another branch. The modern, single-purpose replacement for git checkout.
Example
git switch main
Does
Create and switch
Description
Creates a branch and moves onto it in one step.
Example
git switch -c feature/api
Does
Previous branch
Description
Jumps back to the branch you were on before, like cd - in a shell.
Example
git switch -
Does
Switch (legacy)
Description
The older command that both switches branches and restores files, which is why switch and restore were split out of it.
Example
git checkout main
Does
Merge a branch
Description
Joins another branch into the current one, creating a merge commit unless it can fast-forward.
Example
git merge feature/login
Does
Force a merge commit
Description
Always creates a merge commit even when a fast-forward is possible, keeping the branch visible in history.
Example
git merge --no-ff feature/login
Does
Squash merge
Description
Stages the combined result of a branch without committing it, so the whole branch lands as one commit.
Example
git merge --squash feature/login
Does
Cancel a merge
Description
Restores the state from before the merge started. The escape hatch when conflicts are worse than expected.
Example
git merge --abort
Does
Replay commits
Description
Moves your commits so they start from the tip of another branch, producing linear history and new hashes.
Example
git rebase main
Does
Interactive rebase
Description
Opens an editor listing recent commits so you can reorder, reword, squash or drop them before they are replayed.
Example
git rebase -i HEAD~5
Does
Move a range
Description
Transplants a specific range of commits onto a new base — the tool for a branch built on the wrong parent.
Example
git rebase --onto main old-base feature
Does
Resume a rebase
Description
Carries on after you have resolved a conflict and staged the result.
Example
git rebase --continue
Does
Cancel a rebase
Description
Returns the branch to exactly where it was before the rebase began.
Example
git rebase --abort
Does
Copy one commit
Description
Applies the change from a single commit onto the current branch as a new commit.
Example
git cherry-pick a1b2c3d
Does
Pick without commit
Description
Applies the change and stages it but leaves committing to you, so several picks can be combined.
Example
git cherry-pick -n a1b2c3d
Remotes & Syncing (13)
Exchanging commits with other copies of the repo.
| Command | Does | Description | Example |
|---|---|---|---|
git remote -v | List remotes | Shows the configured remotes and their fetch and push URLs. | git remote -v |
git remote add | Add a remote | Registers another copy of the repository under a short name you can then fetch from and push to. | git remote add upstream <url> |
git remote set-url | Change a remote URL | Points an existing remote at a different URL — used when switching between HTTPS and SSH. | git remote set-url origin git@... |
git remote remove | Delete a remote | Removes a remote and all its remote-tracking branches. | git remote remove upstream |
git fetch | Download commits | Retrieves new commits and updates remote-tracking branches without changing your working tree. | git fetch origin |
git fetch --all --prune | Fetch and tidy | Fetches from every remote and deletes local remote-tracking branches whose upstream is gone. | git fetch --all --prune |
git pull | Fetch and merge | Fetches from the upstream branch and merges it into the current one in a single step. | git pull origin main |
git pull --rebase | Fetch and rebase | Replays your local commits on top of the fetched ones instead of merging, avoiding a merge commit on every sync. | git pull --rebase |
git push | Upload commits | Sends your commits to the remote branch, refusing if it would discard commits you do not have. | git push origin main |
git push -u | Push and track | Pushes and records the remote branch as upstream, so later pushes and pulls need no arguments. | git push -u origin feature/api |
git push --force-with-lease | Safe force push | Overwrites the remote branch only if it still matches what you last fetched — the safe form of --force. | git push --force-with-lease |
git push --delete | Delete remote branch | Removes a branch from the remote without touching your local copy. | git push origin --delete old-branch |
git push --tags | Push tags | Uploads tags, which ordinary pushes do not include. | git push origin --tags |
Does
List remotes
Description
Shows the configured remotes and their fetch and push URLs.
Example
git remote -v
Does
Add a remote
Description
Registers another copy of the repository under a short name you can then fetch from and push to.
Example
git remote add upstream <url>
Does
Change a remote URL
Description
Points an existing remote at a different URL — used when switching between HTTPS and SSH.
Example
git remote set-url origin git@...
Does
Delete a remote
Description
Removes a remote and all its remote-tracking branches.
Example
git remote remove upstream
Does
Download commits
Description
Retrieves new commits and updates remote-tracking branches without changing your working tree.
Example
git fetch origin
Does
Fetch and tidy
Description
Fetches from every remote and deletes local remote-tracking branches whose upstream is gone.
Example
git fetch --all --prune
Does
Fetch and merge
Description
Fetches from the upstream branch and merges it into the current one in a single step.
Example
git pull origin main
Does
Fetch and rebase
Description
Replays your local commits on top of the fetched ones instead of merging, avoiding a merge commit on every sync.
Example
git pull --rebase
Does
Upload commits
Description
Sends your commits to the remote branch, refusing if it would discard commits you do not have.
Example
git push origin main
Does
Push and track
Description
Pushes and records the remote branch as upstream, so later pushes and pulls need no arguments.
Example
git push -u origin feature/api
Does
Safe force push
Description
Overwrites the remote branch only if it still matches what you last fetched — the safe form of --force.
Example
git push --force-with-lease
Does
Delete remote branch
Description
Removes a branch from the remote without touching your local copy.
Example
git push origin --delete old-branch
Does
Push tags
Description
Uploads tags, which ordinary pushes do not include.
Example
git push origin --tags
History & Inspection (14)
Reading what happened and who changed what.
| Command | Does | Description | Example |
|---|---|---|---|
git log | Show history | Lists commits from newest to oldest with author, date and message. | git log |
git log --oneline --graph | Visual history | Compresses each commit to one line and draws the branch structure as ASCII art. | git log --oneline --graph --all |
git log -p | History with diffs | Shows the full patch introduced by each commit alongside its message. | git log -p src/app.ts |
git log --author | Filter by author | Restricts the log to commits by a particular person. | git log --author="Ada" |
git log --since | Filter by date | Restricts the log to a time window, accepting relative phrases as well as dates. | git log --since="2 weeks ago" |
git log -S | Search code history | Finds commits where the number of occurrences of a string changed — how you find when a line was introduced or removed. | git log -S "apiKey" |
git log --follow | History across renames | Tracks a file through renames, which plain log stops at. | git log --follow src/new-name.ts |
git show | Inspect one commit | Prints a commit's message, metadata and full diff. | git show a1b2c3d |
git blame | Who wrote this line | Annotates each line of a file with the commit and author that last changed it. | git blame -L 10,20 src/app.ts |
git shortlog -sn | Commits per author | Counts commits by author, sorted — a quick contribution summary. | git shortlog -sn |
git reflog | Where HEAD has been | Logs every position HEAD has held locally, including commits no branch points at. This is how you recover from a bad reset. | git reflog |
git tag | List or create tags | Names a specific commit, usually a release. With -a it creates an annotated tag carrying a message and author. | git tag -a v1.2.0 -m "Release 1.2.0" |
git describe | Name a commit | Produces a human-readable name for a commit based on the nearest tag, used to stamp build versions. | git describe --tags |
git shortlog | Grouped log | Groups commit messages by author, the format used to draft release notes. | git shortlog v1.0..v1.1 |
Does
Show history
Description
Lists commits from newest to oldest with author, date and message.
Example
git log
Does
Visual history
Description
Compresses each commit to one line and draws the branch structure as ASCII art.
Example
git log --oneline --graph --all
Does
History with diffs
Description
Shows the full patch introduced by each commit alongside its message.
Example
git log -p src/app.ts
Does
Filter by author
Description
Restricts the log to commits by a particular person.
Example
git log --author="Ada"
Does
Filter by date
Description
Restricts the log to a time window, accepting relative phrases as well as dates.
Example
git log --since="2 weeks ago"
Does
Search code history
Description
Finds commits where the number of occurrences of a string changed — how you find when a line was introduced or removed.
Example
git log -S "apiKey"
Does
History across renames
Description
Tracks a file through renames, which plain log stops at.
Example
git log --follow src/new-name.ts
Does
Inspect one commit
Description
Prints a commit's message, metadata and full diff.
Example
git show a1b2c3d
Does
Who wrote this line
Description
Annotates each line of a file with the commit and author that last changed it.
Example
git blame -L 10,20 src/app.ts
Does
Commits per author
Description
Counts commits by author, sorted — a quick contribution summary.
Example
git shortlog -sn
Does
Where HEAD has been
Description
Logs every position HEAD has held locally, including commits no branch points at. This is how you recover from a bad reset.
Example
git reflog
Does
List or create tags
Description
Names a specific commit, usually a release. With -a it creates an annotated tag carrying a message and author.
Example
git tag -a v1.2.0 -m "Release 1.2.0"
Does
Name a commit
Description
Produces a human-readable name for a commit based on the nearest tag, used to stamp build versions.
Example
git describe --tags
Does
Grouped log
Description
Groups commit messages by author, the format used to draft release notes.
Example
git shortlog v1.0..v1.1
Undoing Changes (11)
Backing out work safely — and unsafely.
| Command | Does | Description | Example |
|---|---|---|---|
git restore | Discard edits | Throws away unstaged changes to a file, restoring it from the index. The change is not recoverable. | git restore src/app.ts |
git restore --staged | Unstage a file | Removes a file from the staging area while keeping your edits in the working tree. | git restore --staged src/app.ts |
git restore --source | Restore from a commit | Replaces a file with its contents at a specific commit. | git restore --source HEAD~2 src/app.ts |
git reset --soft | Undo commit, keep staged | Moves the branch back but leaves everything staged, ready to be recommitted differently. | git reset --soft HEAD~1 |
git reset --mixed | Undo commit and staging | The default. Moves the branch back and unstages the changes, leaving them in the working tree. | git reset HEAD~1 |
git reset --hard | Undo and destroy | Moves the branch back and discards the changes entirely. Recoverable only through the reflog, and only for a while. | git reset --hard HEAD~1 |
git revert | Undo with a commit | Creates a new commit that reverses an earlier one, leaving history intact. The correct undo on a shared branch. | git revert a1b2c3d |
git revert -m 1 | Revert a merge | Undoes a merge commit, with -m naming which parent to treat as the mainline. | git revert -m 1 a1b2c3d |
git clean -n | Preview cleanup | Lists the untracked files a clean would delete, without deleting anything. Always run this first. | git clean -nd |
git clean -fd | Delete untracked | Removes untracked files and directories. Not recoverable — nothing was ever committed. | git clean -fd |
git checkout -- | Discard (legacy) | The older way to throw away working-tree changes to a file, now better expressed as git restore. | git checkout -- src/app.ts |
Does
Discard edits
Description
Throws away unstaged changes to a file, restoring it from the index. The change is not recoverable.
Example
git restore src/app.ts
Does
Unstage a file
Description
Removes a file from the staging area while keeping your edits in the working tree.
Example
git restore --staged src/app.ts
Does
Restore from a commit
Description
Replaces a file with its contents at a specific commit.
Example
git restore --source HEAD~2 src/app.ts
Does
Undo commit, keep staged
Description
Moves the branch back but leaves everything staged, ready to be recommitted differently.
Example
git reset --soft HEAD~1
Does
Undo commit and staging
Description
The default. Moves the branch back and unstages the changes, leaving them in the working tree.
Example
git reset HEAD~1
Does
Undo and destroy
Description
Moves the branch back and discards the changes entirely. Recoverable only through the reflog, and only for a while.
Example
git reset --hard HEAD~1
Does
Undo with a commit
Description
Creates a new commit that reverses an earlier one, leaving history intact. The correct undo on a shared branch.
Example
git revert a1b2c3d
Does
Revert a merge
Description
Undoes a merge commit, with -m naming which parent to treat as the mainline.
Example
git revert -m 1 a1b2c3d
Does
Preview cleanup
Description
Lists the untracked files a clean would delete, without deleting anything. Always run this first.
Example
git clean -nd
Does
Delete untracked
Description
Removes untracked files and directories. Not recoverable — nothing was ever committed.
Example
git clean -fd
Does
Discard (legacy)
Description
The older way to throw away working-tree changes to a file, now better expressed as git restore.
Example
git checkout -- src/app.ts
Stashing & Cleanup (8)
Parking work in progress and clearing junk.
| Command | Does | Description | Example |
|---|---|---|---|
git stash | Park changes | Saves uncommitted changes onto a stack and returns the working tree to a clean state. | git stash |
git stash -u | Stash untracked too | Includes untracked files in the stash, which the plain command leaves behind. | git stash -u |
git stash push -m | Named stash | Stashes with a message, so a stack several deep is still readable. | git stash push -m "wip: search" |
git stash list | List stashes | Shows the stash stack with the branch and message of each entry. | git stash list |
git stash pop | Restore and remove | Reapplies the most recent stash and deletes it from the stack. | git stash pop |
git stash apply | Restore and keep | Reapplies a stash but leaves it on the stack, so it can be applied to another branch too. | git stash apply stash@{2} |
git stash drop | Delete a stash | Removes one entry from the stash stack without applying it. | git stash drop stash@{0} |
git gc | Compact the repo | Garbage-collects unreachable objects and repacks the database, shrinking a repository that has grown slow. | git gc --aggressive --prune=now |
Does
Park changes
Description
Saves uncommitted changes onto a stack and returns the working tree to a clean state.
Example
git stash
Does
Stash untracked too
Description
Includes untracked files in the stash, which the plain command leaves behind.
Example
git stash -u
Does
Named stash
Description
Stashes with a message, so a stack several deep is still readable.
Example
git stash push -m "wip: search"
Does
List stashes
Description
Shows the stash stack with the branch and message of each entry.
Example
git stash list
Does
Restore and remove
Description
Reapplies the most recent stash and deletes it from the stack.
Example
git stash pop
Does
Restore and keep
Description
Reapplies a stash but leaves it on the stack, so it can be applied to another branch too.
Example
git stash apply stash@{2}
Does
Delete a stash
Description
Removes one entry from the stash stack without applying it.
Example
git stash drop stash@{0}
Does
Compact the repo
Description
Garbage-collects unreachable objects and repacks the database, shrinking a repository that has grown slow.
Example
git gc --aggressive --prune=now
Advanced & Rewriting (18)
Submodules, worktrees, bisect and history surgery.
| Command | Does | Description | Example |
|---|---|---|---|
git bisect start | Hunt a bad commit | Begins a binary search through history to find the commit that introduced a bug. | git bisect start |
git bisect good/bad | Mark a bisect step | Tells the search whether the checked-out commit works, halving the remaining range each time. | git bisect bad HEAD |
git bisect run | Automate bisect | Runs a script at each step and uses its exit code to decide, finding the culprit with no manual input. | git bisect run npm test |
git worktree add | Second working tree | Checks out another branch into a separate directory sharing the same repository, so you can build two branches at once. | git worktree add ../hotfix hotfix |
git worktree list | List worktrees | Shows every working tree attached to the repository and the branch each holds. | git worktree list |
git submodule add | Nest a repo | Embeds another repository at a fixed commit inside this one. | git submodule add <url> libs/vendor |
git submodule update --init | Fetch submodules | Clones and checks out the submodules a fresh clone leaves empty. --recursive handles nested ones. | git submodule update --init --recursive |
git rebase --autosquash | Apply fix-ups | Automatically reorders and squashes commits made with --fixup into their targets. | git rebase -i --autosquash main |
git filter-branch | Rewrite all history | Rewrites every commit in a repository. Slow and error-prone — git-filter-repo is the recommended replacement. | git filter-branch --tree-filter ... |
git archive | Export a snapshot | Produces a tar or zip of a tree with no .git directory, for shipping a release. | git archive -o rel.zip HEAD |
git apply | Apply a patch | Applies a diff file to the working tree without creating a commit. | git apply fix.patch |
git format-patch | Export commits | Writes each commit as a mailable patch file, the workflow used by mailing-list projects such as the kernel. | git format-patch -3 |
git cherry | Find unmerged commits | Lists commits on one branch that have not been applied to another, matched by content rather than hash. | git cherry -v main feature |
git fsck | Check integrity | Verifies the object database and reports dangling commits — another route to recovering lost work. | git fsck --lost-found |
git rev-parse | Resolve a reference | Turns a name such as HEAD or a branch into the full commit hash. The building block of Git scripting. | git rev-parse --short HEAD |
git update-ref | Move a ref directly | Sets a branch or tag to a specific commit without checking anything out. | git update-ref refs/heads/main a1b2c3d |
git sparse-checkout | Partial checkout | Populates only chosen directories of a large monorepo in the working tree. | git sparse-checkout set apps/web |
git notes add | Annotate a commit | Attaches a note to a commit without altering it, so review or build metadata can be added after the fact. | git notes add -m "Reviewed" a1b2c3d |
Does
Hunt a bad commit
Description
Begins a binary search through history to find the commit that introduced a bug.
Example
git bisect start
Does
Mark a bisect step
Description
Tells the search whether the checked-out commit works, halving the remaining range each time.
Example
git bisect bad HEAD
Does
Automate bisect
Description
Runs a script at each step and uses its exit code to decide, finding the culprit with no manual input.
Example
git bisect run npm test
Does
Second working tree
Description
Checks out another branch into a separate directory sharing the same repository, so you can build two branches at once.
Example
git worktree add ../hotfix hotfix
Does
List worktrees
Description
Shows every working tree attached to the repository and the branch each holds.
Example
git worktree list
Does
Nest a repo
Description
Embeds another repository at a fixed commit inside this one.
Example
git submodule add <url> libs/vendor
Does
Fetch submodules
Description
Clones and checks out the submodules a fresh clone leaves empty. --recursive handles nested ones.
Example
git submodule update --init --recursive
Does
Apply fix-ups
Description
Automatically reorders and squashes commits made with --fixup into their targets.
Example
git rebase -i --autosquash main
Does
Rewrite all history
Description
Rewrites every commit in a repository. Slow and error-prone — git-filter-repo is the recommended replacement.
Example
git filter-branch --tree-filter ...
Does
Export a snapshot
Description
Produces a tar or zip of a tree with no .git directory, for shipping a release.
Example
git archive -o rel.zip HEAD
Does
Apply a patch
Description
Applies a diff file to the working tree without creating a commit.
Example
git apply fix.patch
Does
Export commits
Description
Writes each commit as a mailable patch file, the workflow used by mailing-list projects such as the kernel.
Example
git format-patch -3
Does
Find unmerged commits
Description
Lists commits on one branch that have not been applied to another, matched by content rather than hash.
Example
git cherry -v main feature
Does
Check integrity
Description
Verifies the object database and reports dangling commits — another route to recovering lost work.
Example
git fsck --lost-found
Does
Resolve a reference
Description
Turns a name such as HEAD or a branch into the full commit hash. The building block of Git scripting.
Example
git rev-parse --short HEAD
Does
Move a ref directly
Description
Sets a branch or tag to a specific commit without checking anything out.
Example
git update-ref refs/heads/main a1b2c3d
Does
Partial checkout
Description
Populates only chosen directories of a large monorepo in the working tree.
Example
git sparse-checkout set apps/web
Does
Annotate a commit
Description
Attaches a note to a commit without altering it, so review or build metadata can be added after the fact.
Example
git notes add -m "Reviewed" a1b2c3d
Frequently Asked Questions
What is the difference between git reset and git revert?
git reset moves the branch pointer backwards, so the commits stop being part of the branch — it rewrites history and is unsafe on a branch others have pulled. git revert creates a new commit that undoes an earlier one, leaving history intact, which is the correct choice on any shared branch.
What is the difference between git merge and git rebase?
Merge joins two branches with a merge commit, preserving exactly what happened. Rebase replays your commits on top of another branch, producing a linear history but new commit hashes. Rebase branches you have not pushed; merge anything others have already pulled.
How do I undo the last commit but keep my changes?
Run git reset --soft HEAD~1. The commit is removed and everything it contained is left staged, ready to recommit. Use --mixed to leave the changes unstaged instead, and --hard only when you genuinely want the work destroyed.
What is the difference between git fetch and git pull?
git fetch downloads new commits from the remote and updates your remote-tracking branches without touching your working tree. git pull does a fetch and then immediately merges (or rebases) into your current branch. Fetch first when you want to look before you integrate.