Interview questions · Book 06
Version Control interview questions
159 questions from 40 pages, each with a short answer. Say your answer first, then open the question to check it.
p. 1 · 4 questions
.gitignore
1
What is a .gitignore file?
A .gitignore file is a plain-text file listing patterns for files and folders Git should not track, such as dependencies, build output, logs, and secrets.
2
Why is my .gitignore not working?
The most common reason is that the file was already tracked before you added the rule, because
.gitignoreonly affects untracked files. Rungit rm --cached <file>(add-rfor a folder) and commit, and usegit check-ignore -v <file>to see which rule, if any, matches.3
Should I commit the .gitignore file?
Yes. Committing
.gitignoreshares the same ignore rules with everyone on the team. For personal rules, such as files created by your own editor, use.git/info/excludeor a global ignore file set withgit config --global core.excludesFile.4
Does .gitignore protect secrets?
Only if the secret was never committed. If a file containing passwords or API keys was ever pushed, it remains in the repository history, so revoke the exposed keys and issue new ones.
p. 2 · 4 questions
Branch
1
What is a branch in Git?
A branch in Git is an independent line of development that lets you work on a feature or fix in isolation, without affecting the main code until you merge it.
2
What is the difference between git switch and git checkout?
git switchis a newer command, added in Git 2.23, that does only one job: changing branches.git checkoutcan also switch branches but handles other tasks too, such as restoring files, which many beginners find confusing.3
What is the difference between a branch and a fork?
A branch is a separate line of work inside the same repository. A fork is a full copy of a repository, typically under a different account, often used to contribute to projects you don't have write access to.
4
Why is the default branch called main instead of master?
Around 2020, major Git hosting platforms and many projects changed their default branch name from
mastertomainto use more inclusive language. Git itself lets you pick any default name with theinit.defaultBranchsetting.
p. 3 · 4 questions
Cherry-pick
1
What is a cherry-pick in Git?
A cherry-pick in Git copies the changes from one specific commit onto your current branch as a new commit, without merging the rest of the branch it came from.
2
What is the difference between cherry-pick and merge?
Merge brings every commit from another branch into yours. Cherry-pick copies only the specific commits you choose, creating new commits on your branch.
3
Can I cherry-pick multiple commits?
Yes. List several hashes, as in
git cherry-pick a1b2c3d e4f5a6b, or use a range such asgit cherry-pick A..B, which applies every commit afterAup to and includingB.4
Is cherry-picking bad practice?
Not in itself, but it should be used deliberately. Cherry-picking the same change onto many branches duplicates commits and can make history and future merges confusing, so it works best for targeted cases like hotfixes and backports.
p. 4 · 4 questions
Code Review
1
What is a code review?
A code review is the practice of having other developers check code changes before they are merged, to catch bugs, improve quality, and share knowledge.
2
What should I look for in a code review?
Check that the change does what it claims, handles edge cases and errors, is readable, and fits the existing design. Also look for security issues, missing tests, and unnecessary complexity, and leave style nitpicks to automated formatters and linters.
3
What is the difference between a code review and a pull request?
A pull request is the mechanism for proposing that a change be merged. Code review is the activity of examining that change; it usually happens inside a pull request, but it can also be done in person or through pair programming.
4
Can AI replace human code review?
AI tools can quickly catch many common bugs, style issues, and security risks, and are increasingly used as a first reviewer. They don't yet reliably understand a team's goals, product context, or design trade-offs, so most teams still require human approval before merging.
p. 5 · 4 questions
Commit
1
What is a commit in Git?
A commit is a saved snapshot of a project's files in Git, recorded with a unique ID, an author, a timestamp, and a message describing what changed.
2
How do I undo a commit in Git?
To undo a commit that has already been shared, use
git revert <hash>, which creates a new commit that reverses it. To undo your most recent local commit while keeping its changes in your files, usegit reset --soft HEAD~1.3
What makes a good commit message?
A good commit message starts with a short summary line, around 50 characters, written as a command, such as 'Add password reset email'. If more context is needed, add a blank line followed by a longer explanation of why the change was made.
4
What is the difference between git commit and git push?
git commitsaves a snapshot in your local repository.git pushuploads your local commits to a remote repository so others can see them.
p. 6 · 4 questions
Conventional Commits
1
What are Conventional Commits?
Conventional Commits is a specification for structured commit messages like feat: add search, so tools can write changelogs and pick versions automatically.
2
What are the Conventional Commits types?
The specification requires feat and fix. Common additional types, taken from the Angular convention, are docs, style, refactor, perf, test, build, ci, chore and revert.
3
How do Conventional Commits relate to semantic versioning?
A fix commit corresponds to a patch release, a feat commit to a minor release, and any commit marked as a breaking change to a major release, so tools can calculate the next version from the history.
4
How do I enforce Conventional Commits?
Use commitlint in a commit-msg Git hook, for example with Husky, and in CI to reject messages that don't follow the format. Squash-merge workflows can also check the pull request title instead.
p. 7 · 4 questions
Detached HEAD
1
What is a detached HEAD in Git?
A detached HEAD is a Git state in which you have checked out a specific commit instead of a branch, so any new commits you make don't belong to any branch.
2
How do I fix a detached HEAD in Git?
If you made no commits you want to keep, switch back to a branch with
git switch main. If you did make commits, first rungit switch -c <new-branch>to put them on a branch, then merge or push that branch as usual.3
Can I lose work in a detached HEAD state?
Commits made while detached are at risk, because no branch points to them. After you switch away they can still be found with
git reflogfor a while, 30 days by default, before Git's garbage collection may delete them, so create a branch if you want to keep them.4
Why does checking out a tag cause a detached HEAD?
A tag is a fixed pointer that is not supposed to move, so Git can't let new commits advance it the way a branch does. Instead it points
HEADstraight at the tagged commit, leaving any new commits unattached until you create a branch.
p. 8 · 4 questions
Fast-Forward Merge
1
What is a fast-forward merge?
A fast-forward merge happens when the target has no new commits since the other branch split off, so Git just moves its pointer ahead with no merge commit.
2
When does Git do a fast-forward merge?
When the branch being merged into has no commits that the other branch lacks, meaning history hasn't diverged. Git then moves the pointer forward by default instead of creating a merge commit.
3
What does --no-ff do?
It forces Git to create a merge commit even when a fast-forward would be possible, so the feature's commits stay grouped under one merge in the history.
4
Fast-forward or merge commit: which is better?
It is a team preference. Fast-forwards and rebases give a clean, linear history that is easy to read and bisect. Merge commits preserve when and how features were integrated. Many teams squash or rebase small changes and use merge commits for larger features.