Mastering Git: The Most Common Git Interview Questions and Answers

Table of Contents

Git is one of the most widely used version control systems, and knowing how to use it efficiently is essential for developers. If you’re preparing for a job interview, expect Git-related questions to come up, whether you’re a beginner or an experienced professional. In this detailed guide, we’ll explore the most commonly asked Git interview questions along with in-depth explanations and accurate answers.

Introduction to Git

What is Git?

Git is a distributed version control system (DVCS) that allows multiple developers to collaborate on a project by tracking changes in files. Unlike traditional version control systems, Git enables decentralized development, making it possible to work offline and merge changes seamlessly.

What is the Difference Between Git and GitHub?

Many beginners confuse Git with GitHub, but they serve different purposes:

  • Git: A version control system that runs locally on a developer’s computer.
  • GitHub: A cloud-based hosting platform that allows developers to store, share, and collaborate on Git repositories.

Git Basics

How to Check the Status of a Git Repository?

To check the status of files in your repository, use:

Bash
git status


// Note - Selected File format is Bash, not ideal but it's ok 

This command provides information on modified, staged, and untracked files.

How to Initialize a New Git Repository?

To create a new Git repository in a project directory, use:

Bash
git init

This creates a .git directory where Git stores version control information.

How to Clone a Remote Repository?

To copy a remote repository to your local machine, use:

Bash
git clone <repository-url>

This command downloads the entire repository and history.

Branching and Merging

How to Create and Switch to a New Branch?

To create a new branch and switch to it, use:

Bash
git checkout -b <branch-name>

Alternatively, you can create and switch separately:

Bash
git branch <branch-name>
 git checkout <branch-name>

How to Merge Branches?

Switch to the branch you want to merge changes into, then run:

Bash
git commit -m "Descriptive commit message"

If there are conflicts, Git will prompt you to resolve them before completing the merge.

What is a Merge Conflict and How to Resolve It?

A merge conflict occurs when two branches modify the same part of a file differently. To resolve:

  1. Open the conflicted file.
  2. Manually edit the conflicting sections.
  3. Run:
Bash
git add <file> git commit -m "Resolved merge conflict"

Committing Changes

How to Add Files to the Staging Area?

To stage specific files:

Bash
git add <file-name>

To stage all changes:

Bash
git add .

How to Commit Changes?

To save staged changes:

Bash
git commit -m "Descriptive commit message"

How to Undo the Last Commit?

  • Keep changes but remove commit:
Bash
git reset --soft HEAD~1
  • Remove commit and changes:
Bash
git reset --hard HEAD~1

Remote Collaboration

How to Push Changes to a Remote Repository?

Bash
git push origin <branch-name>

How to Pull the Latest Changes from a Remote Repository?

Bash
git pull origin <branch-name>

How to Create a Pull Request?

  1. Push changes to GitHub:
Bash
git push origin <branch-name>
  1. Go to GitHub, open the repository, and click “New Pull Request.”
  2. Select branches and submit the PR.

How to Delete a Branch?

  • Delete a local branch:
Bash
git branch -d <branch-name>
  • Delete a remote branch:
Bash
git push origin --delete <branch-name>

Advanced Git

Difference Between git merge and git rebase

  • git merge: Combines two branches and keeps the commit history.
  • git rebase: Moves commits from one branch on top of another, creating a linear history.

How to Stash Changes in Git?

To temporarily save changes without committing:

Bash
git stash

To reapply stashed changes:

Bash
git stash apply

To remove a stash:

Bash
git stash drop

How to Revert a Commit Without Deleting History?

Bash
git revert <commit-hash>

This creates a new commit that undoes the previous changes.

How to View Commit History?

Bash
git log

Bonus

What is .gitignore?

.gitignore file specifies files and directories that should be ignored by Git. 

Bash
node_modules/
*.log
.env

What is git cherry-pick?

This command applies a specific commit from one branch to another:

Bash
git cherry-pick <commit-hash>

What is git bisect?

A tool to find a commit that introduced a bug:

Bash
git bisect start
 git bisect bad
 git bisect good <commit-hash>

How to Change the Last Commit Message?

Bash
git commit --amend -m "New commit message"

What is git reflog Used For?

Tracks all changes in HEAD, even undone ones:

Bash
git reflog

Conclusion

Mastering Git is essential for every developer. Whether you’re preparing for an interview or improving your workflow, understanding Git commands and best practices will make you a more efficient and reliable team member. The best way to solidify your Git knowledge is through hands-on practice, so try these commands in a real repository to gain confidence..!

By following this guide, you’ll be well-prepared to answer Git interview questions and showcase your version control expertise.

Skill Up: Software & AI Updates!

Receive our latest insights and updates directly to your inbox

Related Posts

error: Content is protected !!