Git Top Interview Questions and Answers.

Git is an open-source project that tracks changes in source code during software development. It can handle both small and large projects with speed and efficiency. It is one of the best source code management with features like task management and bug tracking and is widely used by most industry experts nowadays.

Q1. What is Git?

Ans. Git is an open-source project distributed version control system (DVCS). Many commercial projects rely on Git as every developer’s code copy is also treated as a repository, which contains all changes done in the past. Below is the detailed description of DVCS:

  • Control System: Git is known for its features like a content tracker, and it stores content.
  • Version Control System: It helps developers to store code at the same time and Git modifies as and when more codes are added. The version control system helps in maintaining and keeping records of all the changes. Further, it offers features like branches and merges.
  • Distributed Version Control System: Git has a remote repository and a local repository, which are stored on servers and computers, respectively. This means that code is stored in both the central server and the developer’s computer. Hence it is termed a distributed version control system.

Q2. Why is it said that Git is designed keeping in mind performance, security, and flexibility?

Ans. Git was developed in 2015 by Linus Torvalds for Linux kernel development. But in the last decade, it gained a lot of interest, and today, due to its flexibility, nearly every development environment uses Git and runs Git command-line tools on every major operating system.

Below are the reasons why Git is popular:

  • Performance: Git has very powerful raw performance characteristics be it branching, merging, or comparing the past versions; it is robust and optimized. Git gives special attention to the content, and it uses a blend of delta encoding and compression. Further, it also clearly stores directory contents and metadata object versions.
  • Security: Integrity is the topmost priority of Git. Its cryptography hashing algorithm named SHA1 safely stores all objects in the Git repository and maintains a true relationship between files and directories.
  • Flexibility: From supporting nonlinear development workflow to adaptability with various systems and protocols, Git is exceptionally elastic. Git’s amazing tracking system offers features like treating branching and tagging as first-class citizens. Its ‘change history’ also features stores operations affecting branches and tags.

Q3. What is the meaning of the commands – git status, git log, git diff, git revert <commit>,  git reset <file>?

Ans.

CommandMeaning
git statusGives a list of which files are staged, unstaged, and untracked
git logIllustrates the entire commit history by using the default format
git diffDisplays the unstaged changes between index and working directory
git revert <commit>Undoes all the changes made in <commit> and applies it to the current branch by creating a new commit
git reset <file>Removes <file> from a staging area without overwriting any changes by keeping the working directory unchanged

Q4. What is Commit?

Ans. Git creates a commit as and when a developer saves any new work. Commit is a screenshot of all the files, and Git will use the previously used file if a file is not changed from one commit to another. One commit creates a chain to other commits and forms a development history graph. Unique cryptocurrency hash identifies commit in Git.

Q5. What are Branches in Git?

Ans. As multiple developers work parallel on a program, they create their own local repository, and this creates multiple changes in a single commit. However, in Git, branches manage various separations, and once the work in a branch is finished, it is merged with the master branch.

Q6. Are Git and GitHub the same thing?

Ans. Git and GitHub are connected as Github is a service to use Git, but they both have slight differences:

GitGitHub
Git is a software tool to use a version control system GitHub is a hosting service for git repositories
Tool for projects that want to collaboratively develop softwareService for projects that use Git for version control

Q7. Which is a better version control system – Git or SVN?

Ans. Both Git and SVN have their pros and cons, and below are the differences between Git and SVN:

GitSVN
Decentralized and distributed version control toolCentralized version control tool
Clones all repositories on the local systemStores version history on the server-side repository
Supports offline commitsSupports online commits only
Swift push/pull operationsSlow push/pull operations
Automatically shares work to commitDoesn’t support automatic sharing

Q8. Name some Git repository hosting functions.

Ans. Below is the list:

  • Github
  • Gitlab
  • Bitbucket
  • SourceForge
  • GitEnterprise

Q9. State the difference between “git pull” and “git fetch.”

Ans. This is an important Git interview question. “git pull” and “git fetch” are used for downloading new data from a remote repository.

“git fetch – It downloads new data from the repository but does not support integrating this data into working files. It offers a fresh view of things that happened in the remote repository.

“git pull” – This command is used to update the current HEAD data branch with all the changes that occurred in the remote repository. Thus, it downloads the data and integrates it with existing working files.

Q10. How do you edit or fix the last commit message in Git?

Ans. If you forget to add anything in the commit message or committed a typo error, you can rectify it by using the –amend flag command.

$ git commit –amend -m “Sorry I missed an important update”

Note: –amend flag will only help in editing or fixing the last commit message.

Q11. How can you change any older commit messages?

Ans. To change an older commit the command is –

$ git rebase –interactive

Q12. How to deal with huge binary files in Git?

Ans. Handling large binary files is a significant problem in git, and to handle this problem “Large File Storage” extension works well for Git. Simply install LFS on your local computer, and after this, your large files will not be stored in the local repository. Instead, it will be stored in a dedicated LFS cache and store.

Q13. How to resolve and solve merge conflicts?

Ans. It is very easy to resolve merge conflicts as Git allows you to go back to the previous state. Just use a “git merge –abort” command, and you will be able to undo the merge and start the task again.

Q14. What do you mean by “git cherry-pick”?

Ans. If by mistake, you have committed a change into the wrong branch, you can use the “git cherry-pick” command. This command will allow you to apply commit from one branch to another branch. 

$ git cherry-pick <commit id>

Q15. In which scenario do you use the “git cherry-pick” command?

Ans. Git cherry-pick command can sometimes result in duplicate commits, and thus, it must be cautiously used. The below situations are apt if planning to use the git cherry-pick command:

  • When you mistakenly make a commit in the wrong branch
  • When you want to make changes that are proposed by other team members

Q16. Name some of the Git tools that you use.

Ans. This is a commonly asked Git interview question. Below is the list of most popular Git tools:

Git ToolsLicense Type
GitHub DesktopMIT
GitKrakenProprietary
SmartGitProprietary
TowerProprietary
Git UpGNU GPL

Q17. What do you mean by the bare repository in Git?

Ans. While initializing a new Git repository, – run git init function is used, and this directory becomes a ‘Working Tree.’ Also, Git creates its own .git directory (which is hidden) where it tracks all the changes and stores the commit objects.

However, a bare repository, also called bare repos, works without creating a ‘Working Tree.’ This bare repository is utilized as a remote repository. It helps share it 

with all the users where the developers will clone it and locally make the required changes.

Q18. Why do developers use Git Clone?

Ans. Developers prefer cloning as it is the simplest way to get a copy of the central repository. The ‘git clone’ command helps in generating a copy of the current Git repository.

Q19. Will you create an additional commit or amend an existing commit?

Ans. It is preferable to create an additional commit because:

  • It might cause inappropriate changes
  • A correct activity that was recently saved in a commit might ruin the express
  • The chances are high that you miss including significant remains

Q20. Which branching strategies have you used?

Ans. To answer this Git interview question, you can share all the branching strategies that you have used. You can frame your answer as follows:

I have used –

  • Feature Branching: keeps all of the changes for a particular feature inside of a branch. 
  • Task Branching: In this, each task is implemented on its own branch with the task key included in the branch name
  • Release Branching: After the develop branch has acquired enough features for a release, that branch can be cloned to form a Release branch. This starts the next release cycle. Now you can not add new features after this point. Only bug fixes, documentation generation, etc. can be added to this branch. When it is ready to ship, the release gets merged into the master and gets a version number. 

Q21. Name some of the most popular Git repository hosting functions.

Ans. Below is the list of Git repository hosting functions:

  • Pikacode
  • Assembla
  • Visual Studio Online
  • GitHub
  • GitEnterprise
  • net
  • Beanstalk
  • CloudForge
  • GitLab
  • Planio
  • Perforce
  • Fog Creek Kiln

Q22. Why do you use Subgit?

Ans. The Subgit is a popular tool used for stress-free transferring of SVN to Git and it allows using various Git and sub-version features.

Q23. State the difference between HEAD, working tree, and index.

Ans. The working tree, also known as the working directory or workspace, is the directory tree of source files. Whereas, the index, which is also known as the staging area, lists all the files in the current branch.

HEAD is known as the last commit, which was marked in the check-out branch.

Q24. Name some Git GUI clients for Linux.

Ans. Below is the list of Git GUI clients for Linux:

Git Clients for LinuxClients for WindowsClients for MacCross-platform Git Clients
Git ForceTortoise GitGitX-devAurees
GitgGitHubGitBoxSmartGit
QGitSourcetreeGitUpGitKraken

Q25. Explain the benefits of using the Version Control System (VCS)?

Ans. The benefits of the Version Control System (VCS) are as follows: 

  • All team members can work freely on any file at any time
  • Allow us to compare files, identify differences, and merge the changes into a common version
  • Keep a track of application builds by determining which version is currently in development, QA, and production
  • Allows all team members to have a complete history of the project.

Q26. What is a Git repository?

Ans. A Git repository is a place that has a collection of files of different versions of a Project. Git stores these files either on the local repository or the remote repository. There are two types of repositories:

  • Bare Repository: contains the .git folder
  • Non-bare Repository: contains both the git index and the checked-out copy of working files

Q27. What is git instaweb? How is it used?

Ans. A git instaweb is a script that helps to set up a temporary instance of GitWeb on a web server for browsing local repositories. It requires a lightweight server such as Lighttpd or Webrick. It is used to automatically direct a web browser and run a webserver with an interface into the local repository.

Q28. Explain Git stash.

Ans. A git stash is a place where you can temporarily stash (or store) changes made to the working copy so we can work on something else, and then come back and reapply them afterward. A git stash is separate from the staging area, the working directory, or the repository.

Q29. What are the benefits of forking workflow?

Ans. The benefits of forking workflow are as follows: 

  • The contributions can be integrated without requiring everyone to push to a single central repository. 
  • Developers can push to their own server-side repositories while only the project maintainer can push to the official repository. 
  • A maintainer can accept commits from any developer without providing them write access to the official codebase.

Q30. What do you mean by the Gitflow workflow?

Ans. This is an important Git interview question. The Gitflow Workflow specifies a branching model for Git. It provides a framework for managing large projects and is mostly used for projects that have a scheduled release cycle. Gitflow assigns very specific roles to different branches and defines how and when they should interact: 

  • Master: This branch is always ready to be released on LIVE. It releases when everything is fully tested and approved. 
  • Develop: All feature branches are merged into this branch and all tests also are performed here. When everything is thoroughly checked, it can be merged into the master. 
  • Feature: Each new feature should reside in its own branch, which can be pushed to develop branch as their parent branch.
  • Hotfix: These branches are used to quickly patch production releases. They are based on master instead of develop.