Software Training Institute in Chennai with 100% Placements – SLA Institute
Share on your Social Media

Git Interview Questions and Answers

Published On: January 10, 2025

Introduction

Git is really important for software development these days. It helps teams work on code together, keep track of changes, and collaborate on projects. Many companies are now using development and continuous integration, so they want people who know Git well. Whether you are new to the field and getting ready for your first interview, or you have been working for a while and want to move up, knowing Git is a big plus. This collection of Git Interview Questions and Answers is here to help you feel more confident, learn more about version control, and get ready for interviews. It explains things in a way that’s easy to understand, even if you are just starting with Git. The goal is to help you prepare for interviews and to learn Git concepts. Discover our Git Course Syllabus to begin your version control learning journey.

Git Interview Questions for Freshers

1. What is Git?

Git is a tool that helps developers keep track of changes in their code. It lets multiple people work on the project without messing up each other’s work. Since every developer has a complete copy of the repository, Git supports faster operations and offline access to project history.

2. Can you explain the difference between Git and GitHub?

People often misunderstand the roles of Git and GitHub in software development. They do different things:

  • Git is a version control tool installed on the user’s computer.
  • GitHub offers cloud storage and version control features for Git-based projects.
  • Git helps track code changes locally.
  • GitHub makes it easy to collaborate, share code, and manage projects online.

3. What is a repository in Git?

A repository, often called a repo, is the storage location for your project files and their version history. It has all project data, including commits, branches, and config files. Git stores this info in a hidden folder called. git inside the project folder.

4. Explain the difference between git init and git clone.

The commands git init and git clone both create repositories. They do different things.

  • git init creates an empty Git repository in an existing project folder.
  • git clone copies an existing repository from a server to your computer.
  • Use git init for projects.
  • Use git clone for existing projects.

5. What are the three main areas or states in Git?

Git manages files in three stages:

  • Working Directory – The workspace where changes are made to project files.
  • Staging Area – A temporary area that prepares changes for a commit.
  • Repository – The location where committed changes are permanently stored.

Understanding these stages helps you manage code changes better.

6. What is a Git commit, and how do developers create one?

A commit is a saved snapshot of your project at a specific point in time, making it easy to return to earlier versions when needed.

  • To create a commit, use this command:
    • git commit -m “Your commit message”

A good commit message should focus on what was changed and its intent.

7. What is the purpose of the staging area and git add?

Before committing, Git lets you review and select changes through the staging area. The git add command adds selected files and changes to the staging area.

  • Benefits of the staging area include:
    • Organizing changes before committing.
    • Selecting required files.
    • Creating commit histories.

8. What does git status do?

The git status command shows your repository’s state. It helps developers understand what changes have been made and what actions need to be taken next.

  • It displays:
    • Modified files
    • Untracked files
    • Staged changes
    • Current branch information

This is one of the most commonly used Git commands during development.

9. What is a branch, and why do we use it?

A branch is a line of development in Git. Developers use branches for features, bug fixes, or experiments without affecting the main project.

  • Using branches provides advantages:
    • Safer development
    • Better team collaboration
    • Easier testing
    • Cleaner project management

10. Can you explain how to create and switch to a new Git branch?

  • You can create and switch to a branch with one command:
    • git checkout -b branch-name
  • Alternatively, you can perform the steps separately:
    • git branch branch-name
    • git checkout branch-name

This lets you work on a feature without impacting the main branch.

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

Both commands get updates from a repository, but they work differently.

  • git fetch
    • Downloads the latest changes.
    • Does not merge changes into their current branch.
    • Let the user review updates before applying them.
  • gitpull
    • Downloads changes from the remote repository.
    • Automatically merges them into their active branch.
    • Saves time when user are ready to update their code immediately.

In terms of git pull = git fetch + git merge.

12. What is a merge conflict, and how do you resolve it?

A merge conflict occurs when Git cannot automatically combine changes from different branches. This usually happens when multiple developers edit the same part of a file.

  • To resolve a merge conflict:
    • Open the conflicting file.
    • Locate the conflict markers.
    • Choose the correct code version.
    • Remove the conflict markers.
    • Save the file.
    • Run:
      • git add .
      • git commit

After committing, the merge process is completed.

13. Can you explain git stash and its use cases?

The git stash command saves your changes without creating a commit. It is helpful when fixing bugs, changing branches, or testing new features.

  • Common use cases include:
    • Handling urgent bug fixes.
    • Switching branches temporarily.
    • Testing another feature without committing unfinished code.
  • To restore stashed changes:
    • git stash pop

14. What is the purpose of a .gitignore file?

A.gitignore file tells Git which files and folders to ignore.

  • Common examples include:
    • Log files
    • Temporary files
    • Environment files (.env)
    • Dependency folders like node_modules

Using a file keeps repositories clean and prevents unnecessary files from being uploaded.

15. Can you explain the difference between git reset and git revert?

Both commands change, but they work differently.

  • git reset
    • Moves the branch back to an earlier commit.
    • Can remove commit history.
    • Best used for local repositories before code is shared.
  • git revert
    • Creates a new commit that reverses previous changes.
    • Preserve project history.
    • Safer for shared repositories and team environments.

For team projects, developers prefer git revert because it keeps an accurate history of changes.

Git Interview Questions for Experienced Candidates

1. How does git rebase differ from git merge?

Git rebase and Git merge are used to combine changes from branches. They handle commit history differently.

  • When you use Git merge, it creates a merge commit. This keeps the history of both branches.
  • In contrast, Git rebase moves your branch commits on top of another branch, creating a cleaner and more linear history. Rebase is often preferred when teams want a simpler commit log.

2. When would you use git cherry-pick?

Git cherry-pick helps you copy a commit from one branch and apply it to another. You don’t have to merge the branch.

  • Here are some common use cases:
    • Fixing a bug in a release branch
    • Moving a feature update
    • Recovering a commit
    • Avoiding unnecessary branch merges

This command is useful when you only need one commit.

3. How do you recover a deleted commit in Git?

If you accidentally delete a commit, you can often recover it using Git reflog.

Git reflog records actions in your repository. It helps you find lost commits.

  • To recover a commit:
    • git reflog
  • Then restore the commit using:
    • git cherry-pick <commit-hash>
    • or
    • git checkout <commit-hash>

4. What is “detaching your HEAD”?

A Detached HEAD state happens when you check out a commit. Not a branch.

  • In this state:
    • HEAD points directly to a commit
    • New commits are not linked to a branch
    • Work can be lost if you switch branches without saving changes

To keep your work safe, create a branch before leaving the Detached HEAD state.

5. What is Git Flow?

Git Flow is a way for teams to manage software projects. It uses branches for different tasks.

  • It typically uses:
    • The main branch has production code
    • The develop branch has development
    • Feature branches have features
    • Release branches prepare releases
    • Hotfix branches fix urgent production issues

Git Flow gives larger teams a structured workflow.

6. How does a “Squash and Merge” differ from a standard merge?

A standard merge adds all commits from a feature branch to the target branch.

A Squash and Merge combines all feature branch commits into one commit. Then it merges.

  • Benefits of Squash and Merge:
    • Cleaner commit history
    • project maintenance
    • Less commit clutter
    • Simpler code reviews

Many teams use this approach to keep repositories organized.

7. What is a Git Hook?

Git Hooks are scripts that run automatically. They run when certain Git events happen.

  • For example:
    • Pre-commit hooks run before a commit.
    • Pre-push hooks run before code is pushed.
    • Post-merge hooks run after a merge.

Git Hooks tasks like testing, code formatting, and quality checks.

8. How do you retrieve a single file from a different branch?

Git lets you copy a file from another branch without switching branches.

  • You can use:
    • git checkout <branch-name> — <file-path>
  • Or the newer command:
    • git restore –source=<branch-name> <file-path>

This is useful when only one file needs to be restored or copied.

9. What are Git Submodules?

Git Submodules allow one Git repository to be included inside another repository as a subdirectory.

  • They are commonly used for:
    • Shared libraries.
    • External dependencies.
    • Reusable components.
    • Multi-project development.

Submodules keep project histories separate. Keep projects connected.

10. What is the Git Object Model, and what are its core components?

Git stores data using four object types that form the foundation of its version control system.

  • Core Git Objects
    • Blob: Stores file content.
    • Tree: Represents directories and file structure.
    • Commit: Stores project snapshots and metadata.
    • Tag: Creates a reference to a commit.

Understanding these objects helps you learn how Git manages data.

11. Explain the difference between git merge –no-ff and git merge –squash.

Both commands affect branch history.

  • git merge –no-ff
    • Always creates a merge commit.
    • Preserve feature branch history.
    • Makes branch activity easier to track.
  • git merge –squash
    • Combines all commits into one.
    • Produces a cleaner history.
    • Requires a manual commit after squashing.

The choice depends on how your team manages project history.

12. What is git reflog, and how can you use it to rescue a lost commit or branch?

`git reflog` tracks every change to local branch references.

  • It helps recover:
    • Lost commits
    • Deleted branches
    • Accidental resets
    • Failed rebases

Many developers consider reflog one of Git’s most valuable recovery tools because it helps restore work that appears to be lost.

13. What is Interactive Rebasing, and how do you squash the last 4 commits?

Interactive rebasing lets you edit commit history before sharing code with others.

  • To edit the four commits:
    • Run `git rebase -i HEAD~4.`
  • Using interactive rebase, you can:
    • Reorder commits
    • Rename commit messages
    • Delete unnecessary commits
    • Squash multiple commits into one

This helps create a commit history.

14. How do you manage massive binary files or large assets in a Git repository?

Git is optimized for source code. Large binary files can slow down your repository.

To handle files efficiently, use Git LFS.

  • Benefits of Git LFS:
    • Faster repository cloning.
    • Reduced repository size.
    • Better performance.
    • Storage of large assets.

It’s commonly used for videos, images, and design files.

15. What is git worktree, and what problem does it solve for developers?

The git worktree command allows developers to work on multiple branches simultaneously in separate folders.

When switching branches, you can create another working directory linked to the repository.

  • Example:
    • `git worktree add..folder main.`

This is useful for urgent bug fixes. While continuing development, on another branch. It improves productivity.

Conclusion

In conclusion, understanding advanced Git concepts is essential for developers working on software projects today. Things like rebasing, cherry-picking, Git Hooks, submodules, and worktrees make it easier to work with others, simplify your workflow, and manage your code. By going over these Git Interview Questions and Answers, you can get a better grasp of version control, feel more confident in interviews, and be ready to tackle real-world coding challenges more effectively and professionally. Get the right career guidance from our leading Training and Placement Institute in Chennai.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.