Top 50 Git Interview Questions and Answers in 2024 [Prepare for Success]

Posted in /  

Top 50 Git Interview Questions and Answers in 2024 [Prepare for Success]
vinaykhatri

Vinay Khatri
Last updated on May 30, 2024

    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.

    Also, it is 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 most 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 the role of a developer or a DevOps engineer . 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. Additionally, 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. A version control system also has features like branches, conflicts, and merges, which we will be discussing in the later sections 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 the system of each developer. This implies that the code is not simply kept on a central server, but a copy of the code is present on all the systems 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.

    Top 50 Git Interview Questions

    We have divided the list of commonly asked Git interview questions into three levels, namely beginner-level Git interview questions, intermediate-level Git interview questions, and advanced-level Git interview questions. Let us begin with beginner Git interview questions.

    Beginner-Level 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 VCS, 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:

    1. 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.
    2. 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 repository 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 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 a commit in Git?

    Answer: Commit sets a message describing the modifications you've made. It also stores a revision of the code, which you may roll back to at any moment using the 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 command 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 resolving 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 the git diff command 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. State the difference between Git and 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 and Mercurial are alternatives to Git.

    GitHub

    GitHub provides all the distributed revision monitoring and source code management functions in Git. Also, GitHub offers many features and is a web-based Git repository hosting service. It provides access controls and several collaboration tools for each project, including wikis, task management, issue tracking, and function requests. BitBucket and Mercurial 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 ones 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, 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.  Can you explain the difference between Stage and 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. Differentiate git pull and 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 command.

    Intermediate-Level Git Interview Questions

    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 two. If you perform this procedure several times, you will be able to decrease n commits 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 git cherry-pick <copied-commit-id > command.

    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 git add command; 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. List out the difference between git revert and 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 git 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 the following syntax:

    git tag <name>

    24. What is the w G it 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 resemble?

    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. Explain a 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 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 the 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, work on their own server repository, and create PRs. The forking workflow is often observed in open-source projects 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 do ‘hooks’ contain?

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

    31. What is the function of the Git ls-tree?

    Answer: The function ls-tree in Git returns the tree representation of the current repository. Along with it, this command also highlights the mode and name of each item and the SHA-1 value of the blob.

    32. Can you state the difference between pull requests and branches?

    Answer:

    Pull Request: A pull request, also known as a merge request, is considered an event in the software development process where developers or contributors start to merge new code changes with the main project repository. In other words, we can say that a pull request helps you tell others about the changes that you have made or pushed to their branch in a GitHub repository.

    Branch: We can simply state a branch as a separate version of code.

    33. How will you delete a branch in Git?

    Answer: The following are the commands to delete a branch:

    • The first command to delete a branch in git is as follows:
    git branch –d [head]
    • Run the below command to delete a branch locally:
    git branch -d <local_branch_name>
    • The following command deletes a branch remotely:
    git push origin --delete <remote_branch_name>

    34. What does a commit object consists of?

    Answer: A commit object comprises the following components:

    • A set of files representing the states of a project at a given point in time.
    • SHA-1 name is a 40-character string that uniquely identifies the commit object.
    • A reference to the parent commit object.

    Advanced-Level Git Interview Questions

    35. State the difference between Git and SVN.

    Answer: The following table highlights the key differences between Git and SVN:

    Git SVN
    Git is a decentralized distributed version control system. SVN is a centralized version control system.
    This system stores data in the form of metadata. This system stores data in the form of files.
    It provides more protection to content than SVN. The content in SVN is less secure.
    Git has a cloned repository. SVN does not have a cloned repository.
    There is no Global Revision Number for Git. SVN has a Global Revision Number.
    Git uses the SHA-1 hash algorithm to hash its content. This system does not support hashed content.

    36. How will you fix a broken commit in Git?

    Answer: To fix a broken commit in Git, we can use the git commit—amend command.

    37. What do you know about Subgit?

    Answer: Subgit is a popular tool that helps us migrate from SVN to Git version control system. It converts SVN repositories into Git repos and lets us work with both. In addition, it is compatible with any Git server, such as GitHub, GitLab, Gerrit, or Bitbucket. More interestingly, Subgit enables us to use all the features of Git and SVN.

    38. Enlist some popular graphical Git clients for Linux.

    Answer: A graphical Git client is a software application that lets you work with Git in visual mode. It comes with built-in options, eliminating the need to write the commands manually. The following are the popular graphical Git clients for Linux:

    • Giggle
    • Git GUI
    • Smart Git
    • Git-g
    • Git Cola
    • qGit

    39. Enlist different git repository hosting functions.

    Answer: The following are the Git repository hosting functions:

    • Visual Studio Online
    • Pikacode
    • GitEnterprise
    • SourceForge.net
    • GitHub

    40. What is rebasing? Can you state the syntax for rebasing?

    Answer: Rebasing is the process of combining or moving a sequence of commits to a new base commit. The following is the syntax for rebasing:

    git rebase [new-commit]

    41. How will you remove a file from Git without removing it from the local system?

    Answer: To remove a file from Git without removing it from the local system, we can use the cached option. The following is the syntax:

    git rm -rf –cached $FILES

    42. Is it possible to recover a deleted branch in Git?

    Answer: Yes, it is possible to recover a deleted branch in Git. In order to do so, you first need to find the SHA for the commit at the tip of the deleted branch using the following command:

    git reflog

    Use the following command to restore the deleted branch:

    git checkout -b <branch> <sha>

    If you have just deleted the branch, you will observe the following message on your terminal:

    Deleted branch <your-branch> (was <sha>)

    To restore that branch, enter the following command:

    git checkout -b <branch> <sha>

    43. Can you state the difference between git checkout [branch name] and git checkout -b [branch name]?

    Answer: The git checkout [branch name] command will switch from one branch to another branch. On the other hand, the git checkout -b [branch name] command will create a new branch and switch to it.

    44. What do you know about the git config command?

    Answer: The git config command is used to set the configuration options for Git installation. For instance, when you download Git, you will use the following commands to set a username and commit an email address in Git:

    $ git config –global user.name “<username>”
    $ git config –global user.email “<email id>”

    45. What do you know about Git Instaweb?

    Answer: Git Instaweb is a script used to set a temporary instance of Gitweb on a web server for browsing local repositories. Therefore, it becomes easier for us to browse working repositories in Gitweb quickly.

    46. How will you create the bare repository?

    Answer: To create the bare repository, first, you need to navigate to the chosen directory in bash or command prompt.

    >mkdir FileName.git
    
    >cd FileName.git
    
    >git init –bare

    47. How will you revert back the commit in Git that has already been made public?

    Answer: The following are the two ways to revert back a commit that has already been made public:

    • Firstly, remove the bad file present in a new commit and make the necessary changes to that file to correct it. Finally, push it to the remote repository using the following command.
    git commit -m “commit message”
    • Another way is to create a new commit that will undo all the changes that you made in the bad commit. You can do it by using the following command:
    git revert <name of bad commit>

    48. What work will you restore when you recover the deleted branch?

    Answer: When you recover the deleted branch, the files that were stashed in the stash index list will be recovered. Therefore, it is always good to stash or commit your work so that you can restore it when you recover the branch.

    49. What do you know about git stash drop?

    Answer: When we want to remove the stash item from the git stash list, we need to use the git stash drop command. By default, this command removes the last added stash item from the git stash list.

    git stash drop

    However, if you want to remove a specific stash item, you can pass the argument to the git stash drop command.

    git stash drop <stash id>

    50. Which command will you use to find the list of files that have changed in a particular command?

    Answer: To find the list of files that have changed in a particular commit, we need to use the following command:

    git diff-tree -r {hash}

    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:

    FAQs


    You can expect questions on Git in the interviews for job roles of developers since it has become one of teh core skills of developers. It is a version control system that lets developers keep track of their source code.

    As Git has been one of the core development tools among developers that facilitate the development process, you can consider it a technical skill.

    Git is a free and open-source distributed version control system (VCS). Using Git, developers or programmers, along with their team members, can collaborate and work on the source code of a single project simultaneously. Also, every developer in a team can track the changes in the source code.

    Yes, Git is a DevOps tool used across the software development industry. It has come in handy in this remote working mode.

    Yes, you should definitely add Git as a skill on your resume as it demonstrates to employers that you are very well aware of the source code management tool.

    Leave a Comment on this Post

    0 Comments