8 questions

Git interview questions and answers

Junior questions

  1. 1. How does git commit differ from git push?

    Answer

    git commit saves a snapshot of the staged changes in the local repository without sending it anywhere. git push sends local commits to a remote repository, for example GitHub. Before committing you stage changes with git add. So you can make several commits offline and push them together later.

    Follow-up question: What do you do if git push is rejected because the remote has new commits?

    Learn the topic in the course: Git: getting started

  2. 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 HEAD shows which branch you are on. Branches let you work on tasks in parallel without breaking the main branch. You create and switch with git switch -c name or git checkout -b name.

    Follow-up question: What is a detached HEAD, and how do you leave it without losing commits?

    Learn the topic in the course: Git: getting started

  3. 3. What is the difference between git fetch and git pull?

    Answer

    git fetch downloads new commits and branches from the remote and updates branches like origin/main, but does not touch your local branches. git pull runs fetch and then immediately integrates the changes into the current branch with a merge or a rebase. fetch is safe: you can look at what arrived first and only then decide how to integrate it.

    Follow-up question: What does git pull --rebase do, and when is it better than a plain pull?

    Learn the topic in the course: Git: getting started

  4. 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 with git add and finish the merge with a commit. If something goes wrong, git merge --abort returns to the state before the merge.

    Follow-up question: How can a team reduce the number of conflicts?

    Learn the topic in the course: Git: getting started

  5. 5. What are git stash and the .gitignore file for?

    Answer

    git stash temporarily shelves uncommitted changes and gives you a clean working copy, for example to switch to another branch. You bring them back with git stash pop or git stash apply. .gitignore lists file patterns Git should not track: node_modules, build output, .env with secrets. It does not affect files already added to the repository, those have to be removed with git rm --cached.

    Follow-up question: How do you commit an empty folder if Git does not track empty directories?

    Learn the topic in the course: Git: getting started

Middle questions

  1. 1. How does git merge differ from git rebase? When do you use each?

    Answer

    merge joins the histories of branches and creates a merge commit, keeping the real history unchanged. rebase moves 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?

    Learn the topic in the course: Git: getting started

  2. 2. How does git reset differ from git revert?

    Answer

    git reset moves the branch pointer to another commit, effectively removing the later commits from history. The --soft, --mixed and --hard modes decide whether the changes stay in the index, in the working copy, or disappear entirely. git revert creates a new commit that undoes the changes of a given commit and does not rewrite history. That is why revert is used for commits already pushed to a shared branch.

    Follow-up question: How do you recover commits after a mistaken git reset --hard?

    Learn the topic in the course: Git: getting started

  3. 3. What does git cherry-pick do, 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 from main to 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?

    Learn the topic in the course: Git: getting started

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.

Other topics