Fun with Learning Technology
LearnCoursesQuestionsTracksToolsNewsExplorePractice
Fun with Learning Technology

A new problem, explained clearly, every day.

Subscribe
Learn
  • Lessons
  • Topics
  • News
  • Tools
  • Courses
  • Career tracks
  • Everything
Site
  • About
  • Contact
  • Support
  • Privacy
  • Terms
Get the daily one

One email per new problem. No spam.

Request a tutorial

Requests shape what gets made next.

© 2026 Fun with Learning TechnologyRSS
Home›Courses›Git & GitHub›Git Interview Questions

Interview Prep

Git Interview Questions

This guide provides a deep dive into common Git technical interview questions by focusing on the underlying mechanics of version control. Mastering these concepts ensures you can troubleshoot complex repository states rather than simply memorizing commands. These insights are essential for candidates aiming to demonstrate expert-level proficiency during technical evaluations.

Understanding the Three-Tree Architecture

A common interview question explores how Git manages data states. The system relies on three distinct trees: the Working Directory, the Staging Area (index), and the Commit History (HEAD). The Working Directory is where files are currently modified. The Staging Area acts as a buffer zone that records planned changes for the next commit. The Commit History stores snapshots of the project. When you run a command, Git moves data between these areas. Understanding this flow is vital because it explains why changes must be explicitly staged before they are saved. If you simply modify files, Git ignores them until they are staged, preventing accidental inclusions. This architecture allows for fine-grained control over what exactly goes into a specific commit, ensuring that only related logical changes are bundled together, which keeps the project history clean and readable for other collaborators who might inspect the logs later.

# Show the staging status of current files
git status

# Add specific files to the staging area
git add app.py

# Create a commit from the staged changes
git commit -m "Refactor login logic"

The Mechanics of Resetting States

Interviewers often ask about the difference between git reset --soft, --mixed, and --hard. These flags dictate how far back in the three-tree architecture a reset operation reaches. A '--soft' reset moves the HEAD pointer to a previous commit but leaves both the Staging Area and the Working Directory intact, effectively keeping your work ready for a new commit. A '--mixed' reset, which is the default, resets the HEAD and the Staging Area but leaves the Working Directory files alone. Finally, a '--hard' reset overwrites everything: HEAD, the Staging Area, and the Working Directory are all reset to the state of the chosen commit. You reach for these commands when you need to undo mistakes or rewrite local history, but you must be cautious: a hard reset permanently deletes uncommitted changes. Mastering this allows you to revert your environment safely without losing work when you realize you are on the wrong track.

# Reset to previous commit, keeping files in working directory
git reset --soft HEAD~1

# Destructive reset: delete all local changes since last commit
git reset --hard HEAD

Branching and Fast-Forward Merges

Branches are simply lightweight, movable pointers to a specific commit. When you create a branch, Git creates a new pointer; when you commit, the pointer moves forward automatically. A 'fast-forward' merge occurs when the current branch has no new commits since the target branch was created. In this scenario, Git simply slides the pointer forward to the target commit, resulting in a linear history. If the history has diverged, Git performs a three-way merge, creating a new 'merge commit' to reconcile the two branches. Understanding this is critical for maintaining a clean project history. If you encounter an interview question about 'why does my history look like a spiderweb,' the answer usually relates to improper merging strategies. Keeping branches updated frequently through rebasing or merging minimizes these complex merge conflicts and makes the commit history easier to follow during debugging sessions.

# Create and switch to a new feature branch
git checkout -b feature-user-auth

# Merge branch into main with a merge commit
git checkout main
git merge --no-ff feature-user-auth

Resolving Conflicts via Rebase

Rebasing is the process of moving or combining a sequence of commits to a new base commit. Instead of creating a merge commit, rebasing rewrites the project history by creating brand new commits for each commit in the original branch. This results in a perfectly linear history, which is often preferred for readability. However, because rebasing changes the commit SHAs, you should never rebase code that has already been pushed to a shared remote repository, as this disrupts the workflow for everyone else. Interviewers look for this nuance: it demonstrates that you understand the collaborative nature of Git. When you encounter a conflict during a rebase, Git stops and asks you to resolve the discrepancies manually before moving to the next commit. This process requires discipline but results in a cleaner, professional-looking log that tells a clear story of your development efforts.

# Rebase current branch on top of main
git checkout feature-work
git rebase main

# After resolving conflicts in files, continue
git add . && git rebase --continue

Detached HEAD States

A 'detached HEAD' state occurs when you checkout a specific commit hash instead of a branch name. In this mode, you are looking at a historical point in the repository rather than the tip of a branch. If you create a commit while in this state, it does not belong to any branch, and if you switch to another branch, those new commits could be lost if you do not attach them to a named branch. This is a common point of confusion for developers, making it a favorite interview topic. To fix this, you should create a new branch while in the detached state to anchor your work. Knowing how to handle this state proves that you understand how Git pointers operate independently of the files themselves. You can use reflogs to recover 'orphaned' commits if you accidentally lose track of them after leaving a detached state.

# Move to a past commit
git checkout a1b2c3d

# Create a branch from this state to save new work
git checkout -b recovered-work

# View lost commits if you made mistakes
git reflog

Key points

  • The Working Directory, Staging Area, and HEAD make up the core three-tree architecture.
  • A hard reset permanently discards all uncommitted changes in your repository.
  • Fast-forward merges maintain a linear history while three-way merges create a merge commit.
  • Rebasing is a powerful tool to clean history but should never be used on shared remote branches.
  • A detached HEAD state occurs when you checkout a specific commit instead of a named branch.
  • The Git reflog tracks every action taken in the repository, acting as a safety net for lost work.
  • Staging changes allows you to bundle logical modifications into a single, cohesive commit.
  • Understanding the difference between resetting and rebasing is crucial for maintaining a healthy repository.

Common mistakes

  • Mistake: Committing sensitive data like API keys to the repository. Why it's wrong: Once committed, the history is exposed and remains in the Git database even if you delete the file later. Fix: Use a .gitignore file and environment variables instead.
  • Mistake: Using 'git push --force' indiscriminately. Why it's wrong: It overwrites remote history, potentially deleting code added by teammates. Fix: Use 'git push --force-with-lease' to ensure you don't overwrite work you haven't fetched yet.
  • Mistake: Merging branches without pulling the latest changes first. Why it's wrong: This often leads to unnecessary merge conflicts or tracking issues. Fix: Always perform 'git pull' or 'git fetch' followed by a 'rebase' before finalizing local merges.
  • Mistake: Staging all changes with 'git add .' without checking. Why it's wrong: It often includes temporary logs, system files, or debug prints that don't belong in the codebase. Fix: Use 'git add -p' to interactively stage specific hunks of code.
  • Mistake: Treating the 'main' branch as a place to develop new features. Why it's wrong: This makes it difficult to maintain a stable production-ready state and causes chaos in team environments. Fix: Always create a descriptive feature branch for every new task.

Interview questions

What is the primary difference between a local Git repository and a remote GitHub repository?

A local Git repository is a folder on your computer that contains the entire history of your project, allowing you to commit, branch, and merge without an internet connection. In contrast, a remote GitHub repository acts as a centralized backup and collaboration platform hosted on the cloud. The primary reason for this distinction is that Git is a distributed version control system, meaning every developer has a full copy of the project history, while GitHub facilitates synchronization, team coordination, and code reviews across multiple machines.

Can you explain the difference between 'git fetch' and 'git pull'?

The 'git fetch' command downloads commits, files, and refs from a remote repository into your local repository's hidden database, but it does not merge them into your working directory or change your current state. 'git pull', however, is essentially a 'git fetch' followed immediately by a 'git merge'. You use 'git fetch' when you want to review changes before integrating them, whereas 'git pull' is used when you want to update your current branch instantly with the remote changes.

What is the purpose of the 'git stash' command, and when would you use it?

The 'git stash' command is used to save your current uncommitted changes—both staged and unstaged—into a temporary storage area, effectively cleaning your working directory to a pristine state. You would use this when you need to switch branches to address a hotfix or urgent bug but aren't ready to commit your current work-in-progress. By running 'git stash pop' later, you can re-apply those changes exactly where you left off, ensuring that your messy development state doesn't interfere with switching contexts.

Compare the 'git merge' and 'git rebase' strategies; when is it better to use one over the other?

A 'git merge' creates a new 'merge commit' that ties two branches together, preserving the exact historical sequence of commits, which is great for transparency and tracing branch development. 'git rebase', conversely, moves or combines a sequence of commits to a new base commit, creating a linear project history that is much cleaner and easier to read. Use 'git merge' on shared public branches to keep history accurate, but use 'git rebase' on local feature branches to keep your personal commit history tidy before pushing to GitHub.

How does 'git reset --soft' differ from 'git reset --hard'?

The 'git reset' command modifies your current branch pointer. Using '--soft' moves the pointer to a specific commit but leaves your staged changes and working directory exactly as they were, which is useful for undoing a commit while keeping your work staged for a new, cleaner commit. Conversely, '--hard' resets the pointer and simultaneously deletes all changes in your working directory and staging area that occurred after that commit. The '--hard' option is destructive and should only be used when you are certain you want to discard recent work entirely.

What is the process of a 'detached HEAD' state, and how do you resolve it?

A 'detached HEAD' state occurs when you checkout a specific commit hash rather than a branch name, which moves the HEAD pointer to a point in history that is not associated with any branch. This is risky because if you commit changes here, they are not tracked by any branch and will be orphaned if you switch away. To resolve this, you should immediately create a new branch using 'git checkout -b <new-branch-name>', which saves your commits to a reachable branch, ensuring your work is not lost when Git performs garbage collection on unreachable objects.

All Git & GitHub interview questions →

Check yourself

1. What is the primary difference between 'git merge' and 'git rebase'?

  • A.Merge creates a new commit, while rebase rewrites the commit history.
  • B.Rebase preserves original commit timestamps, while merge changes them.
  • C.Merge is for local repositories, while rebase is for remote repositories.
  • D.Rebase creates a merge commit, while merge maintains a linear history.
Show answer

A. Merge creates a new commit, while rebase rewrites the commit history.
Merge creates a dedicated merge commit, preserving historical structure. Rebase moves or combines a sequence of commits to a new base, creating a linear project history. The other options describe incorrect functional roles or behavior.

2. If you are in the middle of a complex rebase and encounter conflicts, what is the best way to safely abort the process and return to your original state?

  • A.git rebase --hard
  • B.git rebase --abort
  • C.git reset --hard HEAD
  • D.git checkout main
Show answer

B. git rebase --abort
The 'git rebase --abort' command is designed specifically to revert the branch to its state before the rebase began. The other options are either invalid commands or potentially destructive actions that do not safely clean up the rebase state.

3. When should you use 'git cherry-pick'?

  • A.When you want to permanently delete a range of commits.
  • B.When you want to push specific commits to a new remote branch.
  • C.When you want to apply a specific commit from one branch onto another branch.
  • D.When you want to resolve merge conflicts automatically using the remote version.
Show answer

C. When you want to apply a specific commit from one branch onto another branch.
Cherry-picking allows you to select specific commits from any branch and apply them to your current branch without merging the entire history. The other options describe actions performed by commands like 'rebase', 'push', or 'merge'.

4. What happens when you run 'git reset --soft HEAD~1'?

  • A.It deletes the last commit and all changes made in that commit.
  • B.It removes the last commit from the history, but keeps the changes in the staging area.
  • C.It moves the branch pointer back, but keeps your working directory and staging area unchanged.
  • D.It creates a new commit that reverts the changes introduced by the last commit.
Show answer

B. It removes the last commit from the history, but keeps the changes in the staging area.
The '--soft' flag moves the HEAD pointer back by one commit while leaving the changes from that commit in your staging area. '--hard' would delete changes, and a revert command would create a new inverse commit.

5. Why is it considered best practice to fetch before pushing?

  • A.It initializes the local repository with the latest configuration files.
  • B.It ensures your local repository is aware of any remote changes, preventing non-fast-forward errors.
  • C.It automatically merges the remote branch into your local branch to ensure they are identical.
  • D.It is required by the Git protocol to verify the identity of the remote server.
Show answer

B. It ensures your local repository is aware of any remote changes, preventing non-fast-forward errors.
Fetching syncs your remote tracking branches with the server. If the remote has new commits, pushing without having them locally results in a non-fast-forward error, which this practice avoids. The other options incorrectly describe automation or protocol requirements.

Take the full Git & GitHub quiz →

← PreviousConventional Commits

Git & GitHub

20 lessons, free to read.

All lessons →

Track your progress

Sign in to mark lessons done, score quizzes and keep notes.

Open in the app