60+ Git Interview Questions You Need To Prepare In 2020

In this post, we have listed popular Git Interview Questions and answers that might help you prepare for and crack any Git interview successfully.
Git plays an important role in an organization to achieve DevOps. These interview questions on Git along with answers will help you improve your knowledge and excel at your work and interview as well.

Most Popular GIT Interview Questions With Answers And Examples

1. What is Git?

Git is one of the Distributed Version Control System (DVCS). It allows us to track changes in an application, or in a folder, or in a single file over time across different users, and different computers. Git takes a snapshot of all files whenever we create a new commit. Every committer on a project always has a copy (local repository) of the whole repository on their machine. So, we can commit to the Git file system also in offline mode. It is an open-source project which is compatible with many operating systems and IDEs.

2. What is the best approach to handle multiple versions of the same source code file?

Different versions of the same source code can be handled using a Version Control System.

3. What is Version Control System?

Version Control System

Version Control System is a category of software tools that help software teams to work together on the same project and allow them to manage changes to a file or set of data over time.

It maintains all the edits and historic versions of the project and allows teams to recall specific versions in later stages.

It ensures that all the team members are working on the latest version.

4. What are the advantages of the Version Control System?

1. It is useful in Team Collaboration. Each team member from a different/same location
can contribute to a single project to achieve objectives.
2. It helps in integrating automation code into a CI/CD pipeline.
3. It helps in early testing of a particular feature by checking out a feature branch

5. What is a Distributed Version Control System?

Distributed Version Control System

In the Distributed Version Control System (DVCS), every developer get a copy (Clone) of the central repository on the local system hard drive. Developers commit and update their local repository without any issues. They can update their local one with new data from the central repo using pull operation and they can change the central repository from the local repo using push operation.

6. What is the difference between Git & GitHub?

GitGitHub
It is a distributed version-control system for tracking changes in source code during software development.It provides hosting for software development and version control using Git.
It is a software.It is a service.
It is installed locally on a system.It is hosted in the cloud.
It is a high quality version control system.It is a cloud based hosting service.
It is a command-line tool.It comes with an easy to use intuitive UI.
First released in 2005.Launched in the year 2018.
Free and open source.Free and pay for use.
It focused on version control and code sharing.It focused on centralized source code hosting.

7. What is a Git repository?

Git repository is a place where all the Git files are stored either on the local repository or on the remote/central repository. It allows us to save versions of our code which we can access whenever needed. 

8. What tasks Git performs when a commit is created in Git?

Git creates 3 objects – a snapshot object, a commit object, file objects, and stores them in the local Git database.

9. How is SHA-1 hash used in Git?

The SHA-1 hash is used to uniquely identify the stored objects in the local Git database.

10. What are the states of a file in Git?

There are three different states of a file in Git- Modified, Staged, and Committed

11. What is the purpose of the command git init?

git init command initializes a new Git project and creates a local .git folder in the project directory.

12. What happens after creating a commit?

By creating a commit, snapshots of staged files are added to the repository, and the state of the project is saved.

13. Why is the content of a commit message important in Git?

Commit message helps in a better understanding of changes made to the source code. It is a certain piece of documentation in any project.

14. Is it necessary to push the commits to a server to conclude Git workflow?

No, it is not necessary to push the commits to the server.

15. What tasks Git performs when the git fetch command is hit followed by git merge?

All the newly added remote branches will be fetched from the remote repo to the local repo and the changes from the local repository will then be merged into the currently active branch.

16. Does Git resolve conflicts automatically?

No, Git does not resolve conflicts automatically.

17. Is git pull possible if there are uncommitted changes in a file in the working directory?

git pull command updates the local repo from remote. If there are open changes in the working directory, git pull will fail. So, it is always good to commit changes before pulling new commits.

18. What does git reflog with config HEAD show?

The reference log of HEAD is logged which means logs of all the changes made to branch pointer HEAD.

19. What do you mean by a branch in git?

It is a movable pointer and it points to the latest commit among a series of commits.

20. What is the purpose of a HEAD pointer?

With a HEAD pointer, Git becomes aware of the active branch to work on.

21. What do you understand by a fast-forward merge in Git?

With a fast-forward merge, the HEAD of the active target branch is moved forward to the latest commit of the source branch which is yet to be merged. The condition for this to happen is that the source branch should not diverge from the active branch.

22. When does a branch diverge?

A branch diverges from another branch once an extra and different commit is included in the chain.

23. What do you know about the term Diamond Shape in git?

An additional merge commit is created whenever two diverged branches are merged in git and further causes the commit history of the target branch to look like a diamond shape for that specific merge.

24. How is rebasing different from merging?

Rebasing can not be considered as a replacement for merging. It basically updates a branch onto the state of another branch.

25. How to rewrite the commit history of a branch?

Rewriting a commit history of a branch can be done in many ways. git commit — amend and git rebase commands can be used to do this.

26. Whose shortcut is the option -i?

-i option is the shortcut for –interactive

27. What can -i do ?

The -i option is used while rebasing to edit, squash, remove, reorder, and merge commits.

28. Does -i used once a branch is rebased?

No, -i is used while rebasing.

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

Both git merge and git rebase integrate changes from one branch into another branch.

Git Merge:

Assume you have two branches such as master branch, feature branch.

git merge keeps the commit history of the feature branch as it is.

To merge the master branch into the feature branch

git checkout feature
git merge master

or with a single line

git merge feature master

Git Rebase:

git rebase applies all feature branch changes on top of the master branch by creating a new commit for each of its previous commit messages. Rebase command changes the commit history and generates commits again on top of the master branch.

The below command is used to rebase the feature branch on top of the master branch

The following command can be used to rebase the feature branch on top of the master branch.

git checkout feature
git rebase -i master

“-i” option used for interactive rebase.

We can simply use

git rebase master

30. What should be kept in mind while changing branch history?

The commit history of a publicly shared branch should never be rewritten.

31. How to resolve conflicts?

We resolve for each file. Either accept the remote version or the local version using the git checkout command. A tool called a merge tool can also be used to resolve conflicts.

32. What are ‘git remote’ and ‘git clone’?

git remote: It is used to create, view, and delete connections to other repositories. It is used to refer to a remote repository or central repository.

git clone: It is used to create a copy or clone of the target repository. It is used to target a different already existing repository.

33. What do you understand by a detached HEAD?

Detached HEAD is a state when the HEAD pointer points to a specific commit and not to a branch.

34. What is the role of Git in cherry-picking?

The cherry-picking feature in Git lets you select a specific commit and apply it from one branch to another. In this process, a new commit is created by Git with a new hash and with the same content and applied to the target branch.

35. Can several commits be picked at a time with cherry-picking?

Yes, several commits can be picked at one time.

36. What does Resetting hard do?

It is used to reset the HEAD in the local repository to a particular commit and also in the working directory and staging area.

37. Which is the default reset option when nothing is provided?

–mixed is the default reset option

38. What are ‘git reset –mixed’ and ‘git merge –abort’ commands do?

‘git reset –mixed’ is used to undo changes made in the working directory and staging area.

‘git merge –abort’ is used to stop the merging process and return back to the state before the merging process.

39. Is there any hidden feature in Git by which conflicts are resolved automatically?

Yes, the git rerere command is used to resolve conflicts automatically.

40. Why is reverting used in Git?

As the name suggests, Reverting is used to revert a particular commit and apply a new commit with corrected changes.

41. How you revert a commit command that has already been pushed and made public?

We can follow two processes to revert a commit.

1. By creating a new commit to undo all the changes made to a bad commit and use the following command

git revert <commit id>

2. Remove or modify the bad file in a new commit and push it to the remote repository.

git commit –m “commit message”

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

Git diff:

Git diff is a multi-use Git command and it is used to show the changes between commits, commit, and working tree, etc.,

The git diff command is often used along with git status and git log to analyze the current state of a Git repo.

Git status:

Git status shows the state of the working directory and staging area.

‘git diff’ is similar to ‘git status’ but the only difference is that it shows the difference between various commits and also between the working directory and index.

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

43. Name some Git hosting repository services

  • GitHub
  • GitLab
  • GitEnterprise
  • SourceForge
  • Microsoft VSTS
  • BitBucket
  • LaunchPad
  • Perforce
  • Beanstalk
  • Assembla
  • Pikacode
  • Beanstalk
  • FogBugz
  • Surround SCM
  • Buddy

44. Mention some Git operations you have used?

Git operations I have used are

  • Initialize
  • Add
  • Commit
  • Push
  • Pull
  • Branching
  • Merging
  • Rebasing

45. What is the difference between git checkout [branch name] and git checkout -b [branch name]?

git checkout [branch name] command switches from one branch to another branch

git checkout –b [branch name] command creates a new branch and also switch to it.

46. How do you remove a file from Git without removing it from your local file system?

By using the ‘cached’ option

The below command removes the files from the Git without removing those files from our local disk.

git rm -rf -cached $FILES

47. Which language is used in Git?

Git is written in C programming language. It makes Git faster by reducing runtime overheads contained with high-level languages.

48. How will you fix a Broken Commit?

We use the ‘git commit –amend’ command to fix a broken commit or to change the last commit.

This command combine staged changes with the previous commit as an alternative to creating an entirely new commit. It replaces the recent commit with the amended commit.

49. Mention some Git commands with their function.

Git config: To configure a username and email address
Git add: To add one or more files to the staging area
Git diff: To view the changes made to a file
Git init: To initialize an empty Git repository
Git commit: To commit changes to head but not to the remote repository.

50. How do you initialize a Git repository?

To create a new repo, we have to use the following command

git init

It is a one time command we use during the initial setup of a new repo.

First, we need to create a directory for the project so that a .git directory will be created inside our project directory and our project directory will turn into a Git repository.

51. What is a .git Directory?

.git folder contains all the information that is necessary for your project and all the information about commits, hooks, refs, object databases, remote repository addresses, etc., It maintains a log to track all the changes made to the files in the repository.

52. What is a commit message in Git?

Commit command in Git is used to record the progress in the local repository. It executes after the files to be committed have been added to the staging area using the git add command ie., ‘git commit –m’

53. What is the git push command.

Git Push

Git push command is used to push the files of a local repository to a remote repository. Once a developer modifies the local repository they execute a push command to share those changes with remote team members.

54. What is the git pull command.

Git Pull

Git pull command is a combination of ‘git fetch’ followed by ‘git merge’. It is used to fetch and merge changes from the remote repository to the local repository.

55. What is Git Stash?

GIT stash captures the present state of the working directory and index it and keeps it on the stack at a later stage. It returns a clean working directory.

Assume, you are working on a project and things become messy and you want to switch branches for some time to work on something else and don’t want to commit or undo your code. Stash helps you here. You can simply use the git stash command in git.

56. What is the Git Stash drop?

Git Stash drop in git is used to remove a specific stash item from the list of stashed items.
You can remove a specific stash by executing the following command.

git stash drop <stash_id>

If you want to remove all the stashes at a time from the repository then you can run the following command

git stash clear

For example:

If you want to see a complete list of stashed items use the following command

git stash list

It will display the stashed items list as follows

stash@{0}: WIP on master: a52e112 added the index file
stash@{1}: WIP on master: b473693 Revert “added file_size”
stash@{2}: WIP on master: 44e45v2 added number to log

To remove an item named stash@{0} then we use the following command

git stash drop stash@{0}

57. What should be kept in mind while writing a commit message?

Commit message should inform what is the change introduced in this particular commit and how it will impact the code as it is crucial to track the changes made and also an important step in code documentation.

58. What is the main purpose of GitHub flow?

The main principle of GitHub flow is that the same name branch is regularly pushed from local to the remote repository to get a fast response from the Continuous Integration server.

59. What is the main purpose of the Git Repository Manager?

A Git Repository Manager is used when there is the collaborative development of software.

60. How do you recover a branch which was deleted by you and has already pushed the changes to the central git repository. Assume there are no other git repos and none of your team members had a local copy.

Firstly I check out the reflog for the latest commit to this branch and then check it out as a new branch.

61. What do you know about self-hosted Git service?

A self-hosted Git service is nothing but a Git Repository Manager that is hosted on its own server.

62. What is the use of git instaweb?

Instaweb is a script used to set up a temporary instance of GitWeb on a webserver to browse your local working Git repository in a web browser.

git instaweb allows you to browse your working Git repository in a gitweb.

63. What is SubGit?

SubGit is a software tool used to migrate from SVN to Git. To migrate your repository from SVN to Git, you must install SubBit on your Git Server.

64. What are some alternatives to SubGit?

Some of the SubGit alternatives are as follows

  • SmartGit
  • GitKraken
  • TortoiseGit
  • git-cola
  • Tower
  • gitg

In conclusion:

Here I am going to conclude this post. If you have any other questions in mind, please comment below and we will answer them and include in this post. Don’t miss to refer to this list of Git Interview Questions before going to an interview. Also, I highly recommend you to go through this DevOps Interview Questions list.

Related Posts:

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment

Share via
Copy link
Powered by Social Snap