Interview Q&A / Git & GitHub

INTERVIEW Q&A · 97 QUESTIONS

Git & GitHub interview questions

Branching, merge vs rebase, resolving conflicts and pull requests, the way teams actually work.

  1. What is HEAD in Git?

    HEAD is a pointer to the commit you currently have checked out. Normally it points to a branch name, which points to that branch's latest commit, so HEAD marks where your next commit will be added.

    BeginnerQuick answer1 min read
  2. What is origin in Git, and can a repo have more than one remote?

    origin is the default name Git gives to the remote you cloned from. It is just a nickname for a URL, and a repository can have many remotes, such as origin and upstream for a fork.

    BeginnerQuick answer1 min read
  3. What is semantic versioning, and how do Git tags relate to it?

    Semantic versioning uses MAJOR.MINOR.PATCH. Bump major for breaking changes, minor for new backward-compatible features, patch for bug fixes. Teams tag each release commit, such as v2.3.1.

    BeginnerQuick answer1 min read
  4. What is the difference between a fork, a clone and a branch?

    A fork is your own server-side copy of someone else's GitHub repository. A clone is a local copy on your computer. A branch is a separate line of work inside one repository.

    BeginnerQuick answer1 min read
  5. What is the difference between a lightweight tag and an annotated tag?

    A lightweight tag is just a name pointing to a commit. An annotated tag is a full object storing the tagger, date and a message, and can be signed. Annotated tags are recommended for releases.

    BeginnerQuick answer1 min read
  6. What is the difference between git bisect and git blame?

    git blame shows who last changed each line of a file and in which commit. git bisect finds which commit introduced a behavior by testing the project at different points in history.

    BeginnerQuick answer1 min read
  7. What is the difference between git diff and git diff --staged?

    git diff shows changes in your working files that are not yet staged. git diff --staged shows changes already added to the staging area, which is exactly what the next commit will contain.

    BeginnerQuick answer1 min read
  8. What is the difference between git fetch and git pull?

    git fetch downloads new commits from the remote and updates your origin/* references without touching your own branch. git pull does a fetch and then merges (or rebases) the result into your current branch.

    BeginnerQuick answer1 min read
  9. What is the difference between git init and git clone?

    git init turns the current folder into a new, empty repository. git clone copies an existing repository, including its full history, from a URL, and also sets up the origin remote for you.

    BeginnerQuick answer1 min read
  10. What is the difference between git reset --soft, --mixed and --hard?

    All three move the branch to another commit. --soft keeps your changes staged, --mixed (the default) keeps them but unstaged, and --hard throws them away completely.

    BeginnerQuick answer1 min read
  11. What is the difference between git stash pop and git stash apply?

    Both reapply stashed changes to your working tree. pop also deletes the stash entry afterwards, while apply keeps it, so you can reuse it on another branch.

    BeginnerQuick answer1 min read
  12. What is the golden rule of rebasing?

    Never rebase commits that other people have already pulled. Rebase creates new commits with new hashes, so teammates who have the old ones end up with duplicated or conflicting history.

    BeginnerQuick answer1 min read
  13. What is the staging area in Git, and why does it exist?

    The staging area (index) is a holding zone between your working files and the next commit. git add puts chosen changes there, so a commit contains exactly what you picked rather than everything you touched.

    BeginnerQuick answer1 min read
  14. What kinds of files should you put in .gitignore?

    Generated, secret or machine-specific files: dependencies like node_modules, build output, logs, .env files, and editor or OS clutter. Source code and configuration the team needs must stay tracked.

    BeginnerQuick answer1 min read
  15. What makes a good Git commit message?

    A short imperative summary line of about 50 characters, a blank line, then an optional body explaining why the change was made. Describe the reason, not just what the diff already shows.

    BeginnerQuick answer1 min read
  16. What makes a good GitHub issue?

    A clear title, the steps to reproduce, expected versus actual behaviour, environment details, and screenshots or logs. For features, state the problem and the outcome wanted, not only a solution.

    BeginnerQuick answer1 min read
  17. Why are commit messages written in the imperative mood?

    Git's own messages, like 'Merge branch' or 'Revert commit', are imperative, and the convention completes the sentence 'If applied, this commit will...'. It keeps the log consistent and short.

    BeginnerQuick answer1 min read
  18. Why does git push get rejected with 'non-fast-forward', and what should you do?

    The remote branch has commits you do not have, so your push would overwrite them. Pull (or fetch and rebase) first to combine the work, then push again. Do not force-push over shared work.

    BeginnerQuick answer1 min read
  19. Why don't tags appear on GitHub after git push, and how do you push them?

    git push does not send tags by default. Push one with git push origin v1.0, or all with git push --tags. Tags are not created on the remote until you do this.

    BeginnerQuick answer1 min read
  20. Why is the submodule folder empty after cloning, and how do you fix it?

    A normal clone does not download submodule contents. Run git submodule update --init --recursive, or clone with git clone --recurse-submodules next time.

    BeginnerQuick answer1 min read
  21. How can you recover a branch that you deleted by mistake?

    Find the branch's last commit hash with git reflog, then recreate it with git branch name hash. The commits are not erased immediately, so recovery works until Git garbage-collects them.

    IntermediateQuick answer1 min read
  22. How can you turn a stash into its own branch?

    Run git stash branch new-name. Git creates the branch at the commit where you stashed, applies the stash there, and drops it, which avoids conflicts if the original branch has moved on.

    IntermediateQuick answer1 min read
  23. How do you cherry-pick a range of commits, and what if there is a conflict?

    Use git cherry-pick A^..B to copy commits from A through B. On conflict, fix the files, git add them and run git cherry-pick --continue, or use --abort to cancel.

    IntermediateQuick answer1 min read
  24. How do you find out which rule is ignoring a file?

    Run git check-ignore -v path. It prints the file, line number and pattern of the rule that matches, which helps when a file unexpectedly does not show up in git status.

    IntermediateQuick answer1 min read
  25. How do you fix the message of a commit that is not the latest?

    Run git rebase -i, mark that commit as reword, save, and edit the message. Only do this for commits not yet shared, since every commit after it also gets a new hash.

    IntermediateQuick answer1 min read
  26. How do you keep a forked repository up to date with the original project?

    Add the original repo as a second remote called upstream, fetch it, merge or rebase upstream/main into your main, and push to your fork. GitHub also has a Sync fork button for the same job.

    IntermediateQuick answer1 min read
  27. How do you update a submodule to its latest version?

    Go into the submodule, pull the wanted commit, then commit the changed pointer in the parent. Or run git submodule update --remote to fetch the tracked branch's latest commit for you.

    IntermediateQuick answer1 min read
  28. How do you use secrets such as API keys in GitHub Actions safely?

    Store them under repository Settings, Secrets and variables, then read them in the workflow as secrets.NAME. GitHub masks their values in logs, and they are not passed to workflows from forks by default.

    IntermediateQuick answer1 min read
  29. How do you work from an old tag, and can you move or delete a tag?

    Checking out a tag puts you in detached HEAD, so create a branch from it with git switch -c fix v1.0. You can delete a tag with git tag -d, but moving a published tag confuses others and should be avoided.

    IntermediateQuick answer1 min read
  30. How does a three-way merge work in Git?

    Git compares three snapshots: the two branch tips and their common ancestor. Changes made on only one side are taken automatically; if both sides changed the same lines differently, Git reports a conflict.

    IntermediateQuick answer1 min read