Git Command Set¶
Command-indexed quick lookup for
gitsubcommands. Full semantics: git-scm.com.
See also: Collaboration · Gitignore & Hooks · Commit Conventions · Git memo (Notes)
Creating Projects¶
git init¶
Create a new Git repository in the current directory (adds a .git folder).
In plain terms: “Turn this folder into a local Git repo.”
Effect: Working tree files unchanged; new empty local history; remote unchanged.
Typical uses:
- Brand-new project under version control
- Existing code folder that never had Git before
git clone¶
Copy a remote repository to local disk, including history and default remote origin.
In plain terms: “Download the whole repo and wire up origin for me.”
Effect: New project directory with .git; tracking remote configured; checkout of default branch.
Typical uses:
- First time on a team project
- Fresh machine, need a clean copy
git clone https://github.com/ORG/REPO.git
git clone git@github.com:ORG/REPO.git
git clone git@github.com:ORG/REPO.git my-folder
Basic Commands¶
git status¶
Show how the working tree, staging area, and latest commit differ.
In plain terms: “What did I change, what’s staged, what’s still untracked?”
Effect: Read-only; prints modified / staged / untracked paths.
Typical uses:
- Before commit, sanity-check the file list
- After merge conflicts, see what’s left to fix
git add¶
Stage changes for the next commit.
In plain terms: “Pick what goes into the next commit.”
Effect: Working tree content unchanged; index updated; remote unchanged.
Typical uses:
- Commit only some files, leave WIP unstaged
- Track a new file for the first time
# Stage one file
git add file.md
# Stage a directory
git add sub_folder/
# Stage everything changed (new, modified, deleted)
git add .
# Stage hunks interactively
git add -p file.md
git diff¶
Show line-level differences between working tree, index, and commits.
In plain terms: “Show me the actual line changes before I commit.”
Effect: Read-only diff output.
Typical uses:
- Review before commit
- See what’s staged vs last commit
# Working tree vs index (not staged yet)
git diff
# Index vs last commit (staged, not committed)
git diff --cached
# Against a specific commit
git diff f2a9c2cad88cfe2206dc7e6c271d673365723add
# Against previous commit
git diff HEAD^
git commit¶
Record staged snapshot as a new commit on the current branch.
In plain terms: “Save a snapshot with a message.”
Effect: Branch moves forward locally; working tree usually unchanged; remote waits for push.
Typical uses:
- Checkpoint finished work
- Fix last message or add forgotten files before push (
--amend)
git commit -m 'add file.'
# Stage + commit tracked files only (skips new untracked files)
git commit -am 'update a tracked file.'
git commit --amend
git commit --amend --no-edit
Note
Use amend only on the last commit that is not pushed yet. On shared branches after push, add a new commit or use revert per team policy. Message format: Commit Conventions.
git restore¶
Restore files in the working tree or unstage them (Git 2.23+).
In plain terms: “Undo my edits to a file, or unstage something I added by mistake.”
Effect: Can drop unstaged edits or remove paths from index; remote unchanged.
Typical uses:
- Messy file edits—go back to last commit version
- Staged too much—unstage first
git reset¶
Move branch HEAD to a given commit; optionally reset index and working tree.
In plain terms: “Walk the branch pointer back—maybe keep my edits, maybe throw everything away.”
Effect: --soft moves commits only; default --mixed unstages but keeps files; --hard wipes uncommitted work. Remote unchanged until you force-push.
Typical uses:
- “Oops” last commit but keep code in working tree
- Nuclear reset local branch to match old commit (careful with
--hard)
Mode cheat sheet:
--soft: move HEAD only--mixed(default): move HEAD, clear staging, keep working tree--hard: align HEAD, index, and working tree—data loss
Warning
git reset --hard permanently drops uncommitted work. On shared branches already pushed, avoid rewriting history—use git revert instead.
git rm¶
Stop tracking a file and optionally delete it from disk.
In plain terms: “Git, this file is gone—delete it from disk too if needed.”
Effect: Deletion staged for next commit; remote updates after push.
Typical uses:
- Remove dead code cleanly
- Prefer over plain
rm+ manualgit addfor tracked files
| Command | Deletes file on disk | Git knows | Auto-staged | Need git add? | Risk |
|---|---|---|---|---|---|
rm file | yes | no | no | yes | low |
git rm file | yes | yes | yes | no | medium |
git rm -r folder | yes | yes | yes | no | high |
git rm file.md
git rm -r folder
git rm --cached file.md
# After commit, status shows:
# deleted: start.md
Warning
git rm -r deletes a whole tree—double-check the path.
git gc¶
Garbage-collect and compress local repository data.
In plain terms: “Vacuum and compress my local .git folder.”
Effect: Local repo size/performance only; no remote impact.
Typical uses:
- Repo feels bloated after lots of fetches
- Routine maintenance on large clones
git fsck¶
Verify integrity of local Git objects.
In plain terms: “Is my repo corrupted? Any dangling objects?”
Effect: Diagnostic; --lost-found may copy objects under .git/lost-found.
Typical uses:
- Errors about missing/corrupt objects
- Suspect disk issues
Branching and Merging¶
git branch¶
List, create, rename, or delete local branches (does not switch).
In plain terms: “What branches exist? Create or rename one without switching.”
Effect: git branch dev creates a pointer only; -d removes merged locals; -r/-a show remotes.
Typical uses:
- Create feature branch name before switching
- Prune merged locals after release
git branch
git branch --list
git branch -r
git branch -a
git branch dev
git branch --merged
git branch --no-merged
git branch -m test test_new
git branch -d test_new
git switch¶
Switch branches or create-and-switch (Git 2.23+, branch switching only).
In plain terms: “Jump to another branch to work there.”
Effect: Working tree matches target branch; dirty state may block switch or need stash.
Typical uses:
- Move from
maintofeature - Track remote branch locally
git checkout¶
Legacy but ubiquitous: switch branches, detach at tags, old-style file restore.
In plain terms: “Switch branch or check out a tag—still everywhere in older docs and scripts.”
Effect: Same as switch for branches; detached HEAD on tags; -f discards local changes (risky).
Typical uses:
- Scripts still using
checkout - Inspect release tag
git checkout dev
git checkout -b new-branch
git checkout --track origin/dev
git checkout -b dev origin/dev
git checkout v2.0
git merge¶
Integrate another branch into the branch you are on.
In plain terms: “Bring feature into my current branch.”
Effect: Fast-forward or merge commit; conflicts need manual fix; remote after push.
Typical uses:
- Merge feature back to
main - Squash feature into one commit on main
git checkout main
git merge dev
git merge --no-ff feature
git merge --squash feature
git commit -m "Squashed and merged feature branch"
git rebase¶
Replay your commits on top of another base—straighter history than merge.
In plain terms: “Replay my commits on latest main so history looks linear.”
Effect: Rewrites local commit bases; push after rebase often needs force; can rewrite shared remote if forced.
Typical uses:
- Update feature branch onto fresh
main pull --rebasefor linear history- Squash WIP commits interactively
Warning
Avoid rebasing commits already pushed on shared branches unless the team agrees. If you must push, use git push --force-with-lease. Fork / PR workflow: Collaboration.
git cherry-pick¶
Apply existing commit(s) onto the current branch.
In plain terms: “I want that one commit, not a full branch merge.”
Effect: New commit(s) on current branch with same changes, new hashes; remote after push.
Typical uses:
- Backport hotfix to release branch
- Move a commit off the wrong branch
git log¶
Browse commit history.
In plain terms: “Who changed what and when on this branch?”
Effect: Read-only; format with --oneline, --graph, etc.
Typical uses:
- Find regression commit
- List commits since a tag
git stash¶
Temporarily shelve uncommitted work.
In plain terms: “I need a clean tree to switch tasks—hold my WIP.”
Effect: Working tree clean (by default); stash stack holds patches; restore with pop/apply.
Typical uses:
- Urgent fix on another branch mid-feature
- Try something risky, drop stash if it fails
git stash
git stash push -m "wip: login form"
git stash list
git stash show -p stash@{0}
git stash pop
git stash apply stash@{0}
git stash branch dev_new stash@{0}
git stash drop stash@{0}
git tag¶
Label a commit—usually for releases.
In plain terms: “Mark this commit as v1.0.0 so we can find it later.”
Effect: New ref pointing at commit; annotated tags carry message; push tags to share.
Typical uses:
- Release tagging
- CI builds triggered by tags
git tag
git tag -a v0.0.1 -m 'release 0.0.1'
git tag -d v0.0.2
git tag -v v0.0.1
git show v0.0.1
git log v0.0.1
git push origin v0.0.1
git push origin --tags
Sharing and Updating Projects¶
git fetch¶
Download new commits and refs from remote without merging into current branch.
In plain terms: “See what’s new on the server without touching my working files.”
Effect: Updates origin/* tracking refs; current branch and working tree unchanged.
Typical uses:
- Before push, check if remote moved
- Manual integrate via merge/rebase after fetch
git pull¶
Fetch plus integrate into current branch (merge or rebase).
In plain terms: “Get remote updates and merge them into where I am now.”
Effect: Local branch may fast-forward or create merge commit; conflicts possible.
Typical uses:
- Start of day sync on
main - Team prefers linear history:
pull --rebase
git push¶
Upload local commits to a remote branch.
In plain terms: “Send my commits to origin.”
Effect: Remote branch advances; -u sets upstream; force push rewrites remote.
Typical uses:
- Push feature for PR
- After rebase, update remote with
--force-with-lease
git remote¶
Manage remote names (usually origin) and URLs.
In plain terms: “What remotes exist, what URLs, add/rename/remove them.”
Effect: Edits local .git/config; does not change server by itself.
Typical uses:
- Add upstream after fork
- Point origin to new repo URL after migration
git remote
git remote -v
git remote show origin
git remote add origin git@github.com:ORG/REPO.git
git remote rename origin upstream
git remote remove origin
git remote get-url origin
git remote set-url origin git@github.com:ORG/NEW_REPO.git
git remote update origin
git remote prune origin
git remote prune -n origin
Inspection and Comparison¶
git show¶
Show metadata and patch for a commit, tag, or object.
In plain terms: “What exactly changed in this commit?”
Effect: Read-only; works with HEAD^, HEAD~2, merge parents, reflog.
Typical uses:
- Review a specific hash
- Understand merge commit parents
git show d4d30167d01d10d7de5e7aca699863dc27735678
git show d4d30
git show v0.0.1
git show HEAD
git show HEAD~0
git show HEAD^
git show HEAD^1
git show HEAD^^
git show HEAD^~1
git show HEAD^2
git show HEAD@{2}
git show main@{yesterday}
Note
^: parent commit.HEAD^=HEAD^1(first parent on merges).~n: walk back n commits on first-parent chain (HEAD~2).HEAD^2: second parent of a merge commit.@{n}/@{yesterday}: reflog and date selectors—seegitrevisions(7).
Setup and Config¶
git config (global)¶
Machine-wide defaults (name, email, etc.).
In plain terms: “Default author info for all repos on this laptop.”
Effect: Writes ~/.gitconfig; affects future commits.
git config --global user.name "your name"
git config --global user.email "your email"
git config --global --list
git config (repository)¶
Overrides global settings for this repo only.
In plain terms: “Use a different email for this one work project.”
Effect: Writes .git/config; wins over global for this repo.