20 Most asked Interview questions on Git [Full List with answers]

20 Most asked Interview questions on Git [Full List with answers]

From cloud computing to DevOps to Project Management, GIT is probably the best source code management tool of this era. Git's prominence in DevOps makes it clear that there will be plenty of job prospects in the near future. So, taking this into consideration let's dive straight onto the top most asked git interview questions with their answers.

Today, we listed some of the top Git interview questions that will help you crack an interview.

So, let’s get started!

Before going forward let's understand first What is Git? and Why we use it?

  • The most well-known, open-source, and extensively used distributed version control system (DVCS) is called Git, and it is used to oversee the development of both small and big projects in a more effective and organized way.
  • It works best when there are several team members working on a project together. It is used to track project modifications and effectively fosters teamwork during the development process.

Most asked GIT Interview Questions

1. What is a version control system (VCS)?

The contributions made by developers who work collaboratively on projects are tracked by a VCS. They keep track of any code modifications made, which allows developers the advantage to add new code, correct problems, and running tests with the knowledge that their prior working copy may be restored at any time if something goes wrong.

2. What is a git repository?

Git lets developers see the entire timeline of their changes, decisions, and progression of any project in one place. From the moment they access the history of a project, the developer has all the context they need to understand it and start contributing.

3. How Git is different from Github?

Similar to DropBox or Google Drive, GitHub is an online program that lets users host files, but it is made specifically to host Git Repositories. Programmers typically use Git, a command-line tool, to control the versioning (history) of software projects. Every project is housed within a Git Repository, which keeps track of all modifications.

4. What does git clone do?

It is used to point to an existing repository and make a clone or copy of that repository in a new directory, at another location.

5. List 5 main features of Git.

  • It is free and open-source.
  • It helps to track history
  • It Supports non-linear development
  • It Creates backup
  • Scalable
  • It Supports collaboration
  • Branching is easier
  • Distributed development.

6. What does the command git config do?

The git config statement makes it simple to define configuration settings for the repository's operation, user preferences, installation-based setups for git, and many other things.

For example: Before utilizing git commands, we may perform the following instructions to set up your name and email address:

git config --global user. name

output: “your_name"

git config --global user. email

output: “your_email"

image.png

7. What is a conflict and how will you resolve it?

Git typically manages feature merges automatically, although there may sometimes be issues while working in a collaborative context. For example:

  1. When two separate branches have changes to the same line in a file
  2. A file is deleted in one branch but modified in the other.

The procedures taken to resolve git conflicts are as follows:

  1. Identify the files that have conflicts.
  2. Discuss with members who have worked on the file and ensure that the required changes are done in the file.
  3. Add these files to the staged section by using the git add command.
  4. Commit these changes using the git commit command.
  5. Finally, push the changes to the branch using the git

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

Without actually bringing the changes into the local repository, Git Fetch is the command that notifies the local repository that there are updates available in the remote repository. On the other hand, Git Pull copies the changes to the remote location and adds them to the local repository.

completion.png

Git pull = git fetch + git merge

9. How will you create a git repository?

  • Install Git in your system.
  • Then, to create a git repository, create a folder for the project and then run git init.
  • By doing this, an a.git file will be generated in the project directory, signifying that the repository has been set up.

10. Tell me something about git stash.

The git stash command is used to record the current state of the working directory and index in a stash. The current working directory state and index are essentially pushed to the stack for later usage when using the git stash command, creating a clean working directory for further activities.

image.png

11. Why do we not call git “pull request” as “push request”?

"Push" is forcing the changes being present in the target repository (git push). "Pull" is the target repository grabbing your changes to be present there (git pull from the other repo). Untitled design.png

12. What does the git status command do?

The current directory and staging area's status are shown by the git status command. It enables you to identify which files are not being monitored by Git as well as which changes have been staged and which have not.

13. Mention the various Git repository hosting functions.

The following are the Git repository hosting functions:

  1. Pikacode
  2. Visual Studio Online
  3. GitHub
  4. GitEnterprise
  5. SourceForge.net

14. What are the functionalities of git reset --mixed and git merge --abort?

git reset is a mixed command which is used for undoing changes to the working directory and the git index.

git merger is an abort command which is used for stopping the merge process and returning back to the state before the merging occurred.

15. How do you find a commit which broke something after a merge operation?

  • If we are unsure of precisely what to look at, this procedure may take a while. Thankfully, git has a fantastic search tool called git-bisect that operates on the binary search idea.

  • Do the initial setup using:

    git bisect start         # initiates the bisecting session
    git bisect bad           # marks current revision as bad
    git bisect good revision # marks last known commit as good revision
    
  • After executing the aforementioned commands, git pulls a revision that is classified as being in the middle of "good" and "poor" versions. By designating the commit as "good" or "bad," this approach may be repeated until the commit that has a problem is discovered.

16. Explain the steps involved in removing a file from the git index without removing it from the local file system.

  • When we use the git add command carelessly, we might occasionally end up with certain files that are not desired in the git index. It's not usually a good idea to remove files from the local working tree and the index simultaneously by using the git rm command.

  • To prevent making the same error twice, we may use the git reset command to remove the file from the staged version and then add it to the .gitignorefile. This is an alternative to using the git rm command.

17. How to revert a bad commit that is already pushed?

In some circumstances, we might want to undo the pushed modifications and return to the earlier version. Based on the circumstances, there are two alternative methods to manage this:

Method 1: Fix the bad changes to the files and create a new commit and push it to the remote repository. This step is the simplest and most recommended approach to fixing bad changes. You can use the command: git commit -m "<message>"

Method 2: New commit can be created that reverts changes done in the bad commit. It can be done using git revert <name of bad commit>

18. What is the best advisable step in cases of broken commit: Create an additional commit OR amend an existing commit?

For the following reasons, creating a new commit is always preferable to editing an old one:

  • Executing an amend action wipes out any previously saved state for that commit. It's okay if only the commit message is altered or deleted, however, there may be instances when the commits' actual contents are changed. As a result, crucial information related to the commitment is lost.

  • Git commit —amend overuse might have negative effects since the tiny commit amend can expand and accumulate unrelated modifications over time.

19. State the differences between git revert and git reset.

Utilizing the git revert command, one may make a new commit that undoes the modifications performed in the preceding commit. This command adds a new history to the project; it does not change the one that already exists.

The git reset command is used for undoing the local changes done in the git repository. This command manipulates the working directory, git index, and commit history.

20. What does the git annotate command do?

This command adds information from the commit that brought the modification to each line in the specified file. Optionally, this command can annotate from a specified revision.

Syntax: git annotate [<options>] <file> [<revision>]

Conclusion

The git introduction and scope, as well as the most typical git interview questions, were covered in this post. Git's popularity is increasing incredibly quickly, and it is projected that one day, with the aid of git, all developers around the globe would collaborate as a single organism.

We hope now you are feeling a little more confident about your GIT basics. Feel free to add your comments, thoughts, or feedback in the comment section below.

And finally, good luck with the interview! You got this!

Happy Learning!!