Top 100 Git Questions and Answers
Top 100 Git Questions and Answers
When a developer makes a change to a code file, they can commit that change
to their local Git repository. Then, they can push their changes to a remote Git
repository, such as a server hosted by a company or an open-source project.
Other developers can then pull the changes down from the remote repository and
incorporate them into their code.
And an important thing about Git is that it is a free source and open to anyone.
1. What is Git?
Git is a version control system for tracking changes in computer files and is used
to help coordinate work among several people on a project while tracking
progress over time. In other words, it’s a tool that facilitates source code
management in software development.
Git favors both programmers and non-technical users by keeping track of their
project files. It enables multiple users to work together and handles large projects
efficiently.
This helps ensure that all team members are working on the latest version of the
file
3. What is GitHub?
GitHub
SourceForge
GitLab
Bitbucket
Local version control systems have a database that stores all file changes
under revision control on disc in a special format.
Centralized version control systems have a single repository, from which each
user receives their working copy.
Git GitHub
Git can be installed locally on the system GitHub is hosted on the web
Git repository refers to a place where all the Git files are stored. These files can
either be stored on the local repository or on the remote repository.
If you want to initialize an empty repository to a directory in Git, you need to enter
the git init command. After this command, a hidden .git folder will appear.
Git stores content in the form of metadata. SVN stored data in the form of files.
The contents of Git are hashed using the SHA-1 hash SVN doesn’t support hashed
algorithm. contents.
Git commit - Commit changes to head but not to the remote repository
12. What are the advantages of using Git?
Widespread acceptance
Pull requests
Git is a fast and reliable version control system, and the language that makes this
possible is ‘C.’
Using C language reduces the overhead of run times, which are common in high-
level languages.
The git pull origin master fetches all the changes from the master branch onto the
origin and integrates them into the local branch.
After having gone through the beginner level Git interview questions, let us now
look at intermediate GIT interview questions and answers.
The Git push command is used to push the content in a local repository to a
remote repository. After a local repository has been modified, a push is executed
to share the modifications with remote team members.
It does not integrate any of these new data into your working Downloads new data and integrate
files. it with the current working files.
hosting. All the three are services for hosting Git repositories
A Git merge conflict is an event that occurs when Git is unable to resolve the
differences in code between the two commits automatically.
Git is capable of automatically merging the changes only if the commits are on
different lines or branches.
Here are the steps that will help you resolve conflicts in Git:
The last step is to commit the changes in the file with the help of the git
commit command.
The git ls-tree command is used to list the contents of a tree object.
23. What is the process to revert a commit that has already been
pushed and made public?
There are two processes through which you can revert a commit:
1. Remove or fix the bad file in a new commit and push it to the remote
repository. Then commit it to the remote repository using:
2. Create a new commit to undo all the changes that were made in the bad
commit. Use the following command:
Git clone allows you to create a local copy of the remote GitHub repository. Once
you clone a repo, you can make edits locally in your system rather than directly in
the source files of the remote repo
Let’s say you're a developer and you want to switch branches to work on
something else. The issue is you don’t want to make commits in uncompleted
work, so you just want to get back to this point later. The solution here is the Git
stash.
Git stash takes your modified tracked files and saves it on a stack of unfinished
changes that you can reapply at any time. To go back to the work you can use
the stash pop.
27. What does the git reset --mixed and git merge --abort commands
do?
git reset --mixed is used to undo changes made in the working directory and
staging area.
git merge --abort helps stop the merge process and return back to the state
before the merging began.
The Staging Area in Git is when it starts to track and save the changes that occur
in files. These saved changes reflect in the .git directory. Staging is an
intermediate area that helps to format and review commits before their
completion.
30. How do you find a list of files that has been changed in a particular
commit?
The command to get a list of files that has been changed in a particular commit
is:
commit hash lists all the files that were changed or added in the commit.
The git config command is used to set git configuration values on a global or
local level. It alters the configuration options in your git installation. It is generally
used to set your Git email, editor, and any aliases you want to use with the git
command.
The git clean command removes the untracked files from the working directory.
SubGit is a tool that is used to migrate SVN to Git. It transforms the SVN
repositories to Git and allows you to work on both systems concurrently. It auto-
syncs the SVN with Git.
The files that were stashed and saved in the stashed index can be recovered.
The files that were untracked will be lost. Hence, it's always a good idea to stage
and commit your work or stash them.
35. Explain these commands one by one– git status, git log, git diff, git
revert <commit>, git reset <file>.
Git status - It shows the current status of the working directory and the staging
area.
Git log- It is a key tool for reviewing and reading the history of everything that
happens to a repository.
Git diff- It is a multi-purpose Git command that performs a diff function on Git
data sources when executed.
You can change the most recent commit message with the git commit —amend
command.
Git LFS is a Git extension for dealing with large and binary files in a separate Git
repository.
Git comes with a few built-in tools like Git Bash and Git GUI.
41. Will you make a new commit or amend an existing one?
The git commit —amend command allows you to easily modify the most recent
commit.
They are all names for various branches. Even Though a single git repository can
track an arbitrary number of branches, the working tree is only associated with
one of them, and HEAD points to that branch.
Git includes built-in GUI tools for committing (git-gui) and browsing (gitk), but
there are a number of third-party tools available for users seeking platform-
specific experience.
Version control enables software teams to maintain efficiency and agility while
the team grows by adding more developers
The Gitflow Workflow specifies a strict branching model centered on the project
release.
The commit object contains a tree of blob objects and other tree objects that
represent the project revision's directory structure.
They are scripts that are executed automatically whenever a specific event
occurs in a Git repository.
Git stash pop removes the (topmost, by default) stash when applied, whereas git
stash apply keeps it in the stash list for future use.
54. Explain git reflog This command is used by Git to record changes
made to the branches' tips.
It is the storage place of the metadata and object database of the project.
Edit the files to resolve any incompatible changes first, then use "git add" to add
the corrected files and "git commit" to save the repaired merge.
In GIT, the best way to create a branch is to have one'main' branch and then
another branch for implementing the changes that we want to make.
If you want to add an empty repository to a directory in Git, use the git init
command.
It is used to launch a web browser and a webserver with an interface into a local
repository automatically.
It represents a tree object with each item's mode and name included.
63. What exactly is git cherry-pick?
64. State the difference between “git remote” and “got clone”?
“Git remote” allows you to create an entry in the git configuration which specify a
URL.
“Git clone” lets you create a new git repository by letting you copy it from
the current URL.
“Pull request” is done when you feel like changing the developer’s change to
another person's code branch. And “Branch” is just a separate version of code.
66. How might you recover a branch that has previously pushed
changes in the main repository yet has been coincidentally erased from
each team member's local machines?
We can easily recover this by seeing the latest commit of the branch in the reflog
and then going through the new branch.
Detach head refers to that the currently checked repository is not in the local
branch.
68. What command helps us to know the branches merged into master
and which are not?
git branch - -merged lets us get the lost of the branches which are currently
merged into the current branch
git branch - - no- merged shows the branches which are not merged
69. Is LDAP Authentication Supported?
GitLab API only supports LDAP authentication since version 6.0 and higher.
Now let’s raise the level of difficulty with advanced Git interview questions and
answers.
Interested to learn more about Git? Check out the DevOps Engineer Master's
Program and get certified today.
There are many functions that a Git repository hosting service can provide. Some
of the most common and useful functions include:
75. What is the difference between Git and other revision control
systems?
Git is a distributed revision control system, which means that it can be used
without a central server. This allows for a great deal of flexibility in how
projects are managed.
On the other side, revision control systems are often centralized, which can
limit the flexibility of how projects are managed.
Git works by tracking changes to files in a repository. When a file is changed, Git
calculates a unique identifier for the change, called a "commit hash". The commit
hash allows Git to identify the change and track it over time.
Installing Git is simple. Just download the latest version from the Git website
(https://git-scm.com/).
To use Git, a developer first creates a local repository on their computer. This
repository contains all the files for a project and the history of all the changes
made to those files.
Or just follow the instructions in the Git documentation (https://git-scm.com/doc).
One of the main drawbacks is that it can be difficult to learn and use,
especially for those who are not familiar with version control systems.
Some of the most important Git commands are "commit", "push", and "pull".
And the "push" command is used to send changes to the remote repository.
Then the "pull" command is used to retrieve changes from the remote
repository.
Some of the most important features of Git are its distributed nature, its ability to
track changes, and its support for branches.
The ability to track changes helps developers to keep track of their work and
revert to previous versions if necessary.
The support for branches allows developers to experiment with new features
without affecting the main codebase.
Conflict in Git occurs when two or more developers have made changes to the
same part of a file, and those changes can't be automatically merged. When this
happens, Git will mark the file as conflicted and leave it up to the developers to
resolve the conflict.
Resolving a conflict can be done by manually editing the file to choose which
changes should be kept, or by using a tool like Git's merge command to
automatically merge the changes.
The git status command is used to obtain the current state of a Git repository.
This command can be used to determine whether the repository is clean or dirty,
and to see which files have been modified. The git status command will also
show which branch is currently checked out and whether there are any
uncommitted changes.
There are many reasons that Git is considered an easy tool to work with.
Git is highly flexible and can be easily customized to fit the needs of any
project.
And, Git is very stable and reliable, so users can trust that their work will be
safe and sound.
And Git Stash takes a snapshot of your changes and stores them away for later
use.
88. What differentiates between the commands git remote and git
clone?
The main difference between the git remote and git clone commands is that the
git remote adds a remote repository as a shortcut to your current repository,
while the git clone creates an entirely new copy of a remote repository.
89. Tell me the difference between git pull and git fetch?
Both of these commands will fetch any new commits from the remote repository,
but they differ in how they handle these commits.
Git pull will merge the remote commits into the current branch, while git fetch will
simply retrieve the commits and store them in the local repository. This means
that if you have any uncommitted changes, git pull may result in merge conflicts,
while git fetch will not.
Git is a version control system that lets you track changes to your code.
GitHub is a hosting service for Git repositories. You can use GitHub to store
your code remotely, or you can use it to collaborate with other developers on a
project.
Git reflog is a history of all the changes made to a git repository. It is a valuable
tool for debugging and troubleshooting purposes.
And Git reflog can be used to view the history of a repository, see who made
what changes, and when those changes were made.
A detached HEAD is a state where the HEAD pointer is not pointing to the
current commit. This can happen if you check out a commit that is not the most
recent, or if you reset your head to a previous commit.
The first is to simply commit your changes before switching branches. This will
ensure that your changes are saved to a specific branch, and you won't have
to worry about them being lost when you switch branches.
Another way to avoid detached HEAD is to use the "git checkout" command
with the "-b" option.
To resolve a conflict in Git, you will need to first identify the source of the conflict.
Once you have identified the source of the conflict, you can use the "git pull"
command. This will pull the latest changes from the remote repository and merge
them with your local copy.
If the "git pull" command doesn't resolve the conflict, you can try the "git merge"
command. This will merge the two versions of the code manually. You will need
to resolve the conflicts manually and then commit the merged code.
Subgit is a tool for managing Git repositories with Subversion history. It allows
you to keep your existing subversion history while moving to Git, and it also
provides a way to keep your Git history synchronized with subversion.
There are many reasons that you might want to use Subgit,
For example, if you have a large subversion repository with a lot of history,
moving to Git can be a huge undertaking; that's where Subgit can help
transition smoother.
96. Explain the different points when a merge can enter a conflicted
stage.
There are two stages when a merge can enter a conflicted stage.
If there are changes in the working directory of the stage area in the current
project, the merge will fail to start. In this case, conflicts happen due to pending
changes that need to be stabilized using different Git commands.
The failure during the merge process indicates that there’s a conflict between the
local branch and the branch being merged. In this case, Git resolves as much as
possible, but some things have to be fixed manually in the conflicted files.
97. What has to be run to squash the last N commits into a single
commit?
In Git, squashing commits means combining two or more commits into one.
Use the below command to write a new commit message from the beginning.
But, if you want to edit a new commit message and add the existing commit
messages, then you must extract the messages and pass them to Git commit.
The below command will help you achieve this:
git reset -soft HEAD~N &&git commit -edit -m“$(git log -format=%B -reverse
.HEAD@{N})”
Git merge is used to incorporate new commits into your feature branch.
Git merge creates an extra merge commit every time you need to incorporate
changes.
As an alternative to merging, you can rebase the feature branch into master.
Git rebase Incorporates all the new commits in the master branch.
It rewrites the project history by creating brand new commits for each commit
in the original branch
101. How do you recover a deleted branch that was not merged?
To recover a deleted branch, first, you can use the git reflog command. It will list
the local recorded logs for all the references. Then, you can identify the history
stamp and recover it using the git checkout command.
The Git stash drop command is used to remove a particular stash. If there’s a
stash you're no longer using or you want to remove a specific item of stash from
the list, you can use the stash commands.
Let’s say you want to delete an item named stash@{abc}; you can use the
command:
Reverting Resetting
104. How can you discover if a branch has already been merged or not?
git branch --merged - Returns the list of branches that have been merged into the
current branch.
git branch --no-merged - Returns the list of branches that have not been merged.
The command git cherry-pick enables you to pick up commits from a branch
within a repository and apply it to another branch. This command is useful to
undo changes when any commit is accidentally made to the wrong branch. Then,
you can switch to the correct branch and use this command to cherry-pick the
commit.
Transform your DevOps career and learn the science of improving the
operational and developmental activities by choosing our PGP in
DevOps. Contact our admission counselor today and grab your seat!
By now you should have an idea of the Git interview questions that could be
asked and what possible answers should be given. However, if you wish to bag
the job like a pro, you can enroll in Simplilearn's DevOps Certification Training
Course that teaches you about the tools and techniques you need to bridge the
gap between software developers and operations. You’ll become an expert in the
principles of continuous development and deployment, automation of
configuration management, inter-team collaboration, and IT service agility, using
modern DevOps tools such as Git, Docker, Jenkins, Puppet, and Nagios.