Top Git Interview Questions and Answers

CTA

Git plays a major role in various organizations as it is one of the best source code management system. There are more than 40 million Git users in the world today. With features such as task management and bug tracking, Git is an efficient technology used by most industry experts today. After conducting several discussions and research, we have compiled the following list of Git interview questions:

The Git Interview Questions blog is categorized into the following parts:

Basic Git Interview Questions for Freshers

Intermediate Git Interview Questions

Advanced Git Interview Questions for Experienced

GitHub Interview Questions

Git Commands Interview Questions

Git Salary Trends

Git Job Trends

Git Roles & Responsibilities

Conclusion

 Did You Know? 

  • Git was created by Linus Torvalds, the same person who invented Linux.
  • The “add” command in git was a mistake; actually, it was meant to be called “stage,” but accidentally typed “add.”
  • Do you know that 93% of engineers use Git, and it is a fundamental tool for them?

Watch this Git Commands Tutorial Video

Basic Git Interview Questions for Freshers

1. What is the process for creating a repository in Git?

If we want to create a repository in Git, then we need to run the command “git init”. With this command .git repository, we can create a directory in the project directory.

2. What is origin in Git?

Origin refers to the remote repository that a project was originally cloned from and is used instead of the original repository’s URL. This allows for easier referencing.

3. What is the git push command?

The git push command is applied for uploading content to a remote repository from a local repository. Pushing can overwrite changes, so it should be used with caution.

4. What is the git pull command?

The git pull command is for fetching and downloading content from a remote repository and integrating it with a local repository.

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

In git rebase, a feature branch is moved into a master. Git merge maintains the history by adding a new commit.

6. Why do we need branching in GIT?

With the help of branching, we can have our own branch and we can also jump between various branches. We can go to our previous work, at the same time keeping our recent work intact.

7. What is the difference between Git and GitHub?

Git is a version control system that is used in the management of the source code history. GitHub, on the other hand, is a cloud-based hosting service that is used in the management of Git repositories. GitHub is designed to help in the better management of open-source projects.

8. State a way to create a new branch in Git.

If we want to create a new feature into the main branch, then we can use the command ‘git merge’ or ‘git pull’.

9. What is the purpose of the .gitignore file?

The purpose of .gitignore files is to ensure that certain files that are not tracked by Git should remain untracked.

10. How do you clone a specific branch from a Git repository?

To clone a specific branch from a repository, you can use git clone -b <branch> <repository-url>.

11. How do you rename a local Git branch?

To rename a local git branch, you can use git branch -m <old name> <new-name>. If already on the branch that has to be renamed, write git branch -m <new-name>

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

To find the list of files that have changed in a particular commit, you can use git show –name-only <commit-hash>.

13. How do you remove a file from the staging area in Git?

To remove a file from the staging area, you can execute git reset HEAD <file>

14. What are the advantages of using GIT?

Here are some of the most important advantages of Git:

  • Data redundancy and data replication is possible
  • It is a highly available service
  • For one repository we can have only one directory of Git
  • The network performance and disk utilization are excellent
  • It is very easy to collaborate on any project
  • We can work on any sort of project within the Git

15. What is the difference between git init and git clone?

 git init command is used to create a new repository locally, whereas the git clone command copies an existing repository from some other location.

16. What is the purpose of a .gitkeep file?

gitkeep is not a git feature but is a convention to keep an empty folder in the repository.

17. What is the use of the git diff command?

The git diff command helps the users see, compare, and understand the changes in the project.

18. What is Git Bash?

Git Bash is an application that installs Bash, Git, and a few Bash utilities that are commonly used on a Windows OS. In Git Bash, interaction is possible with Git elements and the repository through different commands.

19. What is the purpose of the git clean command?

The git clean command removes the untracked files from the working directory.

20. What is tagging in Git?

Tagging allows developers to mark all the important checkpoints through the course of their projects’ progress. Instead of commit IDs, tag names can be used while commits are checked out and pushed to a remote repo.

21. What is forking in Git?

A repository copy is called a fork. So, forking allows one to experiment with changes without worrying about the original project. This process is ideal for proposing changes to someone else’s projects.

22. What is the meaning of “Index” or “Staging Area” in GIT?

When we are making the commits, we can make changes to it, format it and review it in the intermediate area known as ‘Staging Area’ or ‘Index’.

Staging Area

Certification in Cloud & Devops

23. What language is used in GIT?

C is the programming language that is used for creating Git which ensures that the overheads are reduced.

You can even check out our DevOps Interview Questions.

24. How do you create a copy of a local branch?

To create a copy of a local branch, you can use the command git checkout -b <new-branch-name> <existing-branch-name>.

Intermediate Git Interview Questions

25. How to resolve a conflict in Git?

If we want to resolve a conflict in Git, then we need to edit the files for fixing the conflicting changes and then we can run “git add” to add the resolved files and after that we can run the ‘git commit’ for committing the repaired merge.

26. What is a Git repository? Name some popular Git hosting services.

Repositories contain a batch of files that are different versions of a project. These files are imported from the repositories into the local servers of users for further modifications and updates in the content. 

A few popular Git hosting services are:

  • GitHub
  • GitLab
  • Bitbucket
  • SourceForge

The GIT Cheat Sheet by Intellipaat comes in handy while studying GIT.

27. What are Git and GitHub?

Git is an open-source and free distributed version control system developed to handle projects of all sizes quickly and efficiently.

GitHub uses Git to provide Internet hosting for version control and software development. It offers the functionality of distributed version control and source code management, which is found in Git, in addition to other unique features.

Check out our blog to learn more about Commands in Git!

28. What is cherry-pick in Git?

Git cherry-pick is a command that allows the picking of arbitrary Git commits by reference and adding them to the HEAD. Cherry-picking is the process of picking a commit from one branch and applying it to another. It helps in undoing changes.

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

Git fetch retrieves new data from a remote repository but does not integrate it into our working files. It helps in checking if any changes happened in the remote repository. It does not manipulate or destroy anything in the process.

Git pull, on the other hand, updates the HEAD with the latest changes from the remote server and directly integrates it into the working copy files. Using git pull can end in merge conflict as it tries to merge remote changes with the local ones.

30. Explain git checkout in Git.

Git checkout allows for the switching of the HEAD. It can be used to restore the historic versions of files as well. The command operates upon files, commits, and branches.

31. What does git rebase do?

Rebasing is the reapplying of commits on top of another base trip. A sequence of commits is applied from distinct branches into the final commit. It is a linear process of merging and an alternative to the git merge command. Rebasing makes it seem like one has created a branch from a different commit.

32. What is revert in Git?

The git revert command is a forward-moving undo operation. It is a safe way to undo changes as it will create a new commit that inverses the changes instead of deleting or orphaning commits in the commit history.

33. What is the difference between resetting and reverting?

While git reset changes the state of the branch to a previous one by removing all of the states after the desired commit, git revert does it through the creation of new reverting commits and keeping the original one intact.

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

Git remote adds a reference to a remote repository for further tracking. Git clone, like its name, clones an existing remote repository and builds a new one.

35. What is GIT stash?

The Git stash will take the working directory in the current state and index it to put on the stack at a later stage so that what we get is a clean working directory.

Git Stash

This means that if we are in the middle of some task and need to get a clean working directory and simultaneously we want to keep all our current edits, then we can use the Git stash.

36. How are fork, branch, and clone different from each other?

Forking creates a copy of the original repository, and it remains in the GitHub account. Whereas, in cloning, the repository is copied to the local machine using Git. Forking is used to propose changes to the repository owners. In cloning, the changes are directly pushed to the original repository, provided the user has write access. A branch occurs within a repository and is a way to keep developing and modifying the software without affecting the main project.

37. What is the difference between git reflog and log?

The git log is a public record of the commit history for a branch. Reflog, on the other hand, is a private one of the repository’s local commits.

Unlike reflog, the git log is a part of the Git repository and is replicated after a push, fetch, or pull. A developer cannot access a local repository’s reflog without having access to the computer where it is located.

38. What is GIT stash drop?

When we are finished with working on the stashed item or want to remove the list, we can use the Git stash drop. This will ensure that the item that is last added by default or any particular item can be removed from the argument.

39. How to identify if a certain branch has been merged into master?

Git branch –merged master – shows all branches that are merged into master

Git branch – merged – shows all branches that are merged into the head

Git branch – no-merged –shows all the branches that are not merged

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

Criteria Git SVN
Type of version control Distributed Centralized
Access to networks Not mandatory Mandatory
Global revision number Not available Available
Content Cryptographic SHA-1 Hash No hashed content

Here we list some of the most important differences between Git and SVN:

  • When it comes to handling large files, Git is not preferred but SVN can handle multiple projects in the same repository
  • Git does not have ‘commits’ across multiple branches but SVN lets you create the folders on any location in the repository layout
  • You cannot commit changes in Git but SVN lets you create a tag as a branch and you can create multiple revisions under a root tag

41. What are the benefits of using a pull request in a project?

Teams can work on different parts of the system at the same time and then merge their changes together using the pull requests in a project. Therefore, this boosts team efficiency.

42. What is a 'detached HEAD' state in Git?

A detached HEAD state occurs when the HEAD does not point to a branch but rather points toward a specific commit or the remote repository.

43. What is a tracking branch in Git?

Tracking branches are the local branches that are connected to a remote branch and have a direct relationship with them, allowing you to push or pull changes to remote branches easily.

44. What is a Git bundle?

A  Git bundle is a file containing the entire history of a Git repository. This includes all the branches, tags, and commits. You can create a git bundle using the following command:

git bundle create <bundle_file> <refs> 

The <bundle_file> argument is the name of the file that the bundle will be saved to. The <refs> argument specifies which branches, tags, or commits are to be included in the bundle.

45. What is the purpose of the git show command?

git show command is used to show one or more objects. This can be blobs, trees, tags, and commits.

46. What is the use of a Git clone?

The Git clone command lets us copy the existing Git repository. If we want to get a copy of the central repository then the best way to do it is using ‘cloning’.

Git Clone

47. What is a version control system? Mention its types.

A version control system (VCS) is a software tool used to create different project versions and store them in a repository. All modifications to the code are recorded and tracked by the VCS.

Types of version control systems: 

  • Local version control systems have a database that maintains all the file changes on disk under revision control in a special format.
  • Centralized version control systems contain one repository, and each user gets their own working copy.
  • Distributed version control systems contain multiple repositories, each accessible to separate users with their own working copy.

48. What are the advantages of Git over SVN?

Since Git is an open source version control system it lets us run multiple versions of our project so that it shows the changes that are made to the code over time and if needed we can keep track of the changes that we have made. This means that a large number of developers can make their own changes and upload those changes so that the changes can be attributed to the particular developers.

Get 100% Hike!

Master Most in Demand Skills Now !

49. What are some strategies for managing large binary files in Git?

To manage large binary files in Git effectively, one can consider these strategies:

  1. i) Git Large File Storage (LFS)
  2. ii) Separate Large File Repository

iii) Selective Cloning and Fetching

  1. iv) Binary File Compression
  2. v) Optimize Storage Usage
  3. vi) Use External Storage Services
    vii) Archive Old Files

50. What is the use of the git tag -a command?

The git tag -a command is used to create an annotated tag in git. Annotated tags include the tagger name, email, and date, along with a tagging message.

Syntax: git tag -a <tagname> -m “<tagmessage>” [<commit>]

git tag is the base command that is used to create tags in git.

-a stands for “annotated” and instructs git to create an annotated tag.

tagname: This is the name that you want to give to your tag.

-mmessage”: This is optional; you can include a message with “-m to describe the tag.

Advanced Git Interview Questions for Experienced

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

The ‘Git config’ is a great way to configure our options for the Git installation. Using this command, we can define the repository behavior, preferences and user information.

Become a Cloud and DevOps Architect

52. What are the constituents of the commit object contain?

  • the state of a project at a given point of time is contained in a set of files
  • Parent object commit references
  • A 40-character string that uniquely identifies the commit object called a SHAI name

53. What is HEAD in Git, and how many HEADs can be created in a repository?

The reference to a commit object is called the HEAD. Every repository has a ‘Master’ which is the default head. There can be multiple heads in a repository.

Git Head

Git Branching

54. What does a git pull origin master do?

Git pull origin master pulls the master branch into your current branch from the remote called origin. It only affects your current branch, not your local master branch.

55. Throw some light on the git worktree.

git-worktree is a command used to manage multiple working trees attached to the same repository.

Commands:

add <path> [<commit-ish>]: Create a worktree at <path> and checkout <commit-ish> into it.

list: List the details of each worktree.The main worktree is listed first, followed by each of the linked worktrees.

move: Move a worktree to a new location. The main worktree or linked worktrees containing submodules cannot be moved with this command. 

remove: Remove a worktree. Only clean worktrees (no untracked files and modifications in tracked files) can be removed.

repair: Repair worktree administrative files, if possible, if they have become corrupted or outdated due to external factors.

56. How would you remove all untracked files in Git?

There are multiple ways to remove all untracked files in Git.

  1. i) Discard all untracked files: git clean -f command will permanently remove all the untracked files from your working directory.
  2. ii) Remove specific untracked files: Use git clean -n to list all the untracked files without removing them, then identify the specific files you want to remove. Now, use git rm <filename> to remove the individual untracked files.

57. What is the difference between git log and reflog?

Both git log and git reflog are used to keep track of the history of your repository, but they serve different purposes.

git log focuses on the public record of the commit history of the repository, whereas the git reflog focuses on private, local records of all references that have been updated in the repository.

58. When would you use the git stash command?

 git stash temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and work on them later. Below are some key scenarios when you might use it.

  • When switching branches or tasks
  • Sharing code or working with others
  • Dealing with conflicts or errors

59. What is the difference between git merge --no-ff and git merge --squash?

git merge –no-ff will create a commit for the merge even if it could happen with fast forward, whereas git merge –squash will combine all the commits of the feature branch into one commit during the merge.

60. Explain the use of git rebase --interactive. How can it be used to clean up a commit history?

git rebase -i allows interactively rewriting a series of commits by changing commit order, squashing commits together, splitting commits, editing messages, removing commits, etc. This can clean up history by consolidating small commits, rewording messages, and pruning unnecessary changes.

Some ways git rebase –interactive can be used to clean up a commit history are as follows:

  • Squash multiple small or repetitive commits into a single, larger commit to make the history more concise and readable. 
  • Edit/reword commit messages that are unclear, incorrectly worded, or have spelling/grammar issues to improve readability of history.

61. How can you revert a git rebase?

There are a few ways to revert or undo a git rebase:

i) Reset to the original branch tip commit:

git reflog

# find the commit hash before the rebase

git reset --hard <commit-hash>

The reflog shows your previous HEAD commits going back for a few months. Find the commit hash from before the rebase and reset back to it. This completely erases all commits made since the rebase.

ii) New revert commit

git revert -m 1 <rebase-head-commit>

This creates a new commit that undoes all the changes introduced in the rebase head commit. Preserves all previous commits.

9) What is the significance of the git reflog command, and how can it be used to recover lost commits?

Ans: The git reflog command is used to show a log of all the references recorded in the local repository by Git. This includes commits, resets, rebases, merges, etc. The main significance of git reflog is that it allows us to recover lost commits.

Git reflog can recover lost commits:

  • Git reflog shows when that branch tip commit was at HEAD when you accidentally deleted your feature branch.

62. What are Git hooks? Give examples of how they can be used to enforce code quality checks before commits or pushes.

Git hooks are scripts that run automatically when certain Git events occur, such as committing, pushing, merging, etc. They can be used to enforce project workflows and policies around the Git commit/push process.

63. What is the purpose of git worktree, and how does it differ from using branches?

 The git worktree command allows you to have multiple working directories tied to the same repository. Each working tree acts like a complete, isolated checkout, useful for working on different branches or tasks in parallel.
With Git branches, you can switch context to different commits easily but have just a single view of files in the working directory.

64. How do you define a ‘conflict’ in git?

If we want to merge a commit there is a change in one place and the same change already exists then while merging the Git will not be able to predict which is the change that needs to be taken precedence.

65. What is the regular way for branching in GIT?

The best way to create a branch in GIT is to have one ‘main’ branch and then create another branch for implementing the changes that we want to make. This is extremely useful when there are a large number of developers working on a single project.

66. What are submodules in Git, and how do they work? Provide an example of when you would use them.

 Git submodules allow you to embed external Git repositories inside a parent Git repository at a specific path. Some key properties of submodules:

  • Submodules clone a separate Git repo that has its own commit history, tags, etc. They act like nested repositories.
  • Parent repo records a fixed commit for the submodule. Updating the submodule commits requires an explicit update in parent.
  • Useful for modular components of a project that have separate release cycles or authors.
  • Changes in the submodule repo are not automatically reflected in the parent repo that uses it.

A good example using submodules:

  • A large project relies on a common shared library called common-utils.
  • The utils library has a separate dev team that manages releases at its own pace.
  • The main app can embed common-utils as a submodule placed inside src/utils.
  • When common-utils has a new feature or fix, the main app repo can periodically absorb those by updating the recorded submodule commit.

Here, submodules allow modularizing a large codebase into standalone pieces that can evolve independently.

Github Interview Questions

67. What is a clone on GitHub?

 Cloning in a git repository means you can create a local copy of the code provided by the developer. You can perform cloning with CLI.

68. What do you know about GitHub and its repository?

GitHub is a code-hosting platform for version control and collaboration. A repository is one of the elements of GitHub where you can store your code and files. A repository can have multiple collaborators and can be either public or private.

69. Can you tell us a few benefits of using GitHub over other platforms?

Used by more than 4 million organizations and with more than 100 million developers, GitHub is one of the most popular platforms. Below are some of the benefits of using GitHub:

i) Easy to use

ii) Robust documentation and support

iii) Collaboration

70. How can users enhance the functionality of a branch in Git?

Users can enhance the functionality of a branch in Git by adding the desired feature to any of the branches using the Git merge command. Since there is no limit on adding features to a branch, any branch can have any number of features.

Git Commands Interview Questions

71. How do you initialize a new Git repository?

To start keeping track of the changes, in your project using Git begin by opening the folder where your project is located on your computer. Next access the command line. Enter the command “git init”. This instructs Git to begin monitoring this folder. As a result Git will create a folder named “.git” within your project directory. This special folder stores data, about any modifications you make over time enabling Git to maintain a record of your projects history.

72. What is the difference between “git fetch” and “git pull” ?

When you use the “git fetch” command it downloads any changes, from the repository without merging them into your branch. This enables you to review the changes before merging them. On the hand when you use “git pull ” it not fetches changes from the remote repository but also automatically merges them into your current branch incorporating the latest remote changes immediately.

73. What is a branch in Git and how do you create one?

Git branches allow parallel development. Create a new branch with “git branch <name>” to build features or fix bugs separately. Or use “git checkout -b <name>” to make and switch to the new branch in one step, keeping that work isolated from the main codebase.

74. Describe the process of resolving a merge conflict in Git.

When you encounter a merge conflict you will need to make edits, to the files in order to resolve the conflicting changes. After that you should use “git add” to stage these changes and then commit them with “git commit“. This process allows you to carefully oversee how changes, from branches are integrated ensuring that your projects codebase remains intact.

75. How do you revert a Git repository to a previous commit?

To revert any changes you can make use of the “git reset” command. This command allows you to move the branch to a commit affecting both the staging area and working directory depending on the chosen mode (mixed or hard). Alternatively you can also utilize the “git revert” command which generates a commit, to changes made in a previous commit. This method is especially useful when dealing with changes that have already been pushed to shared repositories.

Git Salary Trends 

A DevOps Engineer is a role that bridges the areas of information technology (IT) and software engineering to streamline a product’s software development life cycle. 

According to Glassdoor, the average salary for a DevOps Engineer in India is ₹8,70,000 per annum, ranging from 5L to 12L/yr in India. 

According to the information presented on Glassdoor, the average salary for a DevOps Engineer in the United States is $108,857 per annum, ranging from $178K to $267K/yr.

Git Job Trends 

DevOps bridges the dev-ops gap, speeding up software delivery through collaboration and automation. According to the information presented on DevOps.com, a recent market survey states that the DevOps market will exceed $20 billion by 2026, growing at a CAGR of 24.7% from 2019 to 2026. 

According to LinkedIn, there are currently 25000+ DevOps job openings in India alone, with 8000+ jobs in Bangalore itself. Moreover, according to reports provided by Naukri, there are currently 19,909 DevOps job openings in January 2024.

Talking about the job trends of DevOps engineers in the United States currently, there are 17,000+ job openings according to LinkedIn.

Git Roles and Responsibilities

DevOps Engineers are responsible for overseeing the software development process from start to finish. This involves implementing continuous integration and delivery (CI/CD) pipelines that allow for faster and more frequent releases of software.

One should have the below-mentioned skills to be hired as a DevOps Engineer in Mphasis.

  • A thorough understanding of system administration is a must and specific experience with Linux and Windows is required.
  • Strong hands-on experience in the following skills: AWS (SNS, SQS, IAM, VPC, RDS, Kinesis, KMS), Terragrunt and Terraform, Azure DevOps (pipeline), Splunk, Docker, Kubernetes, etc.

One should have the below-mentioned skills to be hired as a DevOps Engineer at Healthifyme.

  • Requirements: 

Background in Linux/Unix Administration with Exp in AWS requires hands-on experience with at least 3 of RDS, EC2, ELB, EBS, S3, SQS, or CloudWatch.

Hands-on experience in managing web servers (Apache, Nginx, Lighttpd, and Tomcat) with experience in Python and Nodejs will be good.

Conclusion

We hope this set of Git interview questions will help you prepare for your interviews. All the best!

Enroll today in our comprehensive Advanced Certification in DevOps & Cloud Computing or join Intellipaat’s DevOps Certification Course Training to start your career or enhance your skills in the field of DevOps and get certified today.

If you’re eager to explore additional Git interview questions in depth, feel free to join Intellipaat’s vibrant DevOps Community and get answers to your queries.

Course Schedule

Name Date Details
AWS Certification 23 Mar 2024(Sat-Sun) Weekend Batch
View Details
AWS Certification 30 Mar 2024(Sat-Sun) Weekend Batch
View Details
AWS Certification 06 Apr 2024(Sat-Sun) Weekend Batch
View Details