Interview-Questions-Img

Top 44 Git Interview Questions and Answers in 2024

If you're preparing for a Git-related job interview or simply looking to deepen your understanding of this powerful version control system, you've come to the right place. In today's software development landscape, Git has become an indispensable tool for managing and tracking changes in code repositories. Employers and companies across various industries often look for candidates with strong Git skills, making it crucial to be well-prepared for Git interview questions.

Here, we have compiled a list of top Git interview questions and answers that cover a wide range of topics, from basic concepts to advanced techniques. You'll gain a solid understanding of Git's fundamental principles, as well as the practical aspects of using Git in real-world scenarios. 

If you're new to Git or need a refresher on its core concepts and commands, check out our free Git tutorial. It provides a step-by-step introduction to Git, starting with the basics and gradually progressing to more advanced topics. 

In addition to mastering Git, expanding your web development skills can greatly enhance your career prospects. To help you further upskill, it would be great to go for the best web development course.

Interview Questions

Git is one of the most popular source code management tools. As an open-distributed version control system, it allows developers to track codebase changes and collaborate on projects with other developers. It is typically used for software development, favouring technical and non-technical users by keeping track of their project files. 

Whenever developers change their code file, they commit that change to their local Git repository. Later, they push their changes to a remote Git repository. Other developers can pull those changes from the remote repository and include them in their code. It enables users to work together and handle large projects efficiently.

A Git VCS keeps track of the developers' contributions working as a team on the projects. They maintain the history of the changes done in code, and project evaluation allows developers to add new code, fix bugs, and run tests with the previous code copy that could be restored at any moment if things go wrong at any point. 

Types of Git VCS are:

  • Local version control systems: These have a database that maintains all the changes in the file on disk under revision control in a specific format.

  • Centralized version control systems: These contain one repository; each user gets a working copy.

  • Distributed version control systems: These contain multiple repositories, each accessible to separate users with their working copy.

Git, an open-source version control system, manages the source code history. It handles projects of all sizes efficiently. 

GitHub is a cloud-based hosting service managing Git repositories. It helps manage open-source projects and offers distributed version control and source code management functionality in Git.

Here's a tabular comparison showing the differences between Git and GitHub:

 

Git

GitHub

Definition

Distributed version control system

Web-based Git repository hosting

Function

Manages version control locally

Hosts Git repositories online

Ownership

Developed by Linus Torvalds

Owned by Microsoft

Working

Works offline

Requires an internet connection

Collaboration

Supports local and remote collaboration

Facilitates remote collaboration, code reviews, and pull requests

Features

Branching, merging, and version control

Issue tracking, project management, code review, and collaboration features

Hosting

Self-hosted on developer's machine

Hosted on GitHub servers

Access Control

Limited access control capabilities

Granular access control with permissions and roles

Pricing

Open-source and free to use

Offers both free and paid plans

Integration

Can be integrated with various tools and services

Integrates with CI/CD pipelines, project management tools, and third-party integrations

Popularity

Widely used in software development

Dominant platform for open-source projects and collaborative development

 

A Git repository is a file structure that stores all the project-based files. Git either keeps the files on a local or a remote repository. 

Popular Git hosting services are:

GitHub

GitHub is one of the most popular Git hosting platforms. It offers a wide range of features such as code hosting, version control, issue tracking, collaboration tools, and integration with various development workflows.

GitLab: 

GitLab is another widely used Git hosting service that provides both cloud-based and self-hosted options. It offers similar features to GitHub, including code hosting, version control, CI/CD pipelines, and project management tools.

Bitbucket: 

Bitbucket is a Git-based hosting service owned by Atlassian. It supports both Git and Mercurial repositories and offers features like code collaboration, pull requests, and continuous integration.

Azure DevOps: 

Azure DevOps, formerly known as Visual Studio Team Services (VSTS) and Team Foundation Server (TFS), is a comprehensive set of development tools offered by Microsoft. It includes Git hosting, version control, project management, and CI/CD capabilities.

AWS CodeCommit: 

AWS CodeCommit is a fully managed source control service provided by Amazon Web Services (AWS). It supports Git repositories and integrates well with other AWS services, making it suitable for cloud-based development projects.

To create a Git repository, you can follow these steps:

  • Open a terminal or command prompt on your local machine.

  • Navigate to the directory where you want to create the repository. You can use the cd command to change directories.

  • Once you are in the desired directory, initialize a new Git repository by running the following command:

git init

  • This will create a new, empty Git repository in the current directory.

  • You can now start adding files to your repository. Place the files you want to include in the repository into the current directory or create new files using a text editor.

  • Once you have added or modified the files, you can stage them for committing. Use the following command to add all the files in the current directory to the staging area:

git add 

Alternatively, you can specify individual files or directories to add.

  • Commit the staged changes by running the following command:

git commit -m "Initial commit"

Replace "Initial commit" with a descriptive message that explains the changes you made.

  • You have created a Git repository. You can now start working with branches, pushing changes to remote repositories, and more. Optionally, you can connect your local repository to a remote repository hosted on a platform like GitHub or GitLab.

To write a commit message ‘git commit -a’ command is used, where -a instructs git to commit the new content of all the modified tracked files. If new files are to be committed for the first time, you can use ‘git add <filename>’ before git commit -a.

Git itself is primarily implemented in the C language. It was designed to be fast, efficient, and able to run on a wide range of platforms. The core functionality and most of the commands in Git are written in C for performance reasons. 

However, Git also provides a command-line interface and supports various scripting languages such as Bash, Perl, Python, and more. Additionally, Git can be integrated and utilized within projects developed in different programming languages.

The primary purpose of Git is to provide a distributed version control system for tracking changes in source code during software development. 

Here are some key purposes and benefits of using Git:

  • Version Control
  • Collaboration
  • Code Management
  • Branching and Merging
  • Remote Collaboration
  • Backup and Recovery
  • Open Source Community

The git clone command is used to create a copy of an existing Git repository, including all of its files, branches, commit history, and configuration. It allows you to create a local working copy of a remote repository on your own machine.

When you run git clone, you specify the URL or file path of the repository you want to clone, and Git fetches all the necessary data from that repository and creates a new directory on your local machine with the same name as the remote repository. The new directory will contain a complete copy of the remote repository, including all the files and commit history.

Here's the general syntax for the git clone command:

git clone <repository URL or path>

For example, to clone a repository hosted on GitHub, you would use:

git clone https://github.com/username/repository.git

To initiate an empty repository to a Git directory, enter the git init command. After this command, a hidden .git folder appears.

Git

SVN

It is a decentralised version control tool 

It is a centralised version control tool

It is the 3rd generation of version control tools

It is 2nd generation of version control tools

Clients can clone entire repositories on local systems

Version history is stored on a server-side repository

Offline commits are possible

Only online commits are possible

Push/pull operations are faster

Push/pull operations are slow

Works are shared automatically by commit

Nothing is shared automatically

The benefits of using Git are:

  • Any project can use GIT

  • Superior disk utilisation and network performance

  • High availability

  • Collaboration friendly

  • Data redundancy and replication

  • Only one git directory per repository

  • It is a more much applicable service, etc.

The command ‘git push’ updates remotes refs with related objects.

The ‘git config’ command defines the behaviour of the repository, user information and their preferences, git installation-based configuration etc. It is used to set git configuration values on a local or global level. Further, it sets the git email, editor, and any aliases the user wants to use with the git command.

  • Git init: Initialise an empty git repository

  • Git config: Configures the username and email id

  • Git diff: View the changes made to the file

  • Git commit: Commit changes to the head but not to the remote repository

  • Git add: Add files to the staging area.

When making the commits, we can change, format and review them in the intermediate area known as the Staging Area or Index.

This command fetches and downloads content from the remote repository and integrates it with the local one.

Git branching and merging are key concepts that allow for effective collaboration and version control in software development. 

Git Branching:

In Git, branching involves creating independent lines of development within a repository. It allows multiple developers to work on different features or fixes simultaneously without interfering with each other's code. When a branch is created, it becomes a separate pointer to a specific commit in the Git history.

Each branch represents a different timeline or path of development, enabling developers to make changes, experiment, and work on new features without affecting the main codebase. Branches can be created for specific tasks, bug fixes, or feature development.

Git Merging:

Merging in Git is the process of integrating changes from one branch into another, typically combining the changes made in a feature branch back into the main branch (often called the "master" branch). Merging brings together the independent lines of development, incorporating the changes made in the branch being merged into the target branch.

When merging, Git automatically combines the changes made in both branches. If there are conflicting changes (i.e., modifications to the same lines of code), Git will prompt the developer to resolve the conflicts manually. Once the conflicts are resolved, the merge is completed, and the changes from the branch being merged are now part of the target branch.

Command 'git help -a' gives a list of subcommands. On the cmd, using the 'git version' command, it will show the git version installed. 

The command 'git help' displays the list of commands supported by the git. With the 'git help <commandname>', we can get detailed information about specific git commands.

Git is an open-source version control system. It allows you to run versions of a project, which show the changes made to the code over time. Also, it will enable you to retract if necessary and undo those changes.  Multiple developers can checkout and upload changes attributed to a specific developer.

The conflict occurs whenever the commit to be combined changes in one place, and the current code also changes in the same place. To resolve a conflict, edit the list to fix the different changes, and then run ‘git add’ to add the resolved directory; later, run the 'git commit' to commit the repaired merge.

A head is a reference to the last commit object of a branch. Every repository has a default head called ‘master’ or ‘main’. There's no restriction to the count of heads; it means a repository can have any number of heads.

  • Use git checkout HEAD~1 - to go or check out to 1 commit before the latest commit.

  • If you want to uncommit the last 3 commits without losing the changes, run git reset HEAD~3. You can see the changes made in the previous 3 commits, update it manually, and commit it finally.

  • To uncommit the last 3 commits and remove the changes, run the command git reset – hard HEAD~3. This command altogether removes all the changes.

  • Run git diff HEAD~3 to examine the changes made in the last 3 commits.

  • Run git revert –no-commit HEAD~3…HEAD command so as to make a new commit by reverting the last 3 commits.

git commit -m "n files created" is the syntax to add a message to a commit.

Git Fetch

Git Pull

It only downloads new data from a remote repository.

It updates the current HEAD branch with the latest changes from the remote server.

Doesn’t integrate any of the new data into working files

Downloads new data and integrates with the current working files

Command- git fetch origin

                   git fetch –all

Tries to merge remote changes with local ones. 

Command- git pull origin master

To create a branch in Git, have one ‘main’ branch and then make another branch to implement the changes. This is useful when various developers are working on a single project.

To create a new feature in the main branch, use the command ‘git merge’ or ‘git pull’.

git pull --rebase allows you to distort your commits to a few or one commits later. If you have merged in your unpushed history, it is not so easy to do a git rebase afterwards.

If the git status command displays 'xyz.txt' as a file pending commit, then

1. To unstage an uncommitted file that is in the staging stage command 'git reset <file-name>' is used. After using the 'git reset HEAD xyz.txt' command, the 'git status' command will not show xyz.txt in the list of files pending commit; now it will be displayed under the list of the unstaged or untracked files.

2. To discard the changes 'xyz.txt' in the working directory, the command 'git checkout -- <filename> is used. After using the 'git checkout -- xyz.txt' command, the 'git status' command will not show xyz.txt in the list of unstaged/untracked files as this file 'xyz.txt' will be replaced with the last committed version from the repository.

  • Git branch – merged master – It shows all merged branches into master.
  • Git branch – merged – It shows all merged branches into the head.
  • Git branch – no-merged – It shows all the branches that are not merged.

‘git pull origin master' allows submits from the master branch of the source remote, i.e. into the local origin or master branch, and combines the origin or master branch into the branch you currently have looked out for.

Git has helped developers gain many advantages in efficiently performing the development process.

Some of the main features of git are-

  • Branching capability- Developers working on different project features can efficiently work on multiple branches. It also has an easier merge option and an efficient workflow feature diagram for tracking it.

  • Pull request feature- This feature helps in easier interaction amongst team developers to coordinate merge operations. It keeps proper track of the changes done by developers to the code.

  • Distributed manner of development- Since Git is a distributed system, making it is easier to trace and locate lost data from the main server. Here, the developer gets a repository file on the server and a copy of this is also stored in a local repository. This improves the scalability of the project.

  • Effective release cycle- Due to the wide variety of features, Git helps to increase the release cycle's speed and improve the project workflow efficiently.

There are two processes to revert a commit:

1. Remove or fix the wrong file in a new commit. Later push the file to the remote repository and then commit it using- git commit –m “commit message”

2. You can create a new commit to undo all the changes in the wrong commit by using the command- git revert <commit id>.

Git reset modifies the state of the branch to a previous one by removing all the states after the desired commit. Whereas Git revert creates new reverting commits, keeping the original one intact.

Git stash considers the present state of the working file and index, puts it on the stack for the next and gives you back a clean working file. If you are in the middle of an object and want to jump over to the other task, and at the same time you don't want to lose your current edits, you can use GIT stash.

When you finish the stashed item or want to remove it from the list, run the git command ‘stash drop’.  This removes the last added stash item by default and a specific item if you include it as an argument.

Describe branching systems.

  • Feature Branching 

A component branch model keeps most changes for a specific element within a branch. When the item is tested and approved by automated tests, the branch converges into the master. 

  • Task Branching

Here, each assignment appears on its branch with the undertaking key included in the branch name, and it is difficult to see which code actualises which task; search for the task key in the branch name.

  • Release Branching

Once the created branch obtains enough features for a discharge, you can clone that branch to frame a Release branch. This branch begins the following discharge cycle so that no new features are included after this. 

Bug fixes, documentation age, and other release-oriented assignments goes into this branch. When ready to deliver, the release gets merged into the master and labelled with a form number. Additionally, it should be merged again into creating a branch, which may have advanced since the release started.

Push request is used when the target repository requests to push the changes to it. At the same time, the pull request is used when the repo requests the target repository to pull the changes from it.

Use the command git diff-tree -r {commit hash} to get a list of the changed files. Here, the -r flag allows the command to list individual files. The commit hash lists all the files that were changed or added files in the commit.

  • Parent object commit references
  • A set of files that represent the project state at a given point of time 
  • 40-character string identifying the commit object- SHAI name.

  • Rebasing is another option for merging in git. The syntax used for rebase is ‘git rebase [new-commit]’.

The command ‘git diff’ shows the changes between commits, commit and working tree. The ‘git status’ shows the difference between the working directory and the index, which helps understand Git in detail.

Some of the reasons behind it are-

1. The amend operation destroys the previously committed state, but if only the commit message is changed, there’s no issue. However, if contents are amended, there are chances of deleting some necessary kinds of stuff.

2. Abusing ‘git commit-amend’ results in the growth of a small acquiring varying changes.

One can begin to work in Git in the following ways-

  1. Create a new repository using the ‘git init’ command if it's a new project.

  2. In case of working on an existing repository, use the command ‘git clone’.

Explain ‘git is-tree’.

The ‘git is-tree’ represents a tree object consisting of the mode and the names of each item, including the SHA-1 value of the tree or the blob.

The stashed and saved files in the stash index are recovered; however, any untracked files are lost.  Always stage and commit your work or stash them. To fetch the log references of a specific branch, use the ‘git reflog <ref_name>’ command.

  • Help customise the internal behaviour of Git. Git hooks are scripts running automatically on the event occurrence in Git. These are used for workflow automation.

  • Used to enforce a Git commit policy.

  • Rebasing is moving a branch to a new base commit, like rewriting the history of a branch.

  • Helps to maintain linear project history.

  • Once the commits are pushed to a public repository, using Rebasing is not a good practice.

  • Never use ‘git rebase’ on public branches. This is because if others use the same branch, they may get confused by the changes in the Master branch after GIT rebasing

There are several popular GUI (Graphical User Interface) tools available for working with Git. These tools provide a more user-friendly and visual interface compared to using the command line. 

GitKraken: 

GitKraken is a cross-platform Git GUI client that offers a visually appealing and intuitive interface. It provides features such as a visual commit history graph, drag-and-drop Git actions, seamless integration with popular Git hosting services, and built-in merge conflict resolution tools.

Sourcetree: 

Sourcetree, developed by Atlassian, is a free Git GUI client available for Windows and macOS. It offers a clear and easy-to-use interface with features like visual commit history, branch management, and built-in Git flow support.

GitHub Desktop: 

GitHub Desktop is an official GUI client provided by GitHub. It simplifies the process of working with Git repositories and integrates well with GitHub's features. It offers features such as branch management, visual diff tools, and easy repository cloning.

GitExtensions: 

GitExtensions is a Git GUI client for Windows that provides a comprehensive set of features. It includes options for visualizing the commit history, managing branches, resolving conflicts, and integrating with external diff tools.

GitAhead: 

GitAhead is a lightweight and fast Git client available for Windows, macOS, and Linux. It offers an intuitive interface with features like visual commit browsing, staging area management, and simplified conflict resolution.