100 Core Git Interview Questions: 1. What Is and Why Is It Used?
100 Core Git Interview Questions: 1. What Is and Why Is It Used?
🟣 Git interview questions and answers to help you prepare for your next technical interview in
2024.
devinterview.io/
Star Notifications
README
You can also find all 100 answers here 👉 Devinterview.io - Git
https://github.com/Devinterview-io/git-interview-questions 1/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Security and Integrity: Each file in Git has a unique SHA-1 hash, which means
unauthorized changes are easily detected.
Code Reviews: Git simplifies the process of reviewing code before it's incorporated into
the main codebase.
Key Concepts
Commit
A commit represents a saved state of your project. It is a snapshot of all the files in your
project at a given point in time. Each commit has a commit message associated with it.
Branch
Master is a default branch present in all Git repositories. You can create many other branches.
Merge
Merging in Git means taking the independent developments of two branches and combining
them into a single branch.
After merging, the two branches from which you were merging are the same as the merged
branch.
A remote is a common repository on a server or accessible memory that all team members
use to exchange their changes.
https://github.com/Devinterview-io/git-interview-questions 2/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
For everyday tasks, both CLI and GUI are powerful, but it's essential to understand Git's
underlying mechanics so that you can troubleshoot and manage complex tasks efficiently.
Beginner Tips
Make frequent commits.
Write clear and concise commit messages.
Push your code to a remote repository often, especially before taking a break or
finishing a task.
Unique Features
Distributed Model: Git decentralized nature empowers every set of files with a complete
repository history and enables full-fledged operations without network connectivity. This
stands in contrast to centralized systems that require a constant network connection and
utilize a single centralized server for all project data.
Quick Operations: Git is renowned for its fast and efficient operations. Unlike older VCS,
which often performed slowly or required substantial server interactions, Git uses local
operations, resulting in impressive speed.
Data Integrity: Git ensures the cryptographic integrity of every piece of data, securing
against file corruption, loss, or unauthorized alterations. The absence of this feature in
some traditional VCS can pose data integrity risks.
Streamlined Branching: While segments or branches in older VCS were often used for
parallel development, Git branches are lightweight and designed for quick and frequent
usage.
Customizable Workflows: Git permits versatile workflows due to its distributed nature
and the ability to manipulate the commit history before sharing changes with others.
Staging Area (Index): Before committing, developers use the "staging area" to
selectively include specific changes in a commit rather than committing every altered file.
https://github.com/Devinterview-io/git-interview-questions 3/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Efficiency and Performance: Git's rapid and streamlined processes lead to optimized
efficiency.
Atomic Commits: Every change is distinct and exclusive from others, enabling precise
control over data synchronization.
Risk of Single Point of Failure: If the central server is compromised, it poses a threat to
the entire project's data.
Limited Offline Functionality: Users are hindered by the necessity of a steady network
connection.
https://github.com/Devinterview-io/git-interview-questions 4/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Stale Branches: Centralized repositories can have stale branches, hindering collaboration.
Git
Git is a distributed version control system that manages code and tracks its changes. It's
mainly a local solution, only allowing developers to coordinate directly.
Key Features
Local Management: Developers can work on their repositories without the need for a
central server.
Speed: Operations such as committing, branching, and merging are rapid, ideal for local
development.
Staging Area: Changes are first staged before getting committed for better control.
Content Tracking: Rather than file-based, Git is content-based, ensuring efficient
tracking.
GitHub
GitHub, on the other hand, is a web-based platform that provides hosting for Git
repositories. It offers features that extend beyond Git's core functionality.
Key Features
Remote Repositories: It allows developers to sync their local repositories with remote
ones, enabling collaborative work.
Code Hosting: Developers can store and manage their code from anywhere, not just
their local machines.
Collaboration Tools: GitHub provides tools for project management, issue tracking, and
team collaboration.
Code Review: Multiple users can review and discuss proposed changes before they're
merged into the main codebase.
Access Control: Different users can have varying levels of access to a repository or
organization.
https://github.com/Devinterview-io/git-interview-questions 5/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Every Git repository has two primary parts: the working tree and the hidden .git directory,
where Git keeps all of its metadata for the repository.
Key Features
Distributed: Each contributor to the project has their own copy of the repository,
including its complete history and version-tracking capability. This enables offline work
and serves as a backup for the project.
Efficient Storage: Git optimizes storage using techniques such as file hashing and delta
compression, emphasizing minimal redundancy and data redundancy elimination.
Content Integrity: All files in a Git repository, as well as their versions, are checksummed
and verified on every Git operation for data integrity.
Work Tracking: Git allows for tracking changes made to the project over time, helping
individuals or teams understand the evolution of the project, who made specific
changes, and why.
.git Directory
The .git directory is the control center of a Git repository, housing everything Git needs to
manage the repository and its history. It includes:
The object database that hosts all the data about commits, trees, and blobs.
The references directory maintaining pointers to specific commits (e.g., branches and
tags).
Configuration files detailing the repository's settings and attributes.
Working Tree
The working tree is a directory where tracked files are extracted for viewing and editing. Any
changes made to files in the working tree can be staged and committed to the repository to
capture the changes.
https://github.com/Devinterview-io/git-interview-questions 6/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Untracked files, i.e., files not previously staged or committed, coexist with the working tree
and reside outside the Git management.
# Now, we can simulate the construction of our working tree using a dictionary
working_tree = {
'folder1': {
'file1.py': 'print("Hello, World!")'
},
'folder2': {
'file2.py': 'print(2 + 2)'
}
}
https://github.com/Devinterview-io/git-interview-questions 7/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Working Directory: Refers to the current, live set of files and directories. This is the
version of the project that you actively work on and modify.
Staging Area (also known as the "index"): Serves as a buffer zone. Files in this area are
marked for inclusion in the next commit. You control what content moves from the
working directory to the staging area.
Commit History (in the Git directory): Represents the timeline of commits in the form of
a versioned archive. It also houses the full content of the project at relevant commit
points.
Creating Commits: You start by making changes to your working directory. When you're
ready to record these changes, you add specific files from the working directory to the
staging area. With the desired changes in the staging area, the next command captures
the staging area's content and creates a new commit in the commit history.
Browsing History: Git's commit history is time-ordered, typically from the most recent
commit backward. Each commit influences how the project appears at a given point in
time.
Practical Applications
Code Reversion: You can reset a project to an earlier commit, effectively reversing
changes.
Collaboration: Commits facilitate collaboration by enabling team members to
understand changes and when they were made.
Code Review: Before incorporating changes into the primary branch, commits offer a
structured way for team members to review each other's code. This process ensures that
new code meets the repository's standards.
Working Directory
Staging Area
Repository
Core Concepts
Working Directory
The working directory is your playground. It's your space for experimenting, where you can
edit files, merge different versions, and even create new ones.
The staging area (also known as the Index) is a kind of middle-ground. Here you can make
fine adjustments to what changes will be included in your next commit. You have two types
of files here:
MDS: Files that have been modified since your last commit
SML: Files that have been changed and prepared for your next commit
When you're happy with your changes in the staging area, you "stage" them. Staging in this
context is like flagging certain parts of your project that you want to be saved in your next
commit. Notice that this step is completely optional.
Repository (History)
https://github.com/Devinterview-io/git-interview-questions 9/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
The repository is the final tier, dealing with the preservation of your work. Think of it as the
local repository that serves as the safe house for all your commits. It keeps track of all
changes and commits you've made, ensuring that you can still retrieve them even after major
updates.
The repository is also broken down into three apparent "trees" - the Working Directory, the
Staging Area, and the last one, the HEAD. The HEAD points to your most recent commit.
These three trees can seem confusing at first. But really, they're straightforward to
understand - they represent the current status of your project, changes that you're planning
to commit, and the commits that you've confirmed you want to save in the future.
Key Concepts
Master/Trunk/Branch: The primary branch serving as the project source.
Merging: The process of integrating changes from one branch into another. For
instance, integrating finished feature branches into the Master.
Conflict Resolution: The act of resolving overlapping changes made to the codebase
across different branches.
3. Risk Mitigation: Changes are kept separate until they are thoroughly tested. If an
experimental feature doesn't work as expected or introduces bugs, it won't affect the
more stable Master branch.
4. Staging Area: A dedicated space for integration and testing before code is deployed or
merged into the Master branch.
https://github.com/Devinterview-io/git-interview-questions 10/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
5. Feature Toggles: Streamlines the process of selectively deploying new features for
testing.
6. Code Reviews: Assists in code quality checks before merging, ensuring the Master
branch remains stable.
7. Task Management: Aligns with project management tools and methodologies, providing
a way to structure tasks in a version-controllable manner.
8. Release Management: Facilitates the isolation and verification of changes earmarked for
specific releases.
In Git, the concept of a working directory, the staging area (or index), and the repository (or
commit history) are all managed by the HEAD .
Point of Reference: HEAD identifies the current snapshot/commit in the history tree. Any
actions in the working directory or staging area are based on this commit.
Connector between Levels: It links the most recent commit in a branch to both the
staging area and the working directory. Each commit is precisely what HEAD points to.
https://github.com/Devinterview-io/git-interview-questions 11/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
# Verify the new `HEAD` after the commit
git log --oneline --decorate
The git log command in the above code includes the --decorate option, displaying the
most recent commit and any associated references.
Key Attributes
Two-Part Process: Cloning involves duplicating the repository and setting up the local
environment.
Full Version History: The cloned repository typically includes all commits and file
versions.
Remote Linkage: A relationship is built with a designated remote for data
synchronization.
Terminology Overview
Local Repository: Storage on the user's machine where all project and version data
reside.
Remote Repository: An external, shared storage often on a server, accessible for
collaborative work.
Working Directory: The location on the user's file system where files are manipulated
and changes are tracked for commits.
Core Functionality
Duplication: The clone process reproduces the entire project, with all branches and
commits, onto the user's machine. This offers a complete, standalone history.
Configuration: The local environment is configured to maintain synchronization with a
specific remote repository. By default, the repository from which the clone is made
(origin) is set as the primary remote, but additional remotes can be added if required.
Integrity: Git ensures the integrity of the copied data, such as commits and file
snapshots, during the cloning process.
https://github.com/Devinterview-io/git-interview-questions 12/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Selective History Contraints: While it's common for a clone to receive the entire
repository history, limited cloning is also possible, especially with large repositories.
Technique like shallow cloning or specifying a certain commit for cloning can be used.
For GitHub, cloning often uses the HTTPS mode. Upon updates to the remote, the local
repository can be synchronized using standard Git commands (e.g., fetch, pull, push).
Technically, clonality is not tied to a specific remote URL; it's more about aligning the
histories. For instance, after cloning from GitHub using HTTPS, remotes can be reconfigured
to use SSH.
Detailed Process
1. Acquiring Data: The clone command contacts the remote repository, retrieves its history
and files, and then saves this snapshot locally.
2. Repository Creation: The git clone execution sets up a new repository, employing the
data fetched from the target remote, and thus creates both the working directory and
associated .git directory, which hosts the entire Git control structure.
4. Extraction: The clone accomplishes the creation and association of the local repository,
which users can then "extract" to their working directories for manipulation.
Technical Commands
Clone: Initiate the cloning process.
https://github.com/Devinterview-io/git-interview-questions 13/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
git remote -v
Repository
The repository houses the following key components:
Object Database: Persistently stores snapshots of files and directories as objects, which
can be further classified into:
Index/Stage: A dynamic workspace that mirrors the next commit's expected state.
Head Pointer: A reference to the latest commit in your current working branch
https://github.com/Devinterview-io/git-interview-questions 14/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
2. Trees: SHA-1 hash combines the file and directory contents within—changes in any file
or sub-directory would trigger a new hash.
3. Commits: SHA-1 hash incorporates the tree representing the project's directory
structure, along with parent commit(s), commit message, and author information.
Let's assume a file's content changes and you add the updated file to the staging area.
The new version of the file is then stored as a Blob object, and the associated Tree object
also gets updated.
During the following commit, a new Commit object is created, which references the
updated Tree object.
Simultaneously, the Branch you're working on moves forward to reference this new
commit.
This systematic approach assures the integrity of your project's history. Since any alteration in
content, directory structure, or commit details will trigger a change in the SHA-1 hash of the
related object(s), a change can be immediately recognized.
git init
https://github.com/Devinterview-io/git-interview-questions 15/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Most Git GUI applications, like Sourcetree or GitKraken, facilitate setting up repositories
through an intuitive interface. Once you've navigated to your project folder, choose the
"Initialize/Create New Repository" option.
Use the following command to add files or directories to the staging area:
Committing Changes
Once files are in the staging area, use this command to commit these changes:
If you plan to push your local repository to an online service like GitHub, first link your local
repository to a remote one:
Pushing to Remote
Finally, use this command to push your local repository to the remote one:
After the initial "upstream" link is established, subsequent push commands can be executed
with a simple git push .
https://github.com/Devinterview-io/git-interview-questions 16/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
In particular, avoid using files with such sensitive information before creating a .gitignore .
git status is an essential command for tracking changes in a Git repository. It provides
important details such as file states, current working branch, untracked files, and more.
Changes not staged for commit: Highlights modified files that were not yet staged for
the next commit.
Untracked Files: Recognizes files in the working directory that are not a part of the
repository.
Branch is Up to Date: Provides this affirmation when the working branch is up-to-date
with its remote counterpart.
Your Branch is Ahead of 'origin/master' by 1 Commit: Notifies when the current local
branch outpaces the linked remote branch by a set number of commits. This is common
in team-based workflows and signifies the necessity of a push .
Note: The remote branch in the example is 'origin/master', and the number of commits
ahead is 1.
https://github.com/Devinterview-io/git-interview-questions 17/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: app.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
server.js
no changes added to commit (use "git add" and/or "git commit -a")
On branch: master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file1.cpp
modified: file2.cpp
Both file3.cpp and file4.cpp are untracked.
Your branch and 'origin/master' have diverged,
and have 4 and 2 different commits each, respectively.
https://github.com/Devinterview-io/git-interview-questions 18/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Best Practices
Regularly use git status to stay updated on the repository's state.
Review and comprehend the provided information before proceeding with any actions,
such as staging or committing.
git add is the first step in tracking changes you make to existing files or adding new files to
your Git repository. It prepares the changes for the next commit by moving modified or new
files to the staging area.
When you invoke git add , three major actions take place:
1. File Selection: You choose which specific file changes to include in the upcoming
commit.
2. File Comparison: Git analyzes the selected files and identifies the exact line-by-line or
block-level modifications.
3. Staging Area Update: The selected file changes are prepared and categorized. Once
you're ready, you can make a commit action to move all the changes in the staging area
to the repository.
Relation Manager
The staging area acts as a bridge between your working directory and the repository
(history).
Staging Area (Index): A kind of "Sandbox" where you selectively group changes together
for precise commits.
Repository: The historical record of all your commits and their respective states.
Key Actions
Add All Modifications: Restage modified files to the current state: git add .
https://github.com/Devinterview-io/git-interview-questions 19/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Merge Conflicts: After addressing conflicts, ready files for commit: git add <filename>
# Current content
print("This line is part of the initial version.")
In our Python example, consider "file.py" as the file. Initially, the file contains one line, which
was the initial version. Then "This line will be part of the next commit." gets added (not
committed yet), so that these changes occur between git add and git commit .
Untracked Files: These are new files that Git does not track yet. To track untracked files and
subsequently include them in a commit, they need to be staged using the git add
command.
Tracked Files: Files that have previously been committed or added to the staging area can be
included in the next commit.
Specific Files: Directly add individual files or a group of files using their names or paths.
https://github.com/Devinterview-io/git-interview-questions 20/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
By Pattern: Use wildcards or named sets of files to add multiple files at once.
Interactively: Launch a prompt where changes can be reviewed and selected for staging.
git add -i
These commands remove and rename files respectively. Changes are then staged and
committed similarly to new additions.
git reset
git checkout
To remove changes in the working directory, bring them back to the state of the HEAD
commit.
git commit
This command is used to save changes to the local repository after they have been staged.
Parameters (optional):
-m
-a
https://github.com/Devinterview-io/git-interview-questions 21/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
This command omits the staging step, effectively combining git add and git commit . Since
it bypasses the staging area completely, care should be taken when using it, to ensure only
intended changes are included in the commit.
When using -a without -m , Git will open the default text editor for you to compose the
commit message in a separate file.
Temporary Files: This might include build artifacts or editor-specific temporary files.
Sensitive Information: Such as passwords or private keys.
For such exclusions, you can define the file patterns or names in a .gitignore file.
Then, ensure to add a .gitignore file in the root directory for the local repository if needed.
touch .gitignore
CAUTION:
Avoid tracking, staging, or committing sensitive information.
Keep .gitignore up to date, especially in collaborative projects.
https://github.com/Devinterview-io/git-interview-questions 22/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Dangling Commits
In cases when a branch or reference no longer points to a commit, Git still retains these
dangling commits. Using git reflog , you can potentially recover them.
Releases
No releases published
https://github.com/Devinterview-io/git-interview-questions 23/24
8/8/24, 0:02 GitHub - Devinterview-io/git-interview-questions: 🟣 Git interview questions and answers to help you prepare for your next technical inter…
Packages
No packages published
https://github.com/Devinterview-io/git-interview-questions 24/24