All Courses
Git Interview Questions

Git plays an important role in various organizations as it is one of the best source code control systems. Today, there are over 40 million Git users worldwide. With features such as task management and bug tracking, Git is an efficient technology used by most industry professionals today. So enroll now with Git Certification Course at eMexo Technologies and start your career with Git. After some discussion and research, we’ve put together the following list of Git interview questions that helps you to crack your interview.

1. What is Git?

We encourage you to try this question by first talking about Git’s architecture, as shown in the figure below. Try to explain the figure by saying:

Git Interview Questions
  • Git is a distributed version control system “DVCS”. You can track the changes made to the file and roll back to the changes you need.
  • This is a distributed architecture that offers many advantages over other version control systems (VCS) such as SVN.
  • One of its main advantages is that it does not rely on a central server to store all versions of the project’s files.
  • Instead, each developer uses a “local repository” to “duplicate” a copy of the repository shown in the figure, making a complete history of the project available on the hard drive.
  • Therefore, if your server fails, you only need one of your teammates’ local Git repositories to recover.
  • There is a central cloud repository where developers can commit their changes and share them with other teammates.

2. What is the difference between Git and GitHub?

  • Git is a version control system used to manage source code history.
  • GitHub, on the other hand, is a cloud-based hosting service used to manage Git repositories.
  • GitHub was designed to improve the management of open source projects.

3. What do you understand by the term ‘Version Control System’?

Version Control System (VCS) Records all changes made to a file or record so that you can recall a particular version later if needed.
This ensures that all team members are working with the latest version of the file.

Git Interview Questions

4. What are the benefits of using Version Control System?

  • With the VCS version control system, all team members are free to work with any file at any time. VCS provides the flexibility to merge all changes into a common version. All versions and variants prior to
  • are neatly packaged in VCS. You can request a version at any time you need, and get instant snapshots of your entire project.
  • Every time you save a new version of your project, VCS asks for a brief description of the changes you’ve made. You can also see what changes have been made to the contents of the file.
  • In this way, you can see what changes were made by whom in your project. Distributed VCSs like the
  • Git provide a complete history of your project for all team members, so you can use your teammates’ local Git repositories even if the central server goes down.

5. What does git clone do?

This command creates a copy (or clone) of an existing Git repository. Usually used to move a copy of a remote repository to a local repository.

6. What are the main differences between Git and SVN?

  • Git is not recommended for handling large files, but SVN can handle multiple projects in the same repository.
  • Git doesn’t have a “commit” that spans multiple branches, but SVN allows you to create folders anywhere in your repository layout.
  • You can’t commit changes in Git, but you can use SVN to create tags as branches or multiple revisions under root tags.

7. Name a few Git commands with their function?

Git config   - Configure your username and email address
Git add      - Add one or more files to the staging area
Git diff     - Shows changes made to the file
Git init     - Initializes an empty Git repository
Git commit   - commits changes to Head, but not to remote repositories

8. Mention the various Git repository hosting functions?

  • Github
  • Gitlab
  • GitEnterprise
  • Bitbucket
  • SourceForge

9. What do you understand about the Git merge conflict?

  • Git merge conflicts are events that occur when Git can’t automatically resolve code differences between two commits.
  • Git can automatically merge changes only if the commits are on different lines or branches.
Git Interview Questions merge-conflict.

10. What is Git Bash?

Git Bash is an application that installs Bash, Git, and some Bash utilities commonly used in Windows operating systems. Git Bash allows you to work with Git items and repositories using a variety of commands.

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

This command returns the tree object representation of the current repository, along with the mode and name of each item, and the SHA-1 value of the blob.

12. Define tagging in Git?

Tagging allows developers to mark all important checkpoints as the project progresses. You can use the tag name instead of the commit ID while the commit is checked out and pushed to the remote repository.

13. Define forking in Git?

A copy of the repository is called a fork. Therefore, you can use a fork to try out changes without worrying about the original project. This process is ideal for suggesting changes to other projects.

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

  • The git pull command pulls new changes or commits from a specific branch in the central repository and updates the target branch in the local repository.
  • The Git fetch is used for the same purpose, but with a slightly different behavior.
  • Running git fetch pulls all new commits from the desired branch and saves them in the new branch of your local repository.
  • If you want these changes to be reflected in the target branch, you need to run git merge after git fetch.
  • The target branch is updated only after the target branch and the fetched branch have been merged. For simplicity, keep in mind the following formula:
Git pull = git fetch + git merge 

15. Define Git stash?

Let’s say you’re a developer and want to switch branches to work on something else. The problem is that I don’t want to commit to incomplete work, so I want to get back to this point later. The solution here is git stash.
Git Stash retrieves the modified tracking file and stores it in a stack of unfinished changes that can be reapplied at any time. You can use Stash Pop to get back to work.

16. What is git stash drop?

The Git stash drop command is used to delete stashed items. By default, the last added stash item is removed. You can also specify a specific item as an argument.
Give an example here.
If you want to remove a particular stash item from the stash item list, you can use the following command:

git stash list: It will display the list of stashed items like:
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert “added file_size”
stash@{2}: WIP on master: 21d80a5 added number to log

If you want to remove an item named stash@{0} use command git stash drop stash@{0}.

17. What is a detached HEAD and what causes this and how to avoid this?

If the HEAD is disconnected, it indicates that the currently checked out repository is not a local branch. This can happen due to the following scenarios:

  • If the branch is a read-only branch and you try to make a commit on that branch, you can say that the commit is a “floating” commit that isn’t connected to any branch. You will be separated.
  • If you check out a tag or a specific commit and then try to recommit, the commit will not be associated with any branch. Now when you try to check out a branch, those new commits will be automatically placed at the top.

To avoid disconnection, = instead of checking out commit / tag, you can create a branch starting from that commit and switch to that newly created branch using the following command:

git checkout -b <<new_branch_name>>

This ensures that a new branch is checkout out and not a commit/tag thereby ensuring that a detached state wouldn’t happen.

18. What is cherry-pick in Git?

Git Cherry-Pick is a command that allows you to select any Git commit by reference and add them to HEAD. Cherry picking is the process of picking a commit from one branch and applying it to another branch. Helps to undo your changes.

19. What do you understand about the Staging area in Git?

Git’s staging area starts tracking and saving changes that occur in your files. These saved changes will be reflected in your .git directory. Staging is an intermediate area that helps you format and review before the commit is complete.

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

This directory consists of shell scripts that become active when you run the appropriate Git command.
For example, Git tries to execute a post-commit script after commit.

21. What is SubGit and why is it used?

SubGit is a tool used to migrate SVN to Git. Convert your SVN repository to Git so you can work on both systems at the same time. Automatically sync SVN with Git.

22. Explain git checkout in Git?

Git checkout allows you to switch between HEADs. It can also be used to restore a historical version of a file. This command works for files, commits, and branches.

23. What is the difference between git rebase and git merge?

Git Rebase moves the feature branch to the master. Git merge keeps history by adding new commits.

24. What is the difference between resetting and reverting?

git reset changes the state of a branch to the previous state by removing all states after the desired commit, while git revert creates a new undo commit and keeps the original commit.

25. How do you configure a Git repository to run code sanity checking tools right before making commits, and preventing them if the test fails?

At First of all, I recommend you to introduce a little about the sanity check.
Sanity or smoke testing determines if it is possible and reasonable to continue the test.
Then, I will explain how to achieve this.
This can be done with a simple script that references the repository’s pre-commit hook. The pre-commit hook is triggered just before a commit, even before you enter a commit message. This script allows you to run other tools such as linters to perform sanity checks on changes pushed to the repository.
Finally, For an example. See below

#!/bin/sh
files=$(git diff –cached –name-only –diff-filter=ACM | grep ‘.go$’)
if [ -z files ]; then
exit 0
fi
unfmtd=$(gofmt -l $files)
if [ -z unfmtd ]; then
exit 0
fi
echo “Some .go files are not fmt’d”
exit 1

This script checks if you need to pass the .go file you want to commit to gofmt, the standard Go source code formatting tool. By terminating with a non-zero status, the script effectively prevents commits from being applied to the repository.