Skip to content
Laviyx Learn

Try

Git and GitHub Interview Questions

Frequently asked Git and GitHub interview questions on commits, the staging area, branches, merge and rebase, reset and revert, conflicts, pull requests, workflows, the reflog and Git internals, with hints and answers.

22 questions · Updated

  1. 1.What is the difference between Git and GitHub?

    BeginnerConceptBasics
    Show hint

    One is a tool, the other a website.

    Show solution

    Git is a version control tool that runs on your computer and works offline. GitHub is a website that hosts Git repositories, and adds pull requests, issues and automation. GitLab and Bitbucket are alternatives to GitHub.

  2. 2.What is the staging area, and why does it exist?

    BeginnerConceptBasics
    Show hint

    A draft of the next commit.

    Show solution

    The staging area (or index) holds what will go into the next commit. git add copies changes there, and git commit saves them. It lets you split your work into small, focused commits, even when you have changed several things at once.

  3. 3.What is a commit, and what does its ID mean?

    BeginnerConceptBasics
    Show hint

    How is the ID calculated?

    Show solution

    A commit is a snapshot of the project with an author, date, message and parent commit. Its ID is a hash of all of that, so any change to the content or history gives a different ID.

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

    BeginnerConceptRemotes
    Show hint

    Which one touches your branch?

    Show solution

    git fetch downloads new commits and updates origin/main, but it does not change your branch or files. git pull fetches, then merges or rebases those commits into your current branch.

  5. 5.What is a branch in Git?

    BeginnerConceptBranches
    Show hint

    How big is it on disk?

    Show solution

    A branch is a lightweight pointer to a commit, which moves forward with each new commit. Because it is only a small reference, creating one is instant. Branches let you work on features without affecting main.

  6. 6.How do you resolve a merge conflict?

    BeginnerScenarioBranches
    Show hint

    Markers, then add, then commit.

    Show solution

    Open the conflicted files, choose the right content, and delete the <<<<<<<, ======= and >>>>>>> markers. Run git add on each file, then git commit to finish the merge. git merge --abort cancels it.

  7. 7.You added a file to .gitignore, but Git still tracks it. Why?

    BeginnerScenarioBasics
    Show hint

    Ignore rules only apply to some files.

    Show solution

    .gitignore only applies to untracked files. Stop tracking the file with git rm --cached <file> and commit that. The file stays on disk, but it remains in old commits.

  8. 8.What is the difference between merge and rebase?

    IntermediateConceptHistory
    Show hint

    One keeps history, the other rewrites it.

    Show solution

    merge joins two branches, adding a merge commit if they diverged, and leaves existing commits unchanged. rebase replays your commits on top of another branch, giving a straight history, but it creates new commit IDs. Do not rebase commits that others have used.

  9. 9.What is the difference between git reset and git revert?

    IntermediateConceptUndoing
    Show hint

    Which one is safe on a shared branch?

    Show solution

    git reset moves the branch back, removing commits from it, which rewrites history. git revert adds a new commit that undoes an earlier one, so it is safe on shared branches.

  10. 10.Explain git reset --soft, --mixed and --hard.

    IntermediateConceptUndoing
    Show hint

    Where do the changes end up?

    Show solution

    All three move the branch. --soft leaves the changes staged. --mixed, the default, leaves them in your files but unstaged. --hard discards them completely.

  11. 11.When would you use git stash?

    IntermediateScenarioEveryday tools
    Show hint

    An urgent interruption.

    Show solution

    When you need a clean working tree but your work is not ready to commit, for example to switch branches for an urgent fix. git stash saves the changes, and git stash pop restores them. Add -u to include untracked files.

  12. 12.What is git cherry-pick used for?

    IntermediateConceptEveryday tools
    Show hint

    One commit, not a whole branch.

    Show solution

    It copies a single commit to the current branch as a new commit. It is commonly used to bring a bug fix to a release branch without merging everything else.

  13. 13.How do you combine several commits into one?

    IntermediateScenarioHistory
    Show hint

    Two common ways, plus a GitHub button.

    Show solution

    Use git rebase -i HEAD~n and mark commits as squash or fixup. Or use git reset --soft HEAD~n and commit again. On GitHub, Squash and merge does it when merging a pull request.

  14. 14.Why is --force-with-lease safer than --force?

    IntermediateConceptRemotes
    Show hint

    What if a teammate pushed?

    Show solution

    --force overwrites the remote branch no matter what. --force-with-lease refuses if the remote has commits you have not seen, so it does not silently delete a teammate's work.

  15. 15.Describe the pull request workflow.

    IntermediateScenarioTeamwork
    Show hint

    From branch to delete.

    Show solution

    Pull the latest main, create a branch, commit and push. Open a pull request, let the automated checks run and get a review. Push any fixes to the same branch, merge, delete the branch, and pull main.

  16. 16.Compare GitHub Flow, Git Flow and trunk-based development.

    IntermediateConceptTeamwork
    Show hint

    How often do they release?

    Show solution

    GitHub Flow uses short branches off an always-deployable main. Git Flow adds develop, release and hotfix branches for scheduled releases. Trunk-based development merges into main at least daily, with feature flags hiding unfinished work.

  17. 17.How do you recover a commit lost after git reset --hard?

    AdvancedScenarioRecovery
    Show hint

    Git records where HEAD has been.

    Show solution

    Run git reflog to find the commit, then git reset --hard HEAD@{n} or git branch rescue <id>. The reflog is local and its entries expire, but commits normally stay recoverable for weeks.

  18. 18.How do you find which commit introduced a bug?

    AdvancedScenarioRecovery
    Show hint

    Binary search.

    Show solution

    Use git bisect. Mark a bad commit and a good commit, and Git checks out commits in between, halving the range each time. git bisect run <test command> automates it.

  19. 19.You pushed an API key to a public repository. What do you do?

    AdvancedScenarioRecovery
    Show hint

    What comes first, the key or the history?

    Show solution

    Revoke the key first, because it must be treated as leaked. Then remove it from history with git filter-repo if needed, force-push, and have the team re-clone. Add secret scanning, .gitignore rules and hooks to prevent a repeat.

  20. 20.What objects does Git store?

    AdvancedConceptInternals
    Show hint

    There are four types.

    Show solution

    Blobs hold file contents, trees hold folder listings, commits point to a tree and parents, and annotated tags point to other objects. Each is identified by a hash of its content, so identical files are stored once.

  21. 21.Does Git store differences or snapshots?

    AdvancedConceptInternals
    Show hint

    What does a commit point to?

    Show solution

    Snapshots. A commit points to a full tree, and unchanged files reuse existing blobs. Differences are calculated on demand. Packfiles use delta compression, but only to save storage.

  22. 22.How does a three-way merge decide what to do?

    AdvancedConceptInternals
    Show hint

    Which three commits?

    Show solution

    Git compares both branch tips with their common ancestor. A part changed on one side only takes that change. A part changed differently on both sides is a conflict.

Review the lessons