Top 30 Git Interview Questions and Answers

By | November 10, 2021

Git is an open-source and widely utilized distributed version control system (DVCS) for managing the development of all kinds of projects in an efficient and tidy manner. It helps you to track the changes in your project and allows you to revert to previous changes when needed. It is also one of the best tools to manage the flow of a team project. It provides various methods to manage users and assign them different roles in a project.

Git is best suitable when several individuals are working as a team on a project. It is not only suitable for monitoring project modifications but also facilitates efficient coordination and collaboration among team members.

In this article, we will discuss some of the most crucial interview questions related to Git that you can come across during an interview for a developer or a DevOps engineer role. However, let us talk about some important terms before moving on to popular Git interview questions. 

What is Version Control?

To learn Git better, you need to understand the concept of version control. The code in a project is constantly evolving as new code is added.

In addition, many developers may add code to a file at the same time and even remove some files on which other users might be working. A VCS assists in dealing with these by keeping track of what modifications have occurred in your project. Version Control also has features like branches, conflicts, and merges, which we will be discussing in a later section of the article.

What is Git?

Git is software that tracks changes in any of your projects. It is commonly used to coordinate work among programmers who are collaboratively generating source code during software development. Its objectives include data integrity, speed, and support for distributed and a tree-like workflow in which we have several branches along with the main branch. In other words, it resembles a tree data structure and performs operations similar to it. To be more specific, Git is a distributed version control system.

Distributed Version Control Systems

Git has a remote repository on a server and a local repository on each developer’s PC. This implies that the code is not simply kept on a central server, but a copy of the code is present on all the PCs of developers. Developers are free to make changes locally in the code and make a pull request to change the central code. The changes made by an individual user do not affect the central code unless the admin of the project accepts the pull request. Pull requests are simply the requests made by the developers to merge their individual changes.

Vamware

Top Git Interview Questions

1. What is version control?

Answer: A version control system is a software that aids in tracking the changes made to files.

When developing code for a large project, you need something that can store different versions of code files so that when you want to return to a previous state, you may do it with ease. This may be accomplished by utilizing a version control system like Git.

By making use of a version control system, you can develop projects faster, improve productivity, ensure effective coordination among team members, and manage projects effectively. Also, such a system will help to reduce the possibility of mistakes and disputes during project development by tracking even the smallest change in the project files.

2. What is a repository?

Answer: Repositories are collections of various versions of files of a project. These files are imported from the repository into the user’s local machine so that developers can modify them. 

The .git extension file within a project is a Git repository. This repository records all modifications to files in your project, creating a history over time.

Two types of repositories are:

Bare repositories:

These repositories are used to disseminate modifications made by various developers. A user may not change this repository or create a new version based on the modifications made.

Non-bare repositories:

Because these repositories are user-friendly, users may add new revisions to files as well as create new versions of the repositories. If no parameters are provided during the clone operation, the cloning procedure produces a non-bare repository by default.

3. What is the cloning of a repository?

Answer: Cloning is most commonly used to point to an existing repository and create a clone or copy of that repository in a new directory. The original repository might be on the local disc or on a remote computer that supports the specified protocols. The cloning command duplicates an already existing Git repository.

Git obtains a full copy of virtually all data on the server rather than simply a functional copy. When you run git clone, it downloads all versions of each file. In fact, if your server disc becomes damaged, you may frequently utilize almost any of the clones on any client to restore the server to the state it was in when it was cloned.

You can clone a remote repository by using the following git command:

git clone <repo_link.git>

4. What is a remote repository?

Answer: In Git, a remote is a shared repository that all team members utilize to exchange changes. Most of the time, such a remote repository is hosted on a Git repository hosting platform like GitHub. 

Even if your PC is destroyed, the remote repository will remain in place so that you may retrieve the updated code and work on it again.

5. What is a fork?

Answer: The fork is the process of creating a copy of the repository. It is usually done to experiment with the project’s code and ensure that the original source code of the project remains unaltered. Also, it is used to make suggestions for modifications or to draw inspiration from someone else’s creation.

6. What is stashing in Git?

Answer: Stashing preserves your local changes in the current branch and reverts the working directory to the HEAD commit. This means that you can switch to a different branch without committing changes and then get back to the state where you left from the data structure that stores these states.

It allows you to save uncommitted changes to a buffer region called stash and then deletes them from the branch you’re working on. You may retrieve them later by using the git stash apply command.

7. What is the commit in Git?

Answer: Commit sets a message describing the modifications you’ve made. The commit also stores a revision of the code, which you may roll back to at any moment using commit ID .

Commit takes a snapshot of the project’s modifications which are presently staged. Committed snapshots are like project versions—Git will never alter them until you explicitly ask it to. The git add command is used prior to the execution of the git commit to promote or ‘stage’ changes to the project that will be saved in a commit.

Commits are the fundamental building blocks of a Git project timeline. Commits may be thought of as snapshots or milestones within a Git project’s history.

You can commit your changes using the following command:

git commit -m <message>

8. What is HEAD in Git?

Answer: The word HEAD refers to the commit that you are now reading.

Unless your repository’s main branch has a different name, you’ll see the tip of the master branch by default. The most recent commit on your codebase’s main branch is the tip of the master branch.

Git HEAD can also indicate a specific commit in a project’s history. This is due to the fact that Git allows you to examine multiple points in a repository’s history to see how your project has grown. Please note that there can be any number of heads in a repository.

9. What is conflict in Git?

Answer: Conflicts usually occur when two individuals alter the same code lines in the same file. Git cannot automatically identify what is correct code in this instance. Conflicts are resolved by the developer who merges the code changes. Git will identify the file as conflicting and stop the merging operation. You then need to resolve the conflicts and select the appropriate code to retain and what code to skip. Git creates special denotations like ‘======’,  ‘<<<<<<’ and ‘>>>>>>’ in your files to denote the conflicts. 

10. How do you resolve merge conflicts in Git?

Answer: Follow this procedure to resolve a merge conflict in Git:

  • The most straightforward approach to resolve the conflicting file is to open it and make the necessary modifications. You can use vim editor as well to open the file in the Git CLI and edit it.
  • After editing the file, you can stage the newly merged content with the git add command.
  • The final step is to use the git commit command to create a new commit.
  • To complete the merging, Git will generate a new merge commit.

11. When and why would you use the git status command?

Answer: The git status command shows the working directory status and the staging area. It allows you to view all the modifications along with files that are not monitored by Git. It also displays the deleted files and the uploaded files to the repository (untracked files).

The command also tells you what happened with git add and git commit. Status messages provide the appropriate file staging instructions.

Before committing changes, checking the condition of your repository is a recommended habit, so that you do not commit files unintentionally.

12. What is the staging area?

Answer: The staging area is basically a reserved area where files that will be committed are stored. You may add a file version or several files to your next commit (or in the next version of your project).

Please note that you can copy files into the staging area and also take them away from the staging area before making your commit.

What the git add command does is copy the current version of your file to the staging area from your working directory for committing it.

Staging helps you “check” specific changes when reviewing a complicated commit and focuses on the things you haven’t reviewed yet. You’ll probably examine the entire change with git diff before committing it. You will discover that if you make every modification as you look at it, you can focus on the adjustments not yet made.

You can add a file into the staging area with the following git command:

git add <file_name>

13. What is a branch in git? When to use it and how to create a new branch?

Answer: Git branches provide developers with different code workspaces. Naturally, the copy folder approach is considerably sophisticated. For instance, branches don’t waste disk space and are far better when you work with other developers on the same project.

You may just start a new branch and work on it to make significant changes in your project. This branch can then be fused into the main branch or deleted if necessary

The main advantage of the branches is that the development process becomes more secure. You prevent defects and issues from leaking into different contexts and you can quickly undo mistakes.

The git command for creating a new branch is as follows:

git branch -b <branch name>

To switch to a different branch, use this command:

git checkout <branch_name>

14. Git Vs Github

Answer:

Git

The version control system Git is spread like a graph data structure. Each node on the graph is a peer, which may store whole repositories, also as distributed back-ups with several nodes. There is no special idea of a central server. However, nodes can be headless or ‘bare’ in centralized version control systems, which have a similar role as a central server.

SVN, Mercurial are alternatives to Git.

Github

GitHub provides all of Git’s distributed revision monitoring and source code management functions. Also, GitHub offers many features and is a web-based Git repository hosting service.

GitHub provides access controls and several collaboration tools for each project, including wikis, task management, issue tracking, and function requests.

Atlassian and BitBucket are alternatives to GitHub.

15. What is a Detached Head?

Answer: Detached HEAD shows that the current repository checked out is not a local branch.

If a branch is read-only and we try to generate a commit for that branch, then commits cannot be called “free-floating commits.” They’d be in a detached state.

If the tag or commit is checked, and we attempt to make a new commit, the commit is again linked to no branch. When we attempt to check out a branch, these new commits will be placed at the top automatically.

16. What is an index in Git?

Answer: The index usually contains the tree that matches any HEAD commit and any changes that you make with ‘git add’ or “git rm,” which will be the new tree object for your new commit. But during mergers, it also performs a more complicated function when the parent and the two trees are combined.

But it always has git trees – the one that you can check, create, and merge. (These tree objects include only the SHA-1 file content identification and not the actual file information).

Because a commit points at a tree when a git checkout is made, the commit ID (if it is a branch) is retrieved, and the commit tree is loaded into the index and the file .git/HEAD is updated.

This is how HEAD and the index interlink; both are updated on checkout.

17.  Stage vs Commit in Git

Answer: Staging allows you to submit Git files you have completed working on. It gathers all modifications that are collectively committed.

Commit will then push and leave the other files that are not staged for commit. It might be seen as an extra step, as most of the changes are committed, however, it is extremely useful to commit a portion of the code and not the whole code.

Staging gives us the ability to commit smaller parts of codes. In this, we modify our code and then add these modified files to the staging area. 

You can add files in the staging area using the following command:

git add <file_name>

To add all changed files into the staging area, you need to use the following command:

git add .

Now, you can commit the files using the command:

git commit -m <message>

18. git pull vs git fetch

Answer: git fetch just downloads fresh data from the remote repository, but it does not merge any of it into your work files. The download feature is useful for gaining a fresh perspective on everything that happened in the remote repository.

git pull, on the other hand, is used to update the current HEAD branch with the most recent changes from a remote server. This implies that pull does more than simply download new versions of files;  it also immediately merges them with the local files.

Because “git pull” attempts to combine remote and local changes, a “merge conflict” may arise. As with many other operations, it is strongly advised that you begin “downloading git” with a clean working copy. This implies you should not make any unauthorized local modifications prior to pulling. To preserve local changes temporarily, use the git stash.

19. What is the squashing of commits in Git? When do you use it?

Answer: The ability to change commit history is a highly useful feature of Git. The main reason for doing this is that most of this information is only relevant to the developer who produced it, thus it must be simplified or improved before being sent to a shared repository.

Squashing a commit means, idiomatically, moving the changes introduced in that commit into its parent, such that you end up with one commit out of twos.

If you perform this procedure several times, you will be able to decrease n commit to a single one.

In Git, squashing is accomplished through the use of a Rebase, specifically an Interactive Rebase.

Simplifying, when you rebase a group of commits into branch A, you apply all of the changes introduced by those commits beginning from A rather than their original ancestor.

The rebasing of commits can be done using the below command:

git rebase -i branch

20. When do we use the git cherry-pick command?

Answer: When you wish to choose a specific commit from one branch and apply it to another, the cherry-pick command will come in handy.

Assume two individuals are working on a task, each working on a distinct code related to the task on two different branches and committing it after the code is complete. At the end of the day, they need to merge both the job and the master branch.

In this example, we choose one of the commits by copying its commit ID from the git log command, navigating it to another branch, then applying the changes in the commit to the current branch using the command git cherry-pick <copied-commit-id>.

21. What is unstaging in Git? How do you unstage a file in Git?

Answer: As you’re undoubtedly aware, Git employs the notion of an index, sometimes known as a “staging” region, to allow you to choose the code that you want to include in a commit. You can add files or modifications to files to staging using the command git add; they will be included the next time you commit.

However, suppose you inadvertently add something to staging that you don’t want to maintain, then you may need to unstage your changes back to a normal state. This can be done by using the following git command:

git reset path/file.txt

22. git revert vs git reset

Answer: The git revert command is used to undo a specific commit. When a commit causes a regression during testing, it is usually reversed.

Also, when you use git revert, it creates a commit.

git reset, on the other hand, is used to return the current HEAD to the given state. It is also useful in the following situations.

  1. When you made some modifications and no longer want to commit them and return to your previous clean condition.
  2. When doing a git pull, it might lead to merge errors. In this scenario, you may simply wish to get rid of the mess, and in such a case, you may need to use the reset command.

You can use the reset command as shown below:

git reset --hard HEAD

23. What are tags in Git. How do you create one

Answer: Tags are references to specific points in Git history. Tagging is typically used to record a point in time that will be utilized for a certain version release (i.e. v2. 0.2). A tag can be created using following syntax

git tag <name>

24. What is Git stash apply command

Answer: If you wish to resume working where you left off, use the ‘git stash apply‘ command to restore the stored modifications to your current working directory.

25. What data structure does git resembles 

Answer: It resembles a tree data structure where we have different branches. We can merge multiple branches and traverse through various nodes of the tree

26. What is task branching strategy

Answer: Each task is implemented on its own branch in this paradigm, with the task key contained in the branch name. Simply check at the task key in the branch name to discover which branch implements which task.

27. What is feature branching

Answer: A feature branch model stores all modifications for a specific feature within a branch. When the feature has been thoroughly verified and confirmed using automated tests, the branch is merged into master.

28. What is forking workflow

Answer: Rather than relying on a single server-side repository to serve as the “central” codebase, it provides each developer with their own server-side repository. A developer can fork the repositories and work on their own server repository and create PRs from it. The Forking Workflow is often observed in open-source projects that are available to the public.

29. How will you check if a branch has been merged into the current branch

Answer: This command will list the branches merge into the current branch

git branch --merged

30. What does ‘hooks’ contain

Answer: This directory contains shell scripts that are activated when the appropriate Git commands are executed. For example, when you perform a commit, git will attempt to execute the post-commit script.

Conclusion

So, we’ve discussed Git, top Git interview questions, and the implementation of certain git commands in this post. Git’s popularity is skyrocketing, and it is being widely used to manage projects efficiently.

Git is a free distributed version control system that allows developers to collaborate with numerous individuals on projects. With so much scope and ever-increasing demand, Git has become an essential tool for developers.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *