This is another series of articles in the Interviews & Certifications section of our blog- Inviul.Com. The IT industry is moving forward with DevOps and adopting more and more with an automated process. In its way to the DevOps process, Git plays a vital role. Its importance is like maintaining the proper amount of salt in your favorite cuisine. Therefore, I am presenting the top 50+ Git interview questions for you. It will help you in many ways, either as an interviewer or interviewee.
As always, these Git Interview questions are prepared after discussion with top recruiters and interviewers. Also, some questions are taken directly from the experience of a few of the candidates while coming out from the walk-in hiring.
You will find some questions are easier, some are of average level and some are tough, which together constitutes to make the entire Git interview questions blog post. Well, before we start discussing top git interview questions, I have some suggestions for you here:
- Top Java Interview Questions
- List of Cucumber Interview Questions
- TestNG Interview Questions
- Unique 200 Selenium Interview Questions
Do you know?
We can run Appium Tests on Android devices over Wi-Fi. To know more, click here.
Easy Level Git Interview Questions
Let’s start with the easy level of Git Interview Questions.
Question# 1: What is Git?
A: Git is a Distributed Version Control System (DVCS) and Source Code Management System which helps us to track the changes made to the files.
Question# 2: What is a Distributed Version Control System (DVCS)?
A: A repository sharing system which is distributed among the entire team with a cloned copy to the local directory. Every team member pushes changes to the repo with their version of the code.
Question# 3: What is Version Control System (VCS)?
A: It is a tool that tracks the changes in the version of the source code by the team. It maintains the special database to keep the track of every change made.
Question# 4: What are the differences between Git and SVN?
A: Some of the differences are listed below:
Git | SVN |
It is a Distributed Version Control System. | It is a Centralized Version Control System. |
It allows us to clone the repository to a local directory. | It maintains the version history at the server. |
We can commit the changes, even when offline. | It’s mandatory to be connected online to commit changes. |
Push & Pull: Faster | Push & Pull: Slower |
3rd Generation of VCS | 2nd Generation of VCS |
Question# 5: What is the difference between Git & GitHub?
A: Git is a distributed version control system, which helps to make coordination with other developers by keeping a track of all the changes made to the code. It effectively supports the non-linear flow by keeping intact of speed and data integrity.
GitHub is a source code repository, like BitBucket, Stash, etc. It also provided web GUI to track the push & pull. It creates, deletes & updates the branch itself from GUI along with tracking history of all the commits by different users.
Question# 6: What are the advantages of Git as VCS?
A: Some of the advantages of using Git is listed as below:
- Availability
- Highly secure
- Data integrity
- Faster to traverse through branches and commits
- It maintains single directory per repo (.git)
Question# 7: What language is used by Git?
A: C language directly connects with the machine; hence, there are no concerns about overhead at runtime which we can generally observe with High-level language. This ultimately makes Git faster, Thus, C is the language of Git.
Question# 8: How to create a git repository?
A: First we create a directory of the project then we run the command git init. After the successful execution of this command, a .git repository gets created.
Question# 9: How do we clone the repo to a local directory?
A: Read more here about Git commands.
Question# 10: When does conflict arise in git?
A: It happens when we are working in a team and accidentally two team member makes changes to the same piece of code then conflicts occur.
Git has an in-built handler which tries to handle most of the conflict automatically.
Question# 11: What is the command to add new files before commit?
A: We use git add <file_name> command to add new files to the repo.
Question# 12: How do we commit the added files?
A: We use git commit –m “Your Comments” command to commit the added files.
Question# 13: What is the git command to push the committed version?
A: git push.
Question# 14: What is the git command to write a commit message to already tracked files?
A: git commit –a
Here ‘-a’ informs git to add a message to the existing tracked files.
Question# 15: How to fix broken commit with git command?
A: git commit – -amend
Question# 16: How to direct the web browser using the git command?
A: git instaweb command is used to direct a web browser. It runs a web server whose interface is in a local copy of the repository.
Question# 17: How to get tree object of the repo with their SHA-1 value?
A: git is-tree
Question# 19: What is the git command to delete the file from the working directory?
A: git rm <file_name>
Question# 20: How to check logs of the current branch?
A: git log
Question# 21: How to check the changes in content and Metadata of the specified commit?
A: git show <commitId>
Question# 22: How to switch from one branch to another branch?
A: git checkout <branch_name>
Question# 23: How to create a new branch and switching to it?
A: git checkout –b <branch_name>
Question# 24: How to give tags to a commit?
A: git tag <commitId>
Average Level Git Interview Questions
Question# 25: How to revert a commit which is being made public after the push?
A:
First Technique: Go to the branch and remove the files which are impacting other functionality and make a fresh commit without having bad files. This is the simplest and direct approach to this question.
Second Technique: use command git revert <commitId having bad files>. It undoes the changes and brings back your branch to the previous version before your last commit. Now you can make fresh commits.
Question# 26: What is git fetch and how is it different from git pull?
A: git pull command is used to download all the latest changes from the central repository to your local copy. So simply it fetches all the latest changes and merges with your local copy.
git fetch command downloads all the commits from the central repository to your local copy, however, changes are not reflected in the target branch. Hence, git fetch is followed by git merge when you want pulled changes to be reflected in your target branch.
Question# 27: How did you resolve the conflicts?
A: We have taken the following ways to resolve conflicts in the code:
- We informed in the group chat before merging feature branch to master
- We used some advanced merge tools which resolved most of the conflicts automatically
- If the above steps didn’t resolve the problem, then we identified the file which created conflicts then we resolved the issue and then merged it back
Question# 28: How to migrate from SVN to Git?
A: We used a tool called SubGit to migrate data from SVN to Git. It also helps to create a bi-direction connection between these two.
Question# 29: What is the staging area in Git?
A: An intermediate area where formatting and review process takes place before the commit gets publish with commit id.
Question# 30: How to fetch logs & references of the particular branch?
A: git reflog <reference_name>
Question# 31: What is git stash and its related command?
A: Suppose you are currently working in some branch and the work is not yet done; but, you got an urgent request and hence you have to switch to different branch but you do not want to commit half-done work. So in such a case, you can stash your unfinished work by using the command called git stash.
If you want to come back to the stashed area to finish the incomplete task then use command git stash apply.
Question# 32: What is the command to see the list of stashed items?
A: git stash list
Question# 33: How to remove items from the stash’s list?
A: Use command- git stash drop <itemId>
Question# 34: What is the git command to see the changes between different commit and working tree?
A: git diff
Question# 35: How to find the difference between the working directory and its index?
A: git status
Question# 36: How to get a list of files in which changes are made in particular commit?
A: git diff-tree –r {hash}
Or, git diff-tree – -no-commit-id – -name-only –r {hash}
Question# 37: Explain the branching structure of your repository?
A: Feature Branch:
- It has all the changes for the particular feature story
- After fully testing of the feature branch, it gets merged with the master or the default branch
Master Branch:
- All the feature branches are merged here
- It is a kind of parent branch
Task Branch:
- Specified tasks are implemented and stored here
Release Branch:
- This branch is further released in the release cycle
- It contains code from all the master branches (In case of different projects and repo)
- It does not allow any addition of new feature, except documentation, bug fixes, hotfixes, and release dedicated tasks
Question# 38: What are the components of a commit object?
A: There are mainly three components available in a commit object, which are listed as follows:
- A set of committed files
- Any reference to the parent commit
- A 40 character SHA-1 string
Question# 39: How to change the git configuration?
A: Suppose if you want to change the user name then the git command would be: git config –global user.name “Your User Name”
Question# 40: What is a fork?
Question# 41: How to check the list of the merged branch in a particular branch, let’s say, master?
A: First go to the master branch and then run the below commands:
- git branch – -merged (It enlists all the merged branches)
- git branch – -no-merged (It enlists all the non-merged branches)
Question# 42: How to remove a particular file from the git by keeping the same file in your working file system?
A: git reset <file_name>
Or, git reset..
Or, echo <file_name> >> .gitignore
Question# 43: What is HEAD & index in Git?
A: HEAD is the last commit in the current branch, whereas, an index is a staging area where single, large and all the binary files with their SHA-1 data are available.
Question# 44: How to select a particular parent of a commit which is having multiple parents?
A: Suppose there are 4 parents of a commit and you want to select the 3rd one then use ^ character. Example: HEAD^3
Tough Level Git Interview Questions
Question# 45: What is reverse merge?
A: The process of merging the master branch to the feature branch is called reverse merge. It could be defined relatively with two different branches.
Question #46: Define git cherry-pick command?
A: It is a technique to bring particular commit from one branch to another branch by using the command git cherry-pick <commit_id>
Question# 47: How to bring the previous N commits into a sing commit?
A: git reset –soft HEAD~N && git commit
Question# 48: Explain git bisect?
A: It is a technique to identify the commit which introduced a bug in the branch by using a binary search concept. Here is the command: git bisect <secondaryCommand> <option>
Question# 49: How to configure Jenkins with Git?
A: Refer: Guide to Jenkins Continuous Integration
Question# 50: How to integrate GitHub repository?
A: Refer: Guide to configure GitHub repository
Question# 51: How to track every single change made to a particular branch?
A git reflog
Question# 52: What is a pull request?
Question# 53: What is rebasing?
Question# 54: How to recover the deleted branch?
These were one of the most asked Git interview questions. I have left a few questions unanswered, so I request you please answer them in the comment below.