8 questions
Git interview questions and answers
Junior questions
1. How does
git commitdiffer fromgit push?Answer
git commitsaves a snapshot of the staged changes in the local repository without sending it anywhere.git pushsends local commits to a remote repository, for example GitHub. Before committing you stage changes withgit add. So you can make several commits offline and push them together later.Follow-up question: What do you do if
git pushis rejected because the remote has new commits?2. What is a branch in Git, and why do you need one?
Answer
A branch in Git is a lightweight movable pointer to a commit, so creating one costs almost nothing. New commits move the current branch pointer forward, and
HEADshows which branch you are on. Branches let you work on tasks in parallel without breaking the main branch. You create and switch withgit switch -c nameorgit checkout -b name.Follow-up question: What is a detached HEAD, and how do you leave it without losing commits?
3. What is the difference between
git fetchandgit pull?Answer
git fetchdownloads new commits and branches from the remote and updates branches likeorigin/main, but does not touch your local branches.git pullrunsfetchand then immediately integrates the changes into the current branch with a merge or a rebase.fetchis safe: you can look at what arrived first and only then decide how to integrate it.Follow-up question: What does
git pull --rebasedo, and when is it better than a plain pull?4. How does a merge conflict happen, and how do you resolve it?
Answer
A conflict happens when two branches changed the same lines of a file differently, or one changed a file that the other deleted. Git stops the merge and marks the disputed spots with
<<<<<<<,=======and>>>>>>>markers. You keep the correct version by hand, remove the markers, stage the file withgit addand finish the merge with a commit. If something goes wrong,git merge --abortreturns to the state before the merge.Follow-up question: How can a team reduce the number of conflicts?
5. What are
git stashand the.gitignorefile for?Answer
git stashtemporarily shelves uncommitted changes and gives you a clean working copy, for example to switch to another branch. You bring them back withgit stash poporgit stash apply..gitignorelists file patterns Git should not track:node_modules, build output,.envwith secrets. It does not affect files already added to the repository, those have to be removed withgit rm --cached.Follow-up question: How do you commit an empty folder if Git does not track empty directories?
Middle questions
1. How does
git mergediffer fromgit rebase? When do you use each?Answer
mergejoins the histories of branches and creates a merge commit, keeping the real history unchanged.rebasemoves your commits on top of another branch, creating new commits with different hashes, so the history becomes linear. Rebase is handy for updating your own branch before a PR. You must not rebase commits that are already published and used by others, because that rewrites shared history.Follow-up question: What is interactive rebase used for?
2. How does
git resetdiffer fromgit revert?Answer
git resetmoves the branch pointer to another commit, effectively removing the later commits from history. The--soft,--mixedand--hardmodes decide whether the changes stay in the index, in the working copy, or disappear entirely.git revertcreates a new commit that undoes the changes of a given commit and does not rewrite history. That is whyrevertis used for commits already pushed to a shared branch.Follow-up question: How do you recover commits after a mistaken
git reset --hard?3. What does
git cherry-pickdo, and when is it appropriate?Answer
git cherry-pick <hash>takes the changes of a specific commit and applies them on top of the current branch as a new commit with a different hash. It is used to carry over a single fix, for example frommainto a release branch, without merging the whole branch. As with a merge, conflicts can occur. Frequent use creates duplicate commits across branches, so it is a targeted tool.Follow-up question: How do you find the commit that broke a feature if you only know a good and a bad version?
Train these questions
Study the cards, answer by text or voice and get an AI grade. Needs a signed in account.
Answer out loud and get graded
Here you get the questions with model answers. In the ShortLesson app you answer yourself, by text or voice, and AI grades the answer against the rubric and asks a follow-up, like a real interviewer.
The app is coming to the stores soon.