Git & GitHub Interview
Questions & Answers
🌱Beginner QuestionsQ1–Q10
Git is a free, open-source distributed version control system created by Linus Torvalds in 2005. It tracks changes in source code over time so developers can collaborate, revert mistakes, and maintain a full history of a project.
- Version history: Every change is saved — you can go back to any point in time.
- Collaboration: Multiple developers can work on the same codebase simultaneously without overwriting each other's work.
- Branching: Work on new features in isolation, then merge them when ready.
- Distributed: Every developer has a full copy of the repository — no single point of failure.
This is one of the most common confusion points for beginners. They are not the same thing:
| Git | GitHub |
|---|---|
| A version control tool installed on your computer | A cloud platform to host Git repositories |
| Works offline — runs locally | Requires internet — it's a website |
| Command-line tool | Has a web UI, pull requests, issues, Actions |
| Created by Linus Torvalds (2005) | Founded 2008, owned by Microsoft since 2018 |
| Free and open-source | Free for public repos, paid plans for teams |
Think of Git as the engine and GitHub as the garage where you park and share your car. GitLab and Bitbucket are alternative garages.
A Git repository (or "repo") is a folder that Git is tracking. It contains all your project files plus a hidden .git folder that stores the entire version history.
- Local repository: Lives on your own machine. You work here — create files, make commits, create branches. Has no internet requirement.
- Remote repository: Hosted on a server (GitHub, GitLab, Bitbucket). Acts as the shared source of truth for teams. You
pushyour local changes to it andpullothers' changes from it.
Understanding Git's three-stage model is fundamental. Every file in a Git project moves through these states:
- Working Directory (Modified): You've edited the file but haven't told Git about it yet. Git sees it as changed but not staged.
- Staging Area (Staged / Index): You've run
git add. The file is marked to be included in the next commit. Think of it as a "draft commit". - Repository (Committed): You've run
git commit. The change is permanently saved in Git's history.
git add exists. Being able to explain the staging area clearly shows you understand Git's design.A branch is a lightweight, movable pointer to a specific commit. Branches let you work on different things in parallel without affecting the main codebase.
The default branch is called main (previously master). When you create a new branch, you're essentially creating a safe copy to experiment in.
Common branching strategies: feature branches (one branch per feature), Git Flow (main, develop, feature, release, hotfix), trunk-based development (short-lived branches off main).
git clone vs git fork?git clone: A Git command that copies a remote repository to your local machine. You get all files, branches, and history. Your clone is connected to the original via theoriginremote.- Fork (GitHub concept): Creates a copy of someone else's repository on GitHub under your own account. You can make changes freely without affecting the original. Used for contributing to open-source projects via pull requests.
A commit is a snapshot of your project at a specific point in time. It records what changed, who changed it, and when. Every commit has a unique SHA hash (like a3f8c2d) as its identifier.
What makes a good commit message:
- Use the imperative mood: "Add login page" not "Added login page"
- Keep the subject line under 50 characters
- Explain what and why, not how
- Use a body (separated by blank line) for complex changes
.gitignore and why is it important?A .gitignore file tells Git which files and folders to intentionally ignore — they won't be tracked, staged, or committed.
Commonly ignored files include:
- Dependencies:
node_modules/,vendor/ - Environment files:
.env,.env.local(contain secrets!) - Build outputs:
dist/,build/,*.pyc - OS files:
.DS_Store(Mac),Thumbs.db(Windows) - Editor settings:
.vscode/,.idea/
.env files — they often contain API keys, database passwords, and secrets. Once pushed to a public repo, they can be scraped by bots within seconds.A Pull Request (PR) is a GitHub feature (not a Git command) that lets you notify teammates that you've finished a feature branch and want to merge it into the main branch. It creates a formal review process.
- You push your branch to GitHub and open a PR
- Teammates review the code, leave comments, request changes
- You address feedback and push more commits to the same branch
- Once approved, the PR is merged into the target branch
- The feature branch is typically deleted after merging
⚡Intermediate QuestionsQ11–Q21
git merge and git rebase?Both integrate changes from one branch into another, but they do so differently and produce different histories:
| git merge | git rebase |
|---|---|
| Creates a merge commit joining two branch tips | Moves (replays) commits onto the tip of another branch |
| Preserves full history — shows when branches diverged | Creates a cleaner, linear history |
| Non-destructive — original commits unchanged | Rewrites commit history — dangerous on shared branches |
| Good for shared/public branches | Good for cleaning up local feature branches |
A merge conflict happens when two branches modify the same part of the same file and Git can't automatically decide which version to keep. Git pauses the merge and asks you to resolve it manually.
Steps to resolve:
- Open the conflicted file — Git marks conflicts with
<<<,===,>>> - Edit the file to keep what you want (delete the markers)
- Stage the resolved file:
git add <file> - Complete the merge:
git commit
git stash and when would you use it?git stash temporarily shelves (stashes) your uncommitted changes so you can switch to another task, then come back and re-apply them later. It's like putting your work in a drawer.
git fetch and git pull?git fetch: Downloads changes from the remote repository but does NOT merge them into your current branch. Your local files stay untouched. You can review what changed before integrating.git pull: Doesfetch+mergein one step. Downloads changes AND immediately merges them into your current branch.
git fetch + review + git merge on important branches. Use git pull for quick daily syncs on your own feature branches.git reset and what are its three modes?git reset moves the current branch pointer (HEAD) back to a previous commit. The three modes control what happens to your working directory and staging area:
git reset --hard on commits that have already been pushed to a shared remote. Use git revert instead to safely undo public commits.git revert and how is it different from git reset?git revert: Creates a new commit that undoes the changes of a previous commit. The original commit stays in history. Safe to use on shared/public branches.git reset: Moves the branch pointer backwards, effectively removing commits from history. Rewrites history. Dangerous on shared branches.
main or has been pushed — use revert. If it's only in your local unpushed history — reset is fine.git cherry-pick?git cherry-pick applies the changes from a specific commit onto your current branch — without merging the entire branch. It "picks" only what you need.
Common use case: A critical bug fix was committed to a feature branch. You need that fix on main immediately, but the full feature isn't ready to merge. Cherry-pick grabs just the fix commit.
Tags are permanent markers pointing to a specific commit — typically used to mark release versions like v1.0.0, v2.3.1. Unlike branches, tags do not move forward as new commits are added.
| Branch | Tag |
|---|---|
| Moves forward with each new commit | Fixed — never moves |
| Temporary — deleted after merging | Permanent release marker |
| For ongoing work | For versioned releases |
git log and how do you use it effectively?git log shows the commit history. With the right flags it becomes a powerful investigative tool for understanding what changed, when, and by whom.
GitHub Actions is GitHub's built-in CI/CD (Continuous Integration / Continuous Deployment) platform. It lets you automate workflows triggered by events in your repository — like pushing code, opening a PR, or creating a release.
- CI: Automatically run tests every time code is pushed
- CD: Automatically deploy to production when merged to main
- Automation: Auto-label issues, send notifications, generate changelogs
git diff and git status?git status: Shows which files have changed — a summary. Tells you if files are untracked, modified, or staged.git diff: Shows the exact line-by-line changes inside files.
🔥Advanced QuestionsQ22–Q30
Interactive rebase (git rebase -i) lets you rewrite your local commit history before sharing it. You can squash multiple commits into one, reorder commits, edit commit messages, or drop commits entirely.
git bisect and when would you use it?git bisect helps you find the exact commit that introduced a bug by using a binary search through your commit history. You tell Git which commit was good and which is bad, and it checks out commits in the middle until it pinpoints the culprit.
If you have 1,000 commits to check, bisect finds the bad one in just ~10 steps (log₂ 1000 ≈ 10).
git reflog and how can it save you?git reflog records every movement of HEAD — including resets, rebases, and branch switches. It's Git's safety net. Even if you delete a branch or do a hard reset, reflog lets you find and recover lost commits.
git log. If anything goes wrong, reflog will get you back.A Git submodule is a repository embedded inside another repository. It lets you include an external project (like a shared library) as a dependency at a fixed commit, while keeping its history separate.
Submodules are powerful but complex. Many teams prefer package managers (npm, pip) for dependency management, reserving submodules for cases where you need to track a specific commit of an internal shared library.
Git Flow is a branching strategy for managing releases. It defines a strict branching model with specific roles for each branch:
main— production-ready code only. Every commit here is a release.develop— integration branch. Features merge here first.feature/*— one branch per new feature. Branches offdevelop.release/*— preparation for a new release. Bug fixes only.hotfix/*— emergency fixes applied directly tomain.
A force push (git push --force) overwrites the remote branch history with your local history. It is dangerous because it permanently destroys commits on the remote that teammates may have based their work on.
- Only use force push on your own private feature branches after an interactive rebase
- Never force push to
main,develop, or any shared branch - Always prefer
--force-with-leaseover--force - Many teams protect main branches on GitHub to prevent force pushes entirely
git blame and when is it useful?git blame shows you who last modified each line of a file and in which commit. Despite the name, it's not about blame — it's about understanding context: who wrote this code, when, and why.
Use cases: Investigating when a bug was introduced. Understanding why a specific implementation decision was made (pair with git show <commit> to read the full context). Finding who to ask about a piece of code.
Squashing combines multiple commits into a single commit. This cleans up messy WIP commit history before merging a feature branch into main — making the project history easier to read and navigate.
SSH (Secure Shell) authentication uses a key pair (public + private) instead of username/password. Once set up, you never type credentials again. It's faster, more secure, and required on many enterprise setups.
| SSH | HTTPS |
|---|---|
| No password needed after setup | Requires token/password each push |
| More secure (key-based auth) | Works through all firewalls |
| Preferred for daily development | Easier for one-off clones |
Up Next: Python Interview Questions
You've covered Git & GitHub. Next in the series is Python — covering data types, OOP, decorators, generators, and more. Coming soon to RankWeb3.
← JavaScript Q&A