55 Git interview questions with detailed answers, sorted from basic to advanced. Free to practice, with progress saved automatically.
Git is a distributed version control system that records the history of code changes and lets people work in parallel on branches. The questions cover merge versus rebase, branching strategies, commits and history, recovering lost work, force push safety, worktrees, Conventional Commits, and automation with hooks.
Git Flow is a branching strategy with 5 branch types: - main: the code running in production. - develop: where feature work is integrated. - feature/: new features, branched from develop. - release/: preparing a release (small fixes, version bump). - hotfix/: urgent fixes, branched…
Both bring changes from one branch into another; they differ in the shape of history. Merge creates a merge commit with two parents and leaves existing commits untouched. Rebase takes each commit of the feature branch and replays it on top of the target branch,…
Git Flow: multiple long-lived branches — main, develop, feature/, release/, hotfix/. Fits: long release cycles, maintaining several versions in parallel, large teams needing isolation. Downsides: long-lived feature branches → large merge conflicts; hotfixes must merge into both main and develop. Trunk-based development (TBD): everyone pushes…
GitHub Flow (simple): main is always deployable, all features branch from main, PR → review → merge → deploy immediately. Only one branch type beyond main. Git Flow (complex): main + develop + feature/ + release/ + hotfix/. More process overhead but tighter release control.…
A monorepo fits best with trunk-based development: short branches merged into main within a day or two. Teams block each other not because of branches but because review and CI go through one shared repo, so split both by directory. - CODEOWNERS: with branch protection…
The right order is limit the damage first, fix second. A hotfix is the smallest possible patch, branched from the line running in production, and it does not go through the sprint branch. 1. Scope it and limit the damage. If the bug came from…
A release branch is a branch cut to prepare one release, e.g. release/1.5.0. From that point it only receives bug fixes for 1.5.0, while main (or develop) keeps receiving new features for the next release. Why split it off: if QA tests directly on main,…
Go to GitHub repo → Settings → Branches → Add branch protection rule for main: Minimum required: - Require a pull request before merging — no direct pushes - Require approvals (1-2 reviewers) - Dismiss stale pull request approvals when new commits are pushed —…
Conventional Commits is a convention for commit messages in the form type(scope): description. Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build, revert. Why not just agree: a verbal agreement with nothing checking it means people forget or skip it under deadline pressure.…
Interactive rebase (git rebase -i) lets you rewrite local commit history before sharing it. Common workflow before merging a PR: The editor shows: Commands: - squash (s): fold this commit into the previous one; git opens an editor so you can combine/edit both messages -…
A squash merge combines all commits of a PR into one commit on main. A merge commit keeps every commit of the branch and adds a commit joining the two branches. The choice depends on whether the small commits in the PR are worth keeping.…
Amend the last commit — only when the commit has NOT been pushed: If already pushed (your own branch only): Note: - --amend creates a NEW commit with a different hash — it does not edit in place - NEVER amend commits on main/develop or…
Signed commits verify that a commit genuinely came from the person holding the matching private key — preventing identity forgery (anyone can set git config user.email to your email). GPG signing setup: SSH signing (Git 2.34 or later, easier than GPG because it reuses your…
Atomic commit: each commit contains ONE complete logical change — passes tests on its own, can be reverted on its own, and its message fully describes the change. Why a commit that bundles several things is an anti-pattern: 1. No independent revert: you need to…
Merge: creates a merge commit that joins the two branches and keeps the full history. git log --graph still shows the branch splitting off and coming back. Merge never modifies existing commits. Rebase: takes each commit of the current branch and reapplies it on top…
Fast-forward merge (default when possible): if the current branch is a direct ancestor of the branch being merged, git simply moves the branch pointer forward to the latest commit — no merge commit created. History is linear. No-ff merge (--no-ff): always creates a merge commit…
The hardest conflict is not a simple add/delete but one where both sides refactored the structure: a function was renamed, logic was reorganized. The conflict markers then only show two different blocks of code, not what each side intended. Safe process: 1. Understand context before…
A commit already on main that others have pulled calls for git revert; use git reset only on a local branch nobody else has. git revert git reset --------- What it does Creates a new commit that undoes the old commit's changes Moves the branch…
Pickaxe search — find commits that added or removed a specific string in the code. - git log -S "string" (pickaxe): finds commits where the number of occurrences of the string in a file changed. Because it only counts, a commit that merely moves a…
A branch is just a pointer to a commit. Deleting a branch only removes the pointer; the commits stay in the object store, so the job is to find the SHA of the last commit and create a new branch pointing at it. The quickest…
git blame <file shows for each line: commit hash, author, date, and line content — who wrote that line and when. Genuinely useful use cases: 1. Understanding unfamiliar code: git blame src/utils/pricing.ts -L 23,35 → see line 28 was written by someone 6 months ago…
git push --force: overwrites the remote branch without any checks — if a teammate pushed new commits that you have not fetched, you overwrite and lose their commits. git push --force-with-lease: checks the remote ref before overwriting — only force pushes if the remote ref…
A force push overwrites the branch history on the remote with yours, and commits that only exist on the remote are lost. So only force push a branch only you use, and always use --force-with-lease instead of --force. --force overwrites whatever the remote has. --force-with-lease…
A secret that has been pushed to a remote should be treated as leaked: anyone who cloned the repo, or any bot scanning it, in that window may already have it. So the order is rotate first, clean history second. Cleaning history only tidies the…
git stash puts your uncommitted changes (both the working directory and what you already git add-ed) onto a stack and restores a clean working tree so you can switch tasks. Plain git stash is the same as git stash push. Three commands people mix up:…
git cherry-pick <commit copies one commit from another branch into the current branch — creating a new commit with the same changes but a different hash. When cherry-pick is correct: 1. A hotfix must be applied to several release branches: 2. Getting one specific commit…
git pull is git fetch plus git merge. When you have unpushed local commits and a teammate has pushed new commits to the same branch, the two sides have diverged: each has commits the other lacks, so a fast-forward is impossible. The merge then has…
A pre-commit hook runs on every commit, so it must be fast. Linting a large repo takes tens of seconds, and with a slow hook developers get used to typing --no-verify. lint-staged only runs commands on staged files (already git added), exactly what is about…
commit-msg hook runs after you write the commit message and can reject the commit if the message does not follow the format. Setup: npm i -D @commitlint/cli @commitlint/config-conventional, with .husky/commit-msg (Husky v9) containing the single line npx --no -- commitlint --edit $1. commitlint.config.js: Allow WIP…
pre-push is a hook that runs on the developer's machine right before git push sends commits. If the hook exits with a non-zero code, the push is cancelled. The cost is waiting on every push, and if the suite takes minutes developers get used to…
git fetch downloads new commits/refs from the remote into remote-tracking branches (origin/main) — it does NOT touch your working directory or local branch. git pull = git fetch + git merge (or + git rebase with pull.rebase configured) — it integrates directly into your current…
A conflict happens when git cannot merge automatically: both branches changed the same region of lines in the same file, or one side deleted a file the other side modified. Git pauses the merge and inserts conflict markers into the file: Resolution process: 1. git…
Normally HEAD points to a branch; new commits move the branch along. When you git checkout <commit-hash (or check out a tag), HEAD points directly at a commit — the "detached HEAD" state. You can still read/build/test the code normally, but commits created here belong…
.gitignore only affects files that are not yet tracked. Once a file is committed, git keeps tracking it regardless of .gitignore — you must remove it from the index: Warnings: - Without --cached, git rm deletes the actual file from disk - The file still…
The root difference: SVN is a centralized version control system, Git is distributed. With SVN, history lives only on one server; your machine has just a checkout. With Git, every clone gives you the whole repo with its full history locally. SVN Git --------- Model…
Git is a tool, GitHub is a service. Git is version control software that runs on your machine (written by Linus Torvalds in 2005 for the Linux kernel). GitHub is a website that hosts Git repos on a server and adds collaboration features around them.…