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 .gitignore, and what would you do if you accidentally committed a password or API key?

    .gitignore lists files Git should not track, like .env, node_modules and build output. If a secret was committed, first revoke or rotate it, because anyone who saw it can use it. Then stop tracking the file, add it to .gitignore, and if it was pushed, remove it from history with git filter-repo or BFG and force push.

    IntermediateIn-depth4 min read
  2. What is the difference between git merge and git rebase?

    Both bring changes from one branch into another. Merge joins the two histories with a new merge commit and keeps everything as it happened. Rebase replays your commits on top of the other branch, giving a straight, linear history but rewriting commit IDs. Never rebase commits that others have already pulled.

    IntermediateIn-depth3 min read
  3. What is the difference between git reset and git revert?

    git revert makes a new commit that undoes an earlier commit, so history is kept and it is safe on shared branches. git reset moves your branch pointer back to an older commit and removes the later commits from the branch; --soft keeps the changes staged, --mixed keeps them unstaged, and --hard throws them away. Use reset only on local, unpushed work.

    IntermediateIn-depth3 min read
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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