Top Git Interview Questions You Need To Prepare In 2024

Last updated on Feb 29,2024 296.7K Views
Saurabh is a technology enthusiast working as a Research Analyst at Edureka.... Saurabh is a technology enthusiast working as a Research Analyst at Edureka. His areas of interest are - DevOps, Artificial Intelligence, Big Data and...

Top Git Interview Questions You Need To Prepare In 2024

edureka.co

I really liked working with Git. Git plays a vital role in many organizations to achieve DevOps and is a must-know technology. This reason drives me to prepare you for the most frequently asked Git interview questions.

After a lot of research and discussion with many DevOps-certified experts with more than 10 years of experience in their domain and frequently taking interviews, I have collected the set of questions below. 

This Git Interview Questions blog is a part of the parent blog DevOps Interview Questions. It includes all the DevOps Stages. 

In this blog, we have covered around 50 questions and we have divided them into 3 categories –

DevOps Interview Questions and Answers in 2022 | DevOps Training | Edureka

 

Basic Questions

1. What is the difference between Git and SVN?

GitSVN

Git is a Decentralized Version Control tool

SVN is a  Centralized Version Control tool

It belongs to the 3rd generation of Version Control tools

It belongs to the 2nd generation of Version Control tools

Clients can clone entire repositories on their local systems

Version history is stored on a server-side repository

Commits are possible even if offline

Only online commits are allowed

Push/pull operations are faster

Push/pull operations are slower

Works are shared automatically by commit

Nothing is shared automatically

2. What is Git?

I will suggest you attempt this question by first telling about the architecture of git as shown in the below diagram just try to explain the diagram by saying:

 

3. What is a distributed VCS?

4. What is the difference between Git and Github?

Git is a version control system of distributed nature that is used to track changes in source code during software development. It aids in coordinating work among programmers, but it can be used to track changes in any set of files. The main objectives of Git are speed, data integrity, and support for distributed, non-linear workflows.

GitHub is a Git repository hosting service, plus it adds many of its own features. GitHub provides a Web-based graphical interface. It also provides access control and several collaboration features, basic task management tools for every project.

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

6. What language is used in Git?

Instead of just telling the name of the language, you need to tell the reason for using it as well. I will suggest you to answer this by saying:

Git uses ‘C’ language. GIT is fast, and ‘C’ language makes this possible by reducing the overhead of run times associated with high-level languages.

7. Mention the various Git repository hosting functions.

8. What is a commit message?

The command that is used to write a commit message is “git commit -a”.
Now explain about -a flag by saying -a on the command line instructs git to commit the new content of all tracked files that have been modified. Also, mention you can use “git add <file>” before git commit -a if new files need to be committed for the first time.

9. How can you fix a broken commit?

In order to fix any broken commit, use the command “git commit --amend”. When you run this command, you can fix the broken commit message in the editor.

10. What is a repository in Git?

Repository in Git is a place where Git stores all the files. Git can store the files either on the local repository or on the remote repository.

11. How can you create a repository in Git?

This is probably the most frequently asked question and the answer to this is really simple.

To create a repository, create a directory for the project if it does not exist, then run the command “git init”. By running this command .git directory will be created in the project directory.

12. What is ‘bare repository’ in Git?

A “bare” repository in Git contains information about the version control and no working files (no tree) and it doesn’t contain the special .git sub-directory. Instead, it contains all the contents of the .git sub-directory directly in the main directory itself, whereas the working directory consists of :

  1. A .git subdirectory with all the Git related revision history of your repository.
  2. A working tree, or checked out copies of your project files.

13. What is a ‘conflict’ in git?

Git can handle on its own most merges by using its automatic merging features. There arises a conflict when two separate branches have made edits to the same line in a file, or when a file has been deleted in one branch but edited in the other. Conflicts are most likely to happen when working in a team environment.

14. How is git instaweb used?

git instaweb’ is used to automatically direct a web browser and run a webserver with an interface into your local repository.

15. What is git is-tree?

git is-tree’ represents a tree object including the mode and the name of each item and the SHA-1 value of the blob or the tree.

16. Name a few Git commands and explain their usage.

          Below are some basic Git commands:

CommandFunction
git rm [file]deletes the file from your working directory and stages the deletion.
git log list the version history for the current branch.

git show [commit]  

shows the metadata and content changes of the specified commit.

git tag [commitID] 

used to give tags to the specified commit.

git checkout [branch name]

git checkout -b [branch name]

used to switch from one branch to another.

creates a new branch and also switches to it.

Intermediate level Questions

17. How to resolve a conflict in Git?

The following steps will resolve conflict in Git-

  1. Identify the files that have caused the conflict.

  2. Make the necessary changes in the files so that conflict does not arise again.

  3. Add these files by the command git add.

  4. Finally to commit the changed file using the command git commit

18. In Git how do you revert a commit that has already been pushed and made public?

There can be two approaches to tackle this question and make sure that you include both because any of the below options can be used depending on the situation:

git revert <name of bad commit>

 19. What is SubGit?

SubGit is a tool for SVN to Git migration. It can create a writable Git mirror of a local or remote Subversion repository and use both Subversion and Git as long as you like.

Now you can also include some advantages like you can do a fast one-time import from Subversion to Git or use SubGit within Atlassian Bitbucket Server. We can use SubGit to create a bi-directional Git-SVN mirror of an existing Subversion repository. You can push to Git or commit to Subversion as per your convenience. Synchronization will be done by SubGit. 

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

Git pull command pulls new changes or commits from a particular branch from your central repository and updates your target branch in your local repository.

Git fetch is also used for the same purpose but it works in a slightly different way. When you perform a git fetch, it pulls all new commits from the desired branch and stores it in a new branch in your local repository. If you want to reflect these changes in your target branch, git fetch must be followed with a git merge. Your target branch will only be updated after merging the target branch and fetched branch. Just to make it easy for you, remember the equation below:

Git pull = git fetch + git merge 

21. What is ‘staging area’ or ‘index’ in Git?

That before completing the commits, it can be formatted and reviewed in an intermediate area known as ‘Staging Area’ or ‘Index’. From the diagram it is evident that every change is first verified in the staging area I have termed it as “stage file” and then that change is committed to the repository.

22. What work is restored when the deleted branch is recovered?

The files which were stashed and saved in the stash index list will be recovered back. Any untracked files will be lost. Also, it is a good idea to always stage and commit your work or stash them.

If you want to fetch the log references of a particular branch or tag then run the command – “git reflog <ref_name>”.

23. What is git stash?

Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for some time to work on something else. The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later. The answer to this issue is Git stash.

Stashing takes your working directory that is, your modified tracked files and staged changes and saves it on a stack of unfinished changes that you can reapply at any time.

24. What is the function of ‘git stash apply’?

If you want to continue working where you had left your work then ‘git stash apply‘ command is used to bring back the saved changes onto your current working directory.

25. What is the difference between the ‘git diff ’and ‘git status’?

git diff ’ depicts the changes between commits, commit and working tree, etc. whereas ‘git status’ shows you the difference between the working directory and the index, it is helpful in understanding a git more comprehensively. ‘git diff’ is similar to ‘git status’, the only difference is that it shows the differences between various commits and also between the working directory and index. 

26. What is the difference between ‘git remote’ and ‘git clone’?

‘git remote add’ creates an entry in your git config that specifies a name for a particular URL whereas ‘git clone’ creates a new git repository by copying an existing one located at the URL

27. What is git stash drop?

Git ‘stash drop’ command is used to remove the stashed item. It will remove the last added stash item by default, and it can also remove a specific item if you include it as an argument.

Now give an example.

If you want to remove a particular stash item from the list of stashed items you can use the below commands:

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}.

28. How do you find a list of files that have changed in a particular commit?

For this answer instead of just telling the command, explain what exactly this command will do.

To get a list file that has changed in a particular commit use the below command:

git diff-tree -r {hash}

Given the commit hash, this will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.

You can also include the below-mentioned point, although it is totally optional but will help in impressing the interviewer.

The output will also include some extra information, which can be easily suppressed by including two flags:

git diff-tree --no-commit-id --name-only -r {hash}

Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only will only print the file names, instead of their paths.

29. What is the function of ‘git config’?

Git uses your username to associate commits with an identity. The git config command can be used to change your Git configuration, including your username.

Now explain with an example.

Suppose you want to give a username and email id to associate a commit with an identity so that you can know who has made a particular commit. For that I will use:

git config –global user.name “Your Name”This command will add a username.
git config –global user.email “Your E-mail Address”This command will add an email id. 

30. What does a commit object contain?

Commit object contains the following components, you should mention all the three points presented below:

31. Describe the branching strategies you have used.

32. Explain the advantages of forking workflow

33. How will you know in Git if a branch has already been merged into master?

The answer is pretty direct.

To know if a branch has been merged into master or not you can use the below commands:

git branch --merged – It lists the branches that have been merged into the current branch.
git branch --no-merged – It lists the branches that have not been merged.

34. Why is it desirable to create an additional commit rather than amending an existing commit?

There are a couple of reasons for this –

  1. The amend operation destroys the state that was previously saved in a commit. If there is just the commit message being changed then that’s not a problem.  But if the contents are being amended then chances of eliminating something important remains more.
  2. Abusing “git commit- amend” can result in the growth of a small commit and acquire unrelated changes.

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

This directory consists of shell scripts that are activated if you run the corresponding Git commands.  For example, git will try to execute the post-commit script after you have run a commit. 

36. In Git, how would you return a commit that has just been pushed and made open?

One or more commits can be reverted through the use of git revert. This command, in a true sense, creates a new commit with patches that cancel out the changes introduced in specific commits. If in case the commit that needs to be reverted has already been published or changing the repository history is not an option then in such cases, git revert can be used to revert commits. If you run the following command then it will revert the last two commits:

git revert HEAD~2..HEAD

Alternatively, there is always an option to check out the state of a particular commit from the past and commit it anew.

37. How to remove a file from git without removing it from your file system?

One has to be careful during a git add, else you may end up adding files that you didn’t want to commit. However, git rm will remove it from both your staging area (index), as well as your file system (working tree), which may not be what you want.

Instead, use git reset:

git reset filename          # or

echo filename >> .gitingore # add it to .gitignore to avoid re-adding it

This means that git reset <paths> is exactly the opposite of git add <paths>.

38. Can you explain the Gitflow workflow?

To record the history of the project, Gitflow workflow employs two parallel long-running branches – master and develop:

39. Tell me the difference between HEAD, working tree and index, in Git.

40. What is Git fork? What is the difference between fork, branch, and clone?

41. What are the different ways you can refer to a commit?

42. What is the difference between rebasing and merge in Git?

43. Explain the difference between reverting and resetting.

44. What is git cherry-pick?

The command git cherry-pick is normally used to introduce particular commits from one branch within a repository onto a different branch. Another common use is to forward- or back-port commits from a maintenance branch to a development branch. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.

Consider:

git cherry-pick <commit-hash>

45. How do you find a list of files that have changed in a particular commit?

git diff-tree -r {hash}

Given the commit hash, this will list all the files that were changed or added in that commit. The -r flag makes the command list individual files, rather than collapsing them into root directory names only.

The output will also include some extra information, which can be easily suppressed by including a couple of flags:

git diff-tree --no-commit-id --name-only -r {hash}

Here –no-commit-id will suppress the commit hashes from appearing in the output, and –name-only will only print the file names, instead of their paths.

 

Advanced level Questions

46. How do you squash the last N commits into a single commit?

There are two options to squash the last N commits into a single commit include both of the below-mentioned options in your answer

If you want to write the new commit message from scratch use the following command
git reset –soft HEAD~N &&git commit

If you want to start editing the new commit message with a concatenation of the existing commit messages then you need to extract those messages and pass them to Git commit for that I will use
git reset –soft HEAD~N &&git commit –edit -m”$(git log –format=%B –reverse .HEAD@{N})”

47. What is Git bisect? How can you use it to determine the source of a (regression) bug?

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

I will suggest you to first give a small introduction to sanity checking.

Sanity or smoke test determines whether it is possible and reasonable to continue testing.

Now explain how to achieve this.

This can be done with a simple script related to the pre-commit hook of the repository. The pre-commit hook is triggered right before a commit is made, even before you are required to enter a commit message. In this script, one can run other tools, such as linters and perform sanity checks on the changes being committed into the repository.

Finally, give an example, you can refer the below script:

#!/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 to see if any .go file that is about to be committed needs to be passed through the standard Go source code formatting tool gofmt. By exiting with a non-zero status, the script effectively prevents the commit from being applied to the repository.

49. How do you integrate Git with Jenkins?

Step 1. Click on the manage jenkins button on your jenkins dashboard.

Step 2. Click on manage jenkins plugin.

Step 3: In the Plugins Page

  1. Select the GIT Plugin
  2. Click on Install without restart. The plugin will take a few moments to finish downloading depending on your internet connection, and will be installed automatically.
  3. You can also select the option Download now and Install after restart  In which plugin is installed after restart
  4. You will be shown a “No updates available” message if you already have the Git plugin installed.

Step 4: Once the plugins have been installed, go to Manage Jenkins on your Jenkins dashboard. You will see your plugins listed among the rest.

50. What is git reflog?

The ‘reflog’ command keeps a track of every single change made in the references (branches or tags) of a repository and keeps a log history of the branches and tags that were either created locally or checked out. Reference logs such as the commit snapshot of when the branch was created or cloned, checked-out, renamed, or any commits made on the branch are maintained by Git and listed by the ‘reflog’ command.

Note:  The branch will be recoverable from your working directory only if the branch ever existed in your local repository i.e. the branch was either created locally or checked-out from a remote repository in your local repository for Git to store its reference history logs.

This command must be executed in the repository that had the lost branch. If you consider the remote repository situation, then you have to execute the reflog command on the developer’s machine who had the branch.

command: git reflog

51. How to recover a deleted branch using git reflog?

Step 1History logs of all the references

Get a list of all the local recorded history logs for all the references (‘master’, ‘uat’ and ‘prepod’) in this repository.

Step 2: Identify the history stamp

As you can see from the above snapshot, the highlighted commit id: e2225bb along with the HEAD pointer index:4 is the one when ‘preprod’ branch was created from the current HEAD pointer pointing to your latest work.

Step 3Recover

If you want to recover back the ‘preprod‘ branch then use the command  ‘git checkout’ passing the HEAD pointer reference with the index id – 4. This is the pointer reference when ‘preprod’ branch was created long commit id highlighted in the output screenshot.

I have included the frequently asked Git interview questions. If you have more questions in your mind just type it in the comment box below and we will reply you ASAP. Before going for the interview I will suggest you to check out this Git blog series.

If you found this Git Interview Questions relevant, check out the DevOps training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The Edureka DevOps Certification Training course helps learners gain expertise in various DevOps processes and tools such as Puppet, Jenkins, Nagios and GIT for automating multiple steps in SDLC.

Upcoming Batches For DevOps Engineer Masters Program
Course NameDateDetails
DevOps Engineer Masters Program

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES