Interview Q&A / Git & GitHub
Git & GitHub interview questions
Branching, merge vs rebase, resolving conflicts and pull requests, the way teams actually work.
Prefer rapid revision? Practise Git & GitHub short interview answers →
- Is cherry-pick the same as rebase for moving one commit?
They are close, but cherry-pick copies a commit and leaves the original where it is, while rebase moves a whole sequence of your branch's commits and abandons the originals.
IntermediateQuick answer1 min read - What are Conventional Commits?
Conventional Commits is a message format type(scope): description, with types like feat, fix, docs and chore. It makes history readable and lets tools generate changelogs and pick version bumps automatically.
IntermediateQuick answer1 min read - What are the risks of relying on git stash?
Stashes are local, easy to forget, and git stash drop or clear removes them. They are not on GitHub, so a disk failure loses them. For anything you care about, commit on a temporary branch instead.
IntermediateQuick answer1 min read - What do --ours and --theirs mean when resolving a conflict?
In a merge, ours is the branch you are on and theirs is the branch being merged in. git checkout --ours file or --theirs file takes that whole version of the file. In a rebase the meaning is reversed.
IntermediateQuick answer1 min read - What does git clean do, and how do you use it safely?
git clean deletes untracked files from the working directory. Because they were never committed, they cannot be recovered, so always run git clean -n first to preview what would be removed.
IntermediateQuick answer1 min read - What does git merge --squash do?
It combines all changes from a branch into the working tree and staging area as one set of changes, without creating a merge commit. You then commit once, so the branch's many commits become a single commit on main.
IntermediateQuick answer1 min read - What does git pull --rebase do, and why do many teams prefer it?
It fetches remote changes and replays your local commits on top of them instead of creating a merge commit. The result is a linear history without noisy 'Merge branch main' commits.
IntermediateQuick answer1 min read - What does git restore do, and how is it different from git checkout for files?
git restore discards changes in a file or unstages it. It was added in Git 2.23 to split the file-restoring job away from git checkout, which also switched branches and confused beginners.
IntermediateQuick answer1 min read - What happens when a conflict occurs during a rebase, and how do you continue?
Rebase stops at the commit that conflicts. You fix the files, git add them, and run git rebase --continue. You can skip that commit with --skip or give up entirely with --abort.
IntermediateQuick answer1 min read - What if you cannot test a commit during git bisect?
Run git bisect skip. Git moves to a nearby commit instead. If too many are skipped, it may only narrow the bug to a range of commits rather than one.
IntermediateQuick answer1 min read - What is a CODEOWNERS file in GitHub?
CODEOWNERS maps file paths to people or teams. GitHub automatically requests their review on pull requests touching those paths, and with branch protection their approval can be mandatory.
IntermediateQuick answer1 min read - What is a detached HEAD, and how do you get out of it?
Detached HEAD means HEAD points directly at a commit instead of a branch, for example after checking out a tag or an old hash. New commits there belong to no branch and can be lost unless you create a branch.
IntermediateQuick answer1 min read - What is a Git submodule?
A submodule lets one repository contain another repository at a fixed commit. The parent stores only the URL and the exact commit hash, so the child's history stays separate.
IntermediateQuick answer1 min read - What is git bisect, and when would you use it?
git bisect does a binary search through commit history to find the exact commit that introduced a bug. You mark one good and one bad commit, and Git checks out midpoints until it finds the first bad one.
IntermediateQuick answer1 min read - What is git rebase -i, and what can you do with it?
Interactive rebase opens a list of your recent commits so you can reorder, reword, squash, edit, or drop them before they are replayed. It is the standard way to tidy a messy branch before opening a pull request.
IntermediateQuick answer1 min read - What is git reflog, and how does it help you recover lost commits?
The reflog is a local log of every position HEAD and your branches have been at. If you reset or delete something by mistake, you find the old commit hash in the reflog and point a branch back to it.
IntermediateQuick answer1 min read - What is the difference between a project .gitignore and a global gitignore?
The project .gitignore is committed and shared with the team. A global gitignore, set through core.excludesFile, applies to every repo on your machine and holds personal clutter like editor folders and OS files.
IntermediateQuick answer1 min read - What is the difference between git push --force and --force-with-lease?
--force overwrites the remote branch no matter what. --force-with-lease overwrites only if the remote is still where you last saw it, so you do not wipe out commits a teammate pushed meanwhile.
IntermediateQuick answer1 min read - What is the difference between squash and fixup in an interactive rebase?
Both merge a commit into the one above it. squash lets you combine and edit the two commit messages, while fixup silently discards the second message and keeps the first as it is.
IntermediateQuick answer1 min read - When is cherry-pick a bad idea?
Overusing it creates duplicate commits with different hashes, which later cause confusing conflicts when the branches are merged. Prefer merging or rebasing when you want the whole branch.
IntermediateQuick answer1 min read - When would you use git restore instead of git reset?
Use git restore to change files, either discarding edits or unstaging a path. Use git reset to move the branch pointer to another commit. Restore never moves your branch.
IntermediateQuick answer1 min read - Why does .gitignore not work for a file that is already tracked?
Git ignores only untracked files. If a file was committed earlier, it stays tracked despite the rule. Run git rm --cached file to stop tracking it, then commit; the file remains on disk.
IntermediateQuick answer1 min read - Why is a submodule in detached HEAD state, and is that a problem?
The parent pins an exact commit, so Git checks the submodule out at that commit, not on a branch. It is normal. To edit the submodule, check out a branch inside it first, or commits can get lost.
IntermediateQuick answer1 min read - How can you automate git bisect with a script?
Use git bisect run with a command that exits 0 for good and non-zero (except 125) for bad. Git then runs it at each step and finds the culprit commit without manual answers.
AdvancedQuick answer1 min read - How do you cherry-pick a merge commit?
Add -m 1 (or -m 2) to say which parent is the mainline, because a merge commit has two parents and Git cannot know which side's diff to copy.
AdvancedQuick answer1 min read - How do you remove a submodule properly?
Run git submodule deinit -f path, then git rm -f path, and delete the leftover .git/modules/path folder. Commit the change. Just deleting the folder leaves stale configuration behind.
AdvancedQuick answer1 min read - What does git rebase --onto do?
git rebase --onto newbase oldbase branch moves only the commits after oldbase onto a different base. It lets you transplant part of a branch, for example when a branch was started from the wrong parent.
AdvancedQuick answer1 min read - What does git revert -n (--no-commit) do?
It applies the reverse of a commit to your working tree and index without committing. This lets you revert several commits and record them together as a single commit.
AdvancedQuick answer1 min read - What is git rerere?
rerere means reuse recorded resolution. When enabled, Git remembers how you resolved a conflict and applies the same fix automatically if the same conflict appears again, such as during repeated rebases.
AdvancedQuick answer1 min read