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. How do you resolve a merge conflict in Git?

    Run git status to see the conflicted files, open each one and find the markers <<<<<<<, ======= and >>>>>>>. Edit the code to the correct final version, delete the markers, then git add the file and run git commit (or git rebase --continue during a rebase). Test before you push.

    BeginnerIn-depth3 min read
  2. What is a pull request, and how does the code review workflow work?

    A pull request (PR) is a request to merge your branch into another branch, usually main. You push a branch, open a PR with a clear description, automated checks run, teammates review and comment, you push fixes, and once approved the PR is merged and the branch deleted.

    BeginnerIn-depth3 min read
  3. What is git stash, and when would you use it?

    git stash saves your uncommitted changes in a temporary stack and gives you a clean working directory, so you can switch branches or pull without committing half-done work. Later, git stash pop brings the changes back and removes them from the stack; git stash apply brings them back but keeps the stash.

    BeginnerIn-depth3 min read
  4. What is the difference between Git and GitHub?

    Git is a version control tool that runs on your own computer and records the history of your code. GitHub is a website that hosts Git repositories online and adds collaboration features like pull requests, code review, issues and CI. You can use Git without GitHub, but GitHub is built on top of Git.

    BeginnerIn-depth3 min read
  5. Does git stash save untracked files, and how do you include them?

    By default git stash saves only tracked changes. Add -u (--include-untracked) to also stash new files, or -a to include ignored files as well.

    BeginnerQuick answer1 min read
  6. How big should a commit be?

    One logical change that builds and passes tests on its own. Small, focused commits are easier to review, revert, cherry-pick and bisect than a giant commit mixing several unrelated things.

    BeginnerQuick answer1 min read
  7. How can a team reduce merge conflicts?

    Keep branches short-lived, pull or rebase from main often, make small focused pull requests, avoid reformatting whole files, and talk when two people touch the same area.

    BeginnerQuick answer1 min read
  8. How do the patterns in a .gitignore file work?

    Each line is a pattern. * matches any characters except a slash, a trailing slash matches directories, ** matches nested directories, ! negates a pattern, and

    BeginnerQuick answer1 min read
  9. How do you cancel a merge that went wrong?

    Run git merge --abort. It restores your branch and working tree to the state before the merge started. It works while the merge is still unresolved and uncommitted.

    BeginnerQuick answer1 min read
  10. How do you change the message or contents of your last commit?

    Use git commit --amend. It replaces the last commit with a new one, letting you edit the message and include newly staged changes. Because it rewrites history, avoid it on commits you have already pushed and shared.

    BeginnerQuick answer1 min read
  11. How do you delete a branch locally and on the remote?

    Delete it locally with git branch -d name (or -D to force), and delete it on the remote with git push origin --delete name. They are separate; removing one does not remove the other.

    BeginnerQuick answer1 min read
  12. How do you find which branches are already merged into main?

    Run git branch --merged main to list branches whose commits are all in main, and git branch --no-merged main for the rest. Merged ones are usually safe to delete.

    BeginnerQuick answer1 min read
  13. How do you list, name and inspect your stashes?

    Save with git stash push -m 'message' so each stash has a label, list them with git stash list, and see their contents with git stash show -p stash@{n}.

    BeginnerQuick answer1 min read
  14. How do you rename a Git branch, including one already pushed?

    Locally use git branch -m old new. For a pushed branch, push the new name, set its upstream, and delete the old name on the remote. Tell teammates, since their copies still track the old name.

    BeginnerQuick answer1 min read
  15. How do you tag a commit that is not the latest one?

    Give the commit hash after the tag name, for example git tag -a v1.0 3f9c2ab -m 'Release 1.0'. Without a hash, Git tags the current HEAD.

    BeginnerQuick answer1 min read
  16. How do you undo a commit that you already pushed to a shared branch?

    Use git revert, which adds a new commit that cancels the bad one. History is not rewritten, so teammates can pull normally. Avoid reset plus force-push on shared branches.

    BeginnerQuick answer1 min read
  17. How do you undo the last commit but keep your changes?

    Run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them unstaged. The commit disappears from the branch while your work stays in the files.

    BeginnerQuick answer1 min read
  18. How do you unstage a file you added by mistake?

    Use git restore --staged file, or the older git reset HEAD file. The file leaves the staging area but your edits stay in the working directory.

    BeginnerQuick answer1 min read
  19. How do you view Git history in a compact, readable way?

    Use git log --oneline for one line per commit, adding --graph --decorate --all to see branches and merges drawn as a tree. Options like -n, --author and --since filter the output.

    BeginnerQuick answer1 min read
  20. What do the <<<<<<<, ======= and >>>>>>> markers in a file mean?

    They are Git's conflict markers. The part between <<<<<<< and ======= is your current branch's version, and the part between ======= and >>>>>>> is the incoming version. You edit the file to the final text and delete all three marker lines.

    BeginnerQuick answer1 min read
  21. What do you need before you can use git bisect?

    A known bad commit, a known good commit earlier in history, and a way to test the bug at each step, either by hand or with a script. A clean working tree also helps.

    BeginnerQuick answer1 min read
  22. What does 'Your branch is ahead of origin/main by 2 commits' mean?

    You have two local commits that are not on the remote yet. Nothing is wrong. Run git push to publish them. If it says behind, the remote has commits you do not have yet.

    BeginnerQuick answer1 min read
  23. What does git cherry-pick do?

    cherry-pick copies the changes of one chosen commit onto your current branch as a new commit. It lets you take a single fix without merging the whole branch it lives on.

    BeginnerQuick answer1 min read
  24. What does git status tell you, and how do you read its output?

    git status reports your current branch, how it compares with its upstream, and three groups of files: staged changes ready to commit, modified but unstaged changes, and untracked files Git does not know yet.

    BeginnerQuick answer1 min read
  25. What is a commit hash, and why is it such a long string?

    A commit hash is a SHA-1 fingerprint of the commit's contents, author, message and parent. It uniquely identifies the commit and changes if anything inside it changes. You can usually use just the first 7 characters.

    BeginnerQuick answer1 min read
  26. What is a fast-forward merge, and when does Git use it?

    A fast-forward merge happens when the target branch has no new commits of its own, so Git just moves its pointer forward to the other branch's tip. No merge commit is created and history stays a straight line.

    BeginnerQuick answer1 min read
  27. What is a merge commit, and how is it different from a normal commit?

    A merge commit has two (or more) parents instead of one. It records the point where two lines of history were joined, and holds the combined result of both branches.

    BeginnerQuick answer1 min read
  28. What is an upstream branch in Git?

    An upstream branch is the remote branch that your local branch is linked to. With it set, plain git pull, git push and git status know where to send and compare changes without extra arguments.

    BeginnerQuick answer1 min read
  29. 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
  30. 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