Git reference
Setup, daily commands, history recovery and team workflows.
Git records snapshots and relationships. Commit small, meaningful changes, inspect the diff before committing and use branches to isolate work that is not ready for main. When something goes wrong, prefer restore or revert over rewriting history you have already shared.
Work through the guided lesson, then test yourself with a challenge.
Open courseBranches and integration
git branch
git branch --listLists local branches. Add -a for remote refs and -d to delete a merged branch.
Open entrygit checkout
git checkout <branch>The older multi-purpose command for branches and files. Use switch and restore in new workflows.
Open entrygit cherry-pick
git cherry-pick <commit>Applies a single commit from another branch onto the current one.
Open entrygit merge
git merge --no-ff <branch>Integrates another branch. A no-fast-forward merge keeps a visible merge commit.
Open entrygit merge --abort
git merge --abortCancels an in-progress merge and returns to the pre-merge state.
Open entrygit switch
git switch -c <branch>Creates and switches branches. Prefer it over checkout for branch operations.
Open entrygit worktree
git worktree add <path> <branch>Checks out another branch in a separate directory, so you can work without stashing.
Open entryMerge conflict markers
<<<<<<< HEADConflicts mark your side, the incoming side and the common base. Edit the file, remove markers, then stage it.
Open entryBranches and merges
git branch --merged
git branch --mergedLists branches already merged into the current branch.
Open entrygit branch -d
git branch -d <branch>Deletes a branch that has been merged.
Open entrygit branch -D (force delete)
git branch -D <branch>Force-deletes a branch, even with unmerged commits.
Open entrygit branch -m
git branch -m <new-name>Renames the current branch or another branch.
Open entrygit checkout -b
git checkout -b <branch>The older create-and-switch command. Prefer git switch -c.
Open entrygit cherry
git cherry -v mainLists commits on a branch that are not yet upstream, with +/- markers.
Open entrygit merge --ff-only
git merge --ff-only <branch>Fails unless the merge can be a fast-forward, which keeps history linear.
Open entrygit merge --no-commit
git merge --no-commit <branch>Performs a merge but stops before committing so you can review.
Open entrygit merge --squash
git merge --squash <branch>Stages the combined changes without recording a merge commit.
Open entrygit mergetool
git mergetoolOpens the configured visual merge tool for conflicted files.
Open entrygit range-diff
git range-diff <old-range> <new-range>Compares two versions of a patch series after a rebase.
Open entrygit rerere
git rerereReuses recorded conflict resolutions for the same hunks.
Open entrygit switch - (previous)
git switch -Switches back to the previous branch.
Open entryEveryday workflow
git add
git add <path>Stages a specific change for the next commit. Use git add -p to stage part of a file.
Open entrygit blame
git blame <file>Shows which commit last changed each line. Use it to understand context, not to assign blame.
Open entrygit commit
git commit -m "Explain why"Records staged changes. The message should explain why the change exists, not repeat the diff.
Open entrygit commit --amend
git commit --amendReplaces the last commit, for example to fix the message. Do not amend commits that others have pulled.
Open entrygit diff
git diffShows unstaged changes. Use --staged for what will be committed and --stat for a summary.
Open entrygit fetch
git fetchDownloads remote refs without changing your working tree. Inspect before merging.
Open entrygit log
git log --oneline --graph --decorateShows history. The compact graph makes branches and merges readable.
Open entrygit mv
git mv <from> <to>Renames a file and records the move for Git.
Open entrygit pull
git pull --rebaseFetches and integrates remote work. Rebase keeps local commits linear.
Open entrygit push
git push -u origin <branch>Publishes local commits. The first push sets the upstream branch.
Open entrygit rm
git rm <path>Removes a file from the working tree and the index.
Open entrygit show
git show <ref>Shows a commit, tag or object with its diff.
Open entrygit status
git statusShows the working tree and staging state. Start here before guessing what changed.
Open entryHistory and recovery
git clean
git clean -fdRemoves untracked files and directories. Preview with -n first.
Open entrygit reflog
git reflogA local log of where HEAD has been. It is the fastest way to recover commits after a reset.
Open entrygit reset --hard
git reset --hard <ref>Moves the branch and discards working-tree changes. This can destroy uncommitted work.
Open entrygit reset --mixed
git reset HEAD~1Moves the branch back and unstages changes, leaving the working tree intact.
Open entrygit reset --soft
git reset --soft HEAD~1Moves the branch back while keeping changes staged, ideal for re-splitting a commit.
Open entrygit restore
git restore <path>Discards working-tree changes for a path, or restores a staged file with --staged. Be certain before running it.
Open entrygit revert
git revert <commit>Creates a new commit that undoes an earlier one. It is safe on shared history.
Open entrygit stash
git stash push -m "wip"Shelves uncommitted work temporarily. Do not use it as long-term storage.
Open entrygit stash apply vs pop
git stash apply / git stash popapply keeps the stash entry; pop applies and removes it.
Open entryHooks
commit-msg hook
.git/hooks/commit-msgValidates the commit message, for example against Conventional Commits.
Open entrypost-merge hook
.git/hooks/post-mergeRuns after a merge or pull, often to install dependencies.
Open entrypre-commit hook
.git/hooks/pre-commitRuns before a commit is created. Perfect for linting and formatting checks.
Open entrypre-push hook
.git/hooks/pre-pushRuns before pushing. Use it for tests, not for long deployments.
Open entryInspection
git blame -L
git blame -L <start>,<end> <file>Limits blame to a line range.
Open entrygit cat-file
git cat-file -p <object>Prints the contents of any Git object: commit, tree, blob or tag.
Open entrygit count-objects
git count-objects -vReports loose object counts and repository size.
Open entrygit diff --check
git diff --checkReports whitespace errors such as trailing spaces and conflict markers.
Open entrygit diff --stat
git diff --statA summary of changed files and line counts.
Open entrygit diff --word-diff
git diff --word-diffHighlights changed words instead of whole lines, which is easier for prose.
Open entrygit for-each-ref
git for-each-refIterates refs with formatting, great for scripting branch reports.
Open entrygit fsck
git fsckChecks repository integrity and reports dangling or corrupt objects.
Open entrygit hash-object
git hash-object <file>Computes the object id Git would use for a file.
Open entrygit log --author
git log --author=<name>Filters history by author.
Open entrygit log --follow
git log --follow <path>Follows a file across renames.
Open entrygit log --since
git log --since="2 weeks ago"Filters commits by date.
Open entrygit log --stat
git log --statShows changed files and line counts per commit.
Open entrygit log -p
git log -pShows history with the full patch of each commit.
Open entrygit log -S
git log -S<string>Finds commits that added or removed a string, also known as pickaxe.
Open entrygit ls-tree
git ls-tree <tree>Lists the contents of a tree object.
Open entrygit rev-list
git rev-list --count <range>Lists or counts commits reachable from a revision.
Open entrygit shortlog -sn
git shortlog -snA commit count per author, useful for release notes.
Open entrygit show --stat
git show --stat <ref>Shows a commit with a file summary instead of the full patch.
Open entrygit verify-pack
git verify-pack -v <pack>Inspects the contents and sizes of a packfile.
Open entryInspection and internals
git archive
git archive --format=zipExports a snapshot of the tree without the .git directory.
Open entrygit bisect
git bisect startFinds the commit that introduced a bug with a binary search.
Open entrygit grep
git grep <pattern>Searches tracked files without leaving Git.
Open entrygit ls-files
git ls-filesLists tracked files. Combine it with grep for quick audits.
Open entrygit rev-parse
git rev-parse HEADResolves a ref to a commit hash. Useful in scripts.
Open entrygit shortlog
git shortlog -snSummarises commits per author.
Open entrygit show-ref
git show-refLists every ref and its hash.
Open entryMaintenance
git count-objects -v
git count-objects -vShows object counts, pack size and garbage.
Open entrygit filter-repo
git filter-repo --path <path>Rewrites history to remove files or paths. A separate tool from the Git project.
Open entrygit fsck --lost-found
git fsck --lost-foundWrites dangling commits and blobs to .git/lost-found for recovery.
Open entrygit gc
git gcCompresses loose objects and prunes unreachable ones.
Open entrygit lfs
git lfs track "*.psd"Stores large files outside normal Git objects. Requires Git LFS.
Open entrygit maintenance
git maintenance startSchedules background repository maintenance.
Open entrygit prune
git pruneDeletes unreachable objects. Usually run through git gc.
Open entrygit reflog expire
git reflog expire --expire=30.daysTrims old reflog entries. Be careful: this removes a recovery path.
Open entrygit repack
git repack -adPacks all objects into a single packfile and removes redundant packs.
Open entryPatches and plumbing
git am
git am <patch>Applies patches from email, preserving authorship and messages.
Open entrygit apply
git apply <patch>Applies a patch file to the working tree without creating a commit.
Open entrygit apply --check
git apply --check <patch>Tests whether a patch applies cleanly.
Open entrygit bundle
git bundle create <file> --allPacks refs and objects into one file for offline transfer.
Open entrygit bundle verify
git bundle verify <file>Checks that a bundle is valid and can be applied.
Open entrygit commit-tree
git commit-tree <tree> -p <parent>Creates a commit object from a tree without moving a branch. Plumbing.
Open entrygit format-patch
git format-patch <range>Exports commits as email-ready patch files.
Open entrygit read-tree
git read-tree <tree>Loads a tree object into the index. Plumbing used by higher-level commands.
Open entrygit replace
git replace <object> <replacement>Temporarily substitutes one object for another without rewriting history.
Open entrygit symbolic-ref
git symbolic-ref HEADReads or writes symbolic refs such as the current branch pointer.
Open entrygit update-index
git update-index --assume-unchanged <path>Changes the index directly, including performance hints for large files.
Open entrygit write-tree
git write-treeWrites the current index as a tree object and prints its id.
Open entryRewriting and tagging
git describe
git describe --tagsDescribes the current commit relative to the nearest tag.
Open entrygit rebase
git rebase origin/mainReplays commits on a new base. Do not rewrite shared history without coordination.
Open entrygit rebase --abort
git rebase --abortStops an in-progress rebase and restores the original branch state.
Open entrygit tag
git tag -a v1.0 -m "Release 1.0"Marks a release point with an annotated, human-readable tag.
Open entryInteractive rebase
git rebase -i HEAD~3Reword, squash, reorder or drop commits before sharing them.
Open entrySetup and configuration
.gitignore
.gitignoreLists paths Git should not track. Commit it so the whole team shares the rules.
Open entrygit clone
git clone <url>Copies a remote repository, including its history, and configures origin.
Open entrygit config
git config --global user.name "…"Reads or writes configuration. Use --local for repository-specific values.
Open entrygit help
git help <command>Opens the manual for a command. git <command> -h shows a short summary.
Open entrygit init
git initCreates a repository in the current directory.
Open entrygit remote
git remote -vLists or edits remotes. origin is the conventional name for the main remote.
Open entrySetup and remotes
git clone --branch
git clone --branch <name> <url>Clones a specific branch and checks it out.
Open entrygit clone --depth
git clone --depth 1 <url>A shallow clone with limited history. Faster for CI, but blame and log are incomplete.
Open entrygit config alias
git config --global alias.st statusCreates a shortcut for a command or a longer option combination.
Open entrygit config core.autocrlf
git config --global core.autocrlf inputControls line-ending conversion across Windows and Unix.
Open entrygit config pull.rebase
git config --global pull.rebase trueMakes git pull rebase local commits instead of creating merge commits.
Open entrygit init --bare
git init --bare repo.gitCreates a repository without a working tree, used as a server-side remote.
Open entrygit remote add
git remote add <name> <url>Adds a named remote, such as upstream for the original repository.
Open entrygit remote remove
git remote remove <name>Removes a remote and its tracking branches.
Open entrygit remote set-url
git remote set-url origin <url>Changes the URL of an existing remote.
Open entrygit remote show
git remote show originShows fetch and push URLs, tracked branches and remote HEAD.
Open entrygit sparse-checkout
git sparse-checkout set <paths>Checks out only selected paths in a large monorepo.
Open entrygit submodule add
git submodule add <url> <path>Adds another repository as a submodule at a path.
Open entrygit submodule status
git submodule statusLists submodules with their pinned commits.
Open entrygit submodule update
git submodule update --init --recursiveFetches and checks out submodule commits after cloning.
Open entrySnapshot and staging
git add --all
git add --allStages every tracked and untracked change, including deletions.
Open entrygit add --patch
git add --patchStages selected hunks so one commit contains one focused change.
Open entrygit checkout -- (discard)
git checkout -- <path>The older way to discard working-tree changes. Prefer git restore.
Open entrygit clean -n
git clean -nPreviews which untracked files would be removed.
Open entrygit commit --all
git commit --all -m "message"Stages tracked changes and commits them in one step. New files still need git add.
Open entrygit commit --allow-empty
git commit --allow-empty -m "message"Creates a commit with no changes, useful to trigger CI.
Open entrygit commit --fixup
git commit --fixup <commit>Creates a fixup commit to squash into an earlier commit during interactive rebase.
Open entrygit notes
git notes add -m "why"Attaches extra information to a commit without changing its hash.
Open entrygit restore --staged
git restore --staged <path>Unstages a file while keeping the working-tree change.
Open entrygit stash branch
git stash branch <branch>Creates a branch from the commit a stash was made on and applies it there.
Open entrygit stash drop
git stash drop stash@{0}Deletes one stash entry without applying it.
Open entrygit stash list
git stash listLists saved stashes with their index and message.
Open entrygit stash show
git stash show -pShows the diff stored in a stash.
Open entryTurn the reference into recall.
Read the full lesson, then solve the challenge without looking. That is where the knowledge sticks.