Signup/Sign In

Git Interview Questions

Git is a popular version control system and a lot of companies use Git to manage their code. These companies may test the candidates' knowledge of Git in an interview. In this tutorial, we will see some of the most commonly asked questions about Git in an interview.

Git Interview Questions

Beginner Git Questions

Q1. What is the difference between a Distributed VCS and a Centralized VCS? Can you give a few examples of both types?

A distributed version control system or DVCS allows the developers to have the complete history of the project which is managed using a DVCS. Any person who clones such a repository will have access to the entire commit history of the project. On the other hand, a centralized VCS will have the project history stored on a central server, and developers can only get a working local copy of only the most recent version of the project.

Some popular DVCS are Git and Mercurial. Perforce and SVN are some commonly used centralized VCS.

Q2. What is a Git Repository?

Any project managed using Git is called a Git repository. A Git repository will store all the project files and additional data needed for version control.

Q3. What is the use of Staging Area in Git?

The staging area is used to segregate, organize and review the changes made to the files and add them to the next commit. It is an intermediate step between making changes in the working directory and committing them to the repository. Multiple files can be modified in the working directory but if we only wish to include some of them in the next commit, then we can do that with the help of the staging area. It also helps us in making atomic commits.

Q4. What is the use of the Git Clone command?

The Git Clone command is used to create a local copy of some other repository. When working on a team, the Git Clone is the first command that we will use to copy the central remote repository to our local system.

Q5. What is the difference between Git Fetch and Git Pull command?

The Git Fetch command will download the changes from the remote repository and adds those changes to the remote-tracking branches in our local repository. Git Pull will first download the changes and will also update our local branch with those changes. Basically, the Git Pull command first executes the Git Fetch command to download the new commits and then executes the Git Merge command to merge these commits into our local branch.

Q6.What is a commit? What is stored in a commit object?

A commit is a snapshot of our project which is permanently stored in the Git repository. Commits are used to save different versions of our project. A commit object contains the files that represent the state of the repository at the time when a commit was. A commit also has a parent commit(the commit that was made before it) and the commit object stores a reference to this parent. Each commit object has a unique hash value present in the commit object.

Q7. List a few advantages of using Git.

  • Easy to collaborate with others on a project.

  • Git works on a distributed model and this gives a person access to the complete version history of a project when he clones it.

  • Git provides lightweight and robust branches capabilities.

  • We do not require an internet connection to work with Git on our local machine. This makes it a lot faster than other version control systems that need to communicate with the remote server.

Q8. Are there any differences between Git and GitHub?

Git is a version control system and a source code management tool that is used to manage different versions of our project. GitHub is a Git repository hosting service where we can upload and backup our local Git repositories and also helps us to collaborate with other developers. GitHub provides all the functionalities of Git and a few additional features of its own.

Q9. What does the Git Log command do?

The Git Log command is used to view the version history of our project. We can view the commits that were made and additional information like the name and email of the committer, the date and time when the commit was made, and tags and branches present on the commits. We can also format and filter out the results by using this command.

Q10. Why is the .git directory kept hidden by default?

The .git directory stores all the data that is needed for version control. It is kept hidden to prevent us from accidentally deleting or modifying its content. Our project can get corrupted or we may lose the version history if we try to modify its content.

Intermediate Git Questions

Q1. Explain the two merging strategies used by Git?

The two merge strategies used by Git are Fast Forward Merge and Three-Way Merge.

Fast Forward Merge: This type of merge happens when the two branches are on the same linear path. Git will simply update the lagging branch and set it to reference the same commit as the leading branch. It is sometimes also known as the Two-Way merge

Three-Way Merge: This merge strategy is used when the two branches have diverged from each other and there is not linear path between them. In this type of merge, Git will create a new merge commit which will have the changes from both the branches. This merge commit will have two parents, one from each branch.

Q2. What all commands can we use to create a new branch in Git?

We can use three different commands to create a new branch in Git. We can use the Git Branch command, the Git Checkout command, and the Git Switch command to create a new branch. The Git Checkout command and the Git Switch command will also take us to the newly created branch i.e. the new branch will become our current branch.

Q3. How is Git Revert different from Git Reset?

Git Reset will undo a change by taking us to some previously committed state. Basically, it rewrites the commit history. Git Revert, on the other hand, will undo a commit by creating a new commit with the inverse of the changes present in the unwanted commit. Git Revert will undo changes in a forward-moving fashion.

Q4. What is the advantage of setting up Upstream branches?

Upstream branches are the branches present in the remote repository that is tracked by our local branches. Setting up an upstream branch connection will help us to quickly push and pull changes to the remote repository without specifying the remote name and the branch name. We can also check if our local branch is ahead or behind the remote branch.

Q5. When will a Git repository go in a Detached HEAD state?

A detached HEAD state occurs when the HEAD directly points to a commit instead of pointing to a branch. A HEAD gets detached if we directly checkout a commit, a tag, or a remote-tracking branch.

Q6. What are the advantages of using branches in Git?

Git provides a lightweight and powerful branching mechanism. Branches provide developers an independent work environment where they can develop new features or experiment with things without worrying about affecting the rest of the project. These branches can then be merged with other branches like the master branch.

Q7. What are the different levels of configurations in Git?

Git provides three levels of configuration - Local, Global, and System.

Local: Local level configurations are the ones that only affect our current Git repository. These changes will not affect any other repository.

Global: Global level configurations are the ones that will affect all the repositories for a particular user of a system.

System: System level configurations will affect all the repositories of all the users that share a system.

We can change these configurations by using the Git Config command with the --local, --global, --system options.

Q8. What is the role of the Git Stash command? Write a few options that can be used with this command?

Git Stash command is used to securely store unsaved changes. Stashing is done so that we can temporarily save changes that we don't want to commit yet and work on some other things. We can continue where we left by unstashing these changes.

To simply stash changes:

$ git stash 

To stash changes and also add a message for that stash:

$ git stash save "message"

To view the stashes:

$ git stash list

To apply a stash:

$ git stash apply <stash-id>

To drop a stash:

$ git stash drop <stash-id>

Q9. How will you squash commits in Git?

We can squash commits by using the Git Rebase command in its interactive mode. We can also squash multiple commits of a branch while merging it to another branch by using the --squash option with the Git Merge command.

Q10. What is a Merge Conflict? How will you resolve one?

A merge conflict occurs when two branches have made changes to the same line of the same file. In such a situation, Git does not know which version of the file to keep and which one to discard, so a merge conflict occurs. Git leaves it up to the developers to resolve the conflict and continue the merging process.

The best way to resolve a conflict is to have a chat with the other developers and decide the version of the file that they want to keep. After making the required changes, add the file to the staging area and commit the changes.

Q11. What is the use of the Git Cherry-Pick command?

The Git Cherrypick command is used to take a certain commit from some other branch and add it to the current branch. Cherrypicking is mostly done when we just want a single commit from a branch and do not want to merge the entire branch into the current branch. It is also used when we have accidentally committed to a wrong branch and want to move that commit to some other branch.

Q12. Explain Git Tags.

Tags are a way of marking commits so that we can reference them in the future. In most cases, tags are used to mark release versions of the project. We have two types of tags in Git - Unannotated or Lightweight and Annotated. Lightweight tags are simple pointers to a commit but annotated tags are capable of storing additional information like a tag message, commit information, etc.

Q13. What are the three modes of the Git Reset command?

Git Reset can work in three different modes - soft, hard and mixed. In the soft mode, Git will only reset to some previous commit(reset the repository) but will keep the staging area and the working directory as it is. In mixed mode, it will reset to a previous commit and will also reset the staging area. In the hard mode, Git will reset all three stages i.e. the working directory, the staging area and the repository.

Q14. When will you prefer SVN over Git and when is Git preferred over SVN?

SVN is preferred over Git when we require a centralized workflow. It is also preferred when we have to work with binary files.

Git is preferred over SVN when we want a distributed workflow. Git also provides better branching support and is more widely used.

Q15. Explain the two modes in which we can Git Rebase command?

Git Rebase has the standard and the interactive mode of working. In the standard mode, we can just rebase a branch to some other branch. The interactive mode provides us with a lot of other options to work with. We can alter the previous commit messages, squash commits, drop certain commits from a branch, and do a lot more.

Advanced Git Questions

Q1. Which command will you use to undo a commit which is already pushed to the remote repository?

We cannot undo a commit that is pushed to the remote repository by using the Git Reset command because it will rewrite the history of our project and Git will not allow us to push if the changes of the remote branches are not present on our local branches. We can use the Git Revert command to undo the commit because Git Revert will create a new commit with the inverse of the changes of the unwanted commit. It will not affect the history of the project and so we can easily push it to the remote repository.

Q2. What all commands can be used to unstage a file from the staging area in Git?

We can use the Git Reset command, the Git Restore command, and the git rm command to unstaged files in Git.

$ git reset <file-name>
$ git restore --staged <file-name>
$ git rm --cached <file-name>

Q3. What is the difference between git whatchanged and git log? Are there any flags or options that can be used with the git log command to generate the same output as git whatchanged?

git whatchanged command and the git log command is used to view the commit history of the project. The difference is in the output of these commands. In their basic form without any flags or options, the git whatchanged command will not show us the merge commits. Another difference is that git whatchanged will show us the files that were a part of each commit and git log does not show us this information.

We can achieve the same output from the git log command by using the --no-merges option and the --raw option.

$ git log --no-merges --raw

Q4. What is a Git Pull Request and why is it used?

A pull request is a way of informing another team that you have pushed some changes to the remote repository. It is also a way of telling them to review your code, discuss any changes, and add follow-up commits if needed. If everyone is satisfied with the changes, then the changes can be merged with the master branch and the pull request can be closed.

Q5. What is the difference between the Double Dot and the Triple Dot when used with the Git Diff command to compare two branches?

The double dot notation is used to compare the tips(the most recent commits) of the two branches. The triple dot notation is used to compare the tip of the branch with the common ancestor of some other branch. The following image shows the commits that will be compared by the two notations.

Double dot vs triple dot

Q6. When is rebasing preferred over merging?

Rebasing is preferred over merging when we want to have a linear commit history. Merging will add new merge commits and it can make the history of the project difficult to understand.

Q7. What is Git Reflog?

The git reflog command will show us all the commits that were previously created and were referenced by the HEAD. This command is mostly used to recover commits that were lost due to a wrong reset.

Q8. When will you prefer merging over rebasing?

Merging is preferred over rebasing when we want to have the original commit history. Merging is also preferred over rebasing when working on shared branches because rebasing will essentially change the history of commits and this may lead to inconsistencies for other developers. Reverting a rebase is also very difficult and can lead to loss of data but we can easily revert a merge.

Q9. What is the difference between the -d flag and the -D flag when used with the git branch command?

The -d flag is used to delete branches but we cannot use it to delete unmerged branches because we will lose the work done on these branches. We can use the -D flag to delete unmerged branches from our repository. It is a way of forcefully deleting branches.

Q10. How is Forking different from Cloning?

Forking and cloning both create a new copy of a repository, but the difference is that fork creates a remote, server-side copy of the repository, whereas cloning will create a local copy. Cloning is done by the Git Clone command, but the ability to fork is provided to us by Git repository hosting services like GitHub.

Q11. What is the use of the .gitignore file? Can you write a Glob pattern that will ignore all the files starting with the letter "a" and having a ".py" extension?

The .gitignore file is used to instruct Git to ignore some files and do not track changes made to these files. We can add the names of the files that we want Git to ignore to this file. We can also use Glob patterns and any file names that match the pattern will be ignored.

"a*.py" will ignore all files starting with "a" and ending with ".py". The * is used to match 0 or more characters.

Learn More: Beginners guide to .gitignore file with Example

Q12. What are Hooks?

Hooks are scripts that have a fixed set of instructions and these instructions will automatically get executed if a certain event occurs. Hooks are stored in a .git/hooks directory.

Q13. Why should we pull before pushing to a remote repository?

Pulling and pushing are common operations that we will perform when working with remote repositories. Since we are working in a team and other members may have pushed to the repository and we may not be aware of those changes, pulling before pushing is always a good idea. We can then alter our changes accordingly. Also, Git will block a push if the changes of the remote repository are absent from our local repository.

Q14. Briefly explain the role of the branches involved in the Gitflow workflow?

Five types of branches are involved in the Gitflow workflow.

Master: This is the main branch that will just have the release versions of the project. No commits are directly added to this branch.

Develop: Changes are developed and integrated into this branch. Other feature branches are also created from this branch.

Feature: A developer will create a new feature branch to work on developing a new feature for the project. These branches are based on the development branch and are also merged back into it.

Release: Whenever the commits on the develop branch are ready to be released, a new release branch is created from it. No new major changes are added to this branch and we only include small bug fixes and documentation required for the release. It is merged into the master branch and develop branch.

Hotfix: If a quick solution is required to a problem in a release version, then a hotfix branch is created based on the master branch and the solution to the problem is made on this branch. It is then merged into the master and the develop branch.

Q15. What is the use of the --amend option when used with the Git Commit command?

The --amend option can be used to modify the most recent commit. We can use it to change the commit message, add or remove files from the commit or undo some changes to the previous commit.

Summary

Git is a very popular version control system and sometimes companies expect us to know a few things about it. In this tutorial, we covered some of the most important Git questions that can be asked in an interview. I hope this tutorial was helpful and that you learned something new.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight