Top 45 GIT Interview Questions & Answers (2021 Update)

Top 45 GIT Interview Questions & Answers (2021 Update)
Top 45 GIT Interview Questions & Answers (2021 Update)

Introduction

Assume you’ve been called in for an interview at your dream company. After reading some of the interview experiences, you realise that your dream company has an inexplicable affection for Git. With the little time you had, you hurriedly googled top Git interview questions. And now you’re here!

Give a pat yourself on the back because you are at the right place! The entire blog has been divided into three sections: Basic, Intermediate, and Advanced. Whether you are a beginner or an experienced professional, these Git interview questions will undoubtedly help you expand your subject knowledge and excel in your interviews.

GIT Interview Questions

Basic

  1. What is Git?

Ans:- Git is a distributed version control system that allows you to track changes in files and revert to any point in time. It is used to help coordinate the work of multiple people on a project while also tracking progress over time. In other words, it is a tool that aids in managing source code in software development.

  1. What do you mean by the term ‘Distributed Version Control System’?

Ans:- A distributed VCS is one that does not rely on a central server to keep track of a project file and all of its versions. In distributed VCS, each collaborator or developer is given a local copy of the main repository, referred to as a clone.

distributed_Version_Control_System
Source: H-Town Tech

Every collaborator, as shown in the diagram above, keeps a local repository on their local machines. They can commit and update the local repositories without any problems.

A developer can use a pull operation to update his local repository with the latest changes from the central server. They can use the push operation to send their changes from the local repository to the central server.

  1.  What are the benefits of using Git?

Ans:- Git offers a range of benefits. Some of them are:

  • Free & Open Source: Git is an open-source project licenced under the GPL (General Public License). Git is entirely free to use, and because it is open-source, you can modify the source code to suit your needs.
  • Reliable: Because each collaborator has their local repository, lost data can be recovered from any of the local repositories in the event of a system crash. All of your files will be backed up at all times.
  • Branching Capabilities: Branch management in Git is simple and straightforward. Creating, deleting, and merging branches takes only a few seconds. Each change to your codebase is isolated by feature branches.

When a developer needs to start working on something, regardless of the size of the project, they create a new branch. This ensures that the master branch always contains production-quality code.

  • Distributed Way of Development: Git is a distributed system, and because of this, it is easier to trace and locate data if it is lost from the main server.

In this system, the developer receives a repository file that is present on the server. Along with this file, a copy of it is also stored in the developer’s system, which is known as a local repository. As a result, the project’s scalability improves significantly.

  • Supports Non-Linear Development: Git supports rapid branching and merging, as well as tools for visualising and traversing a non-linear development history. A fundamental concept in Git is that a change will be merged more frequently than it is written as it is sent to various reviewers.
  1. What is the purpose of branching in Git?

Ans:- The purpose of branching in Git is to allow you to build your own branch and move between them. It will enable you to return to past work while keeping your current work intact.

  1. List a few popular Git hosting repositories.

Ans:-

  • Github
  • GitLab
  • BitBucket
  • Buddy
  • PikaCode
  • Beanstalk
  1. What is a repository in Git?

Ans:- A repository is a file structure in which git stores all project-specific files. Git can store files on either the local or remote repository.

  1. What is the difference between Git and GitHub?

Ans:- Git is a version control system that allows you to track changes to computer files. The main purpose of Git is to manage projects, or a set of them, as they change over time. It is helpful for tracking progress over time and coordinating work among multiple people on a project.

GitHub is a service that hosts Git repositories and provides a web-based graphical interface. GitHub enables every team member to collaborate on the project from any location, making collaboration simple.

  1. Name a few Git commands and their functions.

Ans:- 

  • git init – Initialize an empty Git repository
  • git config – Configure the username and email address
  • git commit – Commit changes to head but not to the remote repository
  • git add – Add one or more files into the staging area
  • git diff – Displays the changes made into the file
  1. What does ‘git clone’ do?

Ans:- The command makes a copy (or clone) of an already existing git repository. It is generally used to make a copy of the remote repository and save it to the local repository.

  1.  What is the function of ‘git push’ in Git?

Ans:- The ‘git push‘ command is used to update remote refs and associated objects.

  1.  Tell me something about ‘git stash’?

Ans:- ‘git stash’ can be helpful when we need to switch between branches but don’t want to lose changes in the current branch. The ‘git stash’ command pushes the current working directory state and index to the stack for future use, leaving a clean working directory for other tasks.

  1.  What is meant by ‘git stash drop’?

Ans:- When you no longer need a specific stash, use the ‘git stash drop’ command to remove it. It will remove the most recently added stash item by default, but it can also remove a specific item if you include it as an argument.

If you want to remove all the stashes from the repository at once, use the ‘git stash clear’ command.

  1. In Git, how do you initialise a repository?

Ans:- To set up a repository, first create a directory for the project if it does not already exist, and then run the command ‘git init’. By running this command, a .git directory will be created within the project directory, converting your project directory into a Git repository.

  1. What command can you use to create a commit message?

Ans:- ‘git commit –a’ is the command used to create a commit message. The –a flag tells git to commit the new content of all tracked files that have been modified. If you need to commit new files for the first time, use ‘git add <file>’ before ‘git commit –a’.

  1. What is a .git directory?

Ans:- When you create a repository, you will notice a .git directory inside it. This .git directory contains all of the repository’s metadata and keeps track of all changes made to the files in your repository by keeping a commit history.

This folder contains all information about commits, hooks, refs, object databases, remote repository addresses, and so on. This is the most important aspect of Git. This is the directory that is copied when you clone any Git repository on your local machine.

  1.  What is the language used in Git?

Ans:- C is the underlying programming language in which Git is written. The C programming language speeds up Git by avoiding the runtime overheads associated with other high-level programming languages.

Intermediate

  1. In Git, what is a ‘Staging Area’ or an ‘Index’?

Ans:- Before committing to the changes, the developer is given the option to format and review the files, as well as make changes to them. All of this occurs in the common area known as the ‘Index’ or ‘Staging Area.’

Staging_Area
Source: Velog
  1.  What is contained in the commit object?

Ans:- A commit object consists of the following components:

  • A collection of files that represents the state of a project at a particular point in time.
  • A reference to the parent commits objects.
  • A 40 character string known as the SHA-1 name uniquely identifies the commit object.
  1.  How will you know if the branch has already been merged into master?

Ans:- You can find out the branch merge status by running the following commands:

  • git branch –merged master’: This will return a list of all branches that have been renamed master.
  • git branch –merged’: This will return a list of all branches that have been merged into HEAD.
  • git branch –no-merged’: This will return a list of all branches that have not yet been merged.
  1. What is a ‘HEAD’ in git, and how many HEADs can be created in a repository?

Ans:- The HEAD is simply a reference to the branch’s most recent commit object. There will always be a default HEAD referred to as ‘master’ or now ‘main’ (as per GitHub), but there is no limit to the number of HEADs available. In other words, it can have as many HEADs as it wants.

  1. What is a merge conflict in Git?

Ans:- Git typically handles feature merges automatically, but when working in a team environment, there may be instances of conflict, such as:

  • When two distinct branches of a file make changes to the same line
  • A file is deleted in one branch but changed in another.

A merge conflict occurs when Git is unable to resolve differences in code between two commits automatically.

  1. What is the command used to delete a branch?

Ans:- Use the command ‘git branch –d [head]’ to delete a branch.

  1.  What is the function of ‘git log‘?

Ans:- The ‘git log‘ is used to find specific commits in your project history- by author, date, content, or history.

  1. What happens if you delete the .git directory?

Ans:- You will lose track of your project’s history if the .git/ directory is deleted. Also, version control will be removed from the repository.

  1.  What is the functionality of git ls-tree?

Ans:- The command ‘git Is-tree‘ returns a tree object that includes the mode, name, and SHA-1 value of the blob or tree.

  1.  Explain ‘bare repository’ in Git?

Ans:- The ‘Bare Repository’ is used to coordinate with the distributed development and developers team, especially when working on a project from multiple computers. A bare repository is simply a history of your code’s versions.

  1.  What is the purpose of the ‘git checkout‘ command in git?

Ans:- The ‘git checkout‘ command is used to update specified files or directories in your working tree with those from another branch without merging the two branches.

  1.  Distinguish between ‘git fetch’ and ‘git pull’.

Ans:- The ‘git pull’ command pulls fresh changes from the current working branch located in the remote central repository.

The ‘git fetch’ command accomplishes the same thing, but in two steps:

  • Pulls all commits and modifications from the specified branch and puts them in a new local repository branch.
  • The ‘git fetch’ command should be followed by the ‘git merge’ command to reflect changes in the current / target branch.

A ‘git fetch’ does not immediately update your own local branch or working copy under refs/heads, but a ‘git pull’ brings the local branch up to date with its remote version.

Alternatively, git pull = git fetch + git merge.

  1.  What does the commands ‘git reset –mixed’ and ‘git merge –abort’ perform?

Ans:- The command ‘git reset –mixed’ is used to undo changes to the working directory and git index.

The ‘git merge –abort’ command is used to terminate the merge process and return to the state before the merge.

  1.  What is the function of the command ‘git rm‘?

Ans:- The ‘git rm‘ command is used to remove the file from both staging and the disc.

Advanced

  1.  What is the purpose of ‘git instaweb’?

Ans:- The command ‘git instaweb‘ is used to open a web browser and launch a webserver with an interface into your local repository.

  1. What does ‘hooks’ comprise of in Git? 

Ans:- The ‘git cherry-pick’ command allows you to select commits from one branch of a repository and apply them to another. This command is helpful for undoing changes made by mistake to the wrong branch. Then, switch to the appropriate branch and run this command to cherry-pick the commit.

  1. What is the purpose of the ‘git cherry-pick’ command?

Ans:- The ‘git cherry-pick’ command allows you to select commits from one branch of a repository and apply them to another. This command is helpful for undoing changes made by mistake to the wrong branch. Then, switch to the appropriate branch and run this command to cherry-pick the commit.

  1. What is the difference between a ‘git rebase’ and a ‘git merge’?

Ans:- Both the ‘rebase’ and ‘merge’ commands are used to integrate changes from one branch to another, but they do so in slightly different ways.

As shown in the image below, the result of the ‘merge’ will be a combination of commits. It joins the histories of both branches and creates a new ‘merge commit’ in the feature branch.

The ‘rebase’ command, on the other hand, will move the entire feature branch to the beginning of the master branch.

  1.  Explain how a merge can enter a conflicted stage at different moments.

Ans:- A merge can enter a conflicted stage in one of two ways:

  • Starting the Merge Process
    • The merging will fail to start if the working directory of the stage area in the current project has changed.
    • Conflicts arise in this situation due to pending changes that must be stabilised using various Git commands.
  • During the Merge Process
    • The failure during the merge process implies a conflict between the local branch and the merging branch.
    • Git does the best it can in this circumstance, but some things in the conflicted files must be addressed manually.
  1. What is the difference between fork, clone, and branch?

Ans:-

Fork: A fork is essentially a copy of a repository. In most cases, you fork a repository so that you can experiment with modifications without harming the original project. Forks are most typically used to suggest changes to someone else’s project or to utilise someone else’s project as a foundation for your own concept.

Clone: Creating a clone or copying an existing git repository in a new directory is referred to as ‘git clone’. Cloning creates an automatic connection back to the original repository, making it very simple to interact with the central repository.

Branch: Individual projects within a git repository are referred to as git branches. Within a repository, separate branches can have completely distinct files, and folders, or everything can be identical except for a few lines of code in a file.

  1. How can you squash the previous N commits into one?

Ans:- To combine the last N commits into a single commit, you have two options:

  • If you want to start over with a new commit message, use the command:

git reset –soft HEAD~N &&git commit

  • If you wish to start editing the new commit message by concatenating existing commit messages, you’ll need to extract those messages and send them to Git commit. For that, use the command:

git reset –soft HEAD~N &&git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})”

  1. How do you undo a bad commit that has already been pushed?

Ans:- You can revert a commit in two ways:

  • In a new commit, remove or fix the bad file and push it to the remote repository. Then, use the following command to commit it to the remote repository:

git commit –m “commit message”

  • Create a new commit to undo all of the changes made in the previous commit. The following command should be used:

git revert <commit id>

  1. What is the best course of action in the event of a broken commit: create a new commit or amend an existing commit?

Ans:- It is always preferable to create a new commit rather than amend an existing one for the following reasons:

  • Performing an amend operation destroys the previously saved state of that commit. It is acceptable if only the commit message is changed or destroyed, but there may be cases where the contents of the commits are changed. As a result, important information is lost as well.
  • Overuse of ‘git commit –amend’ can have severe consequences because the small commit amend can grow and gather unrelated changes over time.
  1. How can a merge conflict be resolved?

Ans:- To resolve a merge conflict in Git, follow these steps:

  • The simplest way to resolve the conflicted file is to open it and make the necessary changes.
  • After editing the file, we can use the ‘git add a’ command to stage the newly merged content.
  • The last step is to use the ‘git commit’ command to create a new commit.
  • Git will generate a new merge commit to complete the merge.
  1.  What is ‘git stash pop’? What distinguishes it from ‘git stash apply’?

Ans:- Both commands are used to reapply your previously stashed changes and resume work from where you left off.

In the ‘git stash pop’ command, the changes are re-applied to the working copy after being removed from the stash.

In the ‘git stash apply’ command, the changes will be saved in the stash as well as applied to your working copy. When you want to apply the same stashed changes to many branches, you can use this command.

  1. What is the function of the ‘git reflog’ command?

Ans:- The ‘git reflog’ command keeps track of any modification made to the repository references (which can be branches or tags), as well as the history of branches/tags logs that were either created locally or checked out.

Git maintains reference logs, including commit snapshots from when the branch was established or cloned, checked-out, renamed, and any commits done on the branch. The ‘reflog’ command lists these references logs.

  1.  Is it possible to retrieve a Git branch that has been deleted?

Ans:- Yes, it is. You’ll need to know the SHA to recover a deleted branch. Every operation in Git generates a unique ID called SHA or hash.

The SHA is displayed on the terminal when you delete a branch:

deleted branch <your-branch-name> (<sha>)

To retrieve the deleted branch, run the following command:

git checkout -b <sha> your-branch-name>

If you don’t know the SHA of the commit at the branch’s tip, use the git reflog command to find out, then use the checkout command to restore your branch.

  1.  What work is restored if you recover a deleted branch?

Ans:- It is possible to recover the files that were stashed and saved in the stash index. Untracked files will be lost. That’s why staging and committing your work or stashing it is a good idea.

  1. How can you delete a file from Git without deleting it from your local filesystem?

Ans:- For this, you can use the ‘cached’ option:

git rm -rf –cached git rm -rf –cached $FILES

This command will delete the files from your repository but not from your local filesystem.

Frequently Asked Questions

What is Git repository interview questions?

Git is a distributed version control system that allows you to track file changes and roll them back to any point in time. It is one of the interviewers’ favourite topics to play with. The git repository interview questions include all of the critical and frequently asked questions you may encounter in an interview.

What is the difference between Git and SVN?

Git and SVN have the following differences:

First, Git is an open-source distributed version control system developed by Linus Torvalds.
However, SVN is an Apache-licensed open-source software version and revision control system.

Second, Git utilises a Distributed Model. Instead of using a centralised server, Git builds a local repository to keep everything locally.
However, SVN makes use of a centralised server to save changes to the source code.

Third, Git operations do not require network connectivity.
However,Almost all SVN activities require a network.

Fourth, The concept of global revision number does not exist in Git.
However, SVN does contain a global revision number.

What is CI/CD interview questions?

CI/CD stands for Continuous Integration, Continuous Development and Continuous Delivery. These are a collection of DevOps best practices that ensures that code updates are delivered often and reliably. CI/CD interview questions include questions based on CI/CD, their advantages and disadvantages, their relation with DevOps, implementation of CI/CD pipeline etc.

What is SonarQube interview questions?

Sonar is a web-based tool for analysing code quality in Maven-based Java projects. It includes a broad range of code quality checkpoints, such as architecture and design, coding rules, potential bugs, complexity, duplications, unit testing, and so on. As a result, it has become one of the essential interview topics. Its interview questions include its uses, benefits, quality profiles, quality gates, architecture, and role in databases, etc.

Key Takeaways

With this discussion, this blog attempted to deep dive into the most important GIT interview questions. We hope that these GIT interview questions will help you understand the fundamentals of GIT and land a job at your dream company.

We hope you found this blog useful. Feel free to let us know your thoughts in the comments section.