Interview question Git
Interview question Git
A Comprehensive
Interview Guide
Table of Contents
1. Introduction to Git
○ What is Git?
○ Why Use Git?
○ Git vs. Other Version Control Systems
○ Key Features of Git
2. Basic Git Concepts
○ What is a Distributed Version Control System?
○ What is a Repository?
○ How to Create a Local Repository
○ What is a Bare Repository?
○ How to Configure Git Locally
3. Git Commands and Workflow
○ Git Clone
○ Git Add
○ Git Commit
○ Git Push
○ Git Pull
○ Git Fetch
○ Git Merge
○ Git Rebase
○ Git Stash
○ Git Reset
○ Git Revert
○ Git Log
○ Git Diff
4. Branching and Merging
○ What is Branching?
○ How to Create, Rename, and Delete Branches
○ How to Switch Between Branches
○ What is Merging?
○ How to Resolve Merge Conflicts
○ What is Rebasing?
○ Difference Between Merge and Rebase
5. Advanced Git Concepts
○ Git Hooks
○ Git Submodules
○ Git Worktrees
○ Git Bisect
○ Git Cherry-pick
○ Git Reflog
○ Git Tags
6. Git Best Practices
○ Commit Message Guidelines
○ Branching Strategies
○ Code Review with Git
○ Managing Large Repositories
○ Git Security Best Practices
7. Git in Collaborative Environments
○ Pull Requests
○ Code Reviews
○ Managing Access and Permissions
○ Locking Branches
○ Handling Large Teams
8. Git Troubleshooting
○ Common Git Errors and How to Fix Them
○ How to Recover Lost Commits
○ How to Undo Changes
○ How to Handle Detached HEAD State
Assma Fadhli
9. Git Tools and Integrations
○ Git GUI Clients
○ Git with IDEs
○ Git with CI/CD Pipelines
○ Git with GitHub, GitLab, and Bitbucket
10.Real-World Scenarios and Exercises
○ Fixing a Broken Commit
○ Resolving a Merge Conflict
○ Reverting a Bad Merge
○ Working with Remote Repositories
○ Managing Multiple Branches
11.Git Cheat Sheets
○ Basic Commands
○ Branching and Merging Commands
○ Advanced Commands
12.FAQs
○ How to Delete a Repository?
○ How to Give Access to a Specific Person?
○ How to Lock a Branch?
○ How to Recover Deleted Files?
○ How to Handle Large Files in Git?
13.Git Internals
○ How Git Works Under the Hood
○ The Git Object Model
○ The Git Directory (.git)
○ Hashing in Git
○ Git Plumbing vs. Porcelain
14.Git Workflows
○ Centralized Workflow
○ Feature Branch Workflow
○ Git Flow
○ Forking Workflow
○ Trunk-Based Development
15.Git in DevOps
○ Git and CI/CD Pipelines
○ Automated Testing
○ Infrastructure as Code (IaC)
○ GitOps
16.Git for Open-Source Projects
○ How to Contribute to Open-Source Projects
○ Best Practices for Open-Source Maintainers
Assma Fadhli
17.Git Performance Optimization
○ How to Speed Up Git Operations
○ Handling Large Repositories
Assma Fadhli
1. Introduction to Git
What is Git?
Git is a distributed version control system (DVCS) designed to handle everything from small to large
projects quickly and efficiently. It allows multiple developers to work on a project simultaneously
without overwriting each other's changes. Git tracks changes in source code, enabling developers to
revert to previous versions, compare changes, and collaborate seamlessly.
● Key Features:
○ Distributed: Every developer has a full copy of the repository, including its history.
○ Branching and Merging: Git makes it easy to create branches for new features or bug
fixes and merge them back into the main codebase.
○ Speed: Git is optimized for performance, even with large projects.
○ Data Integrity: Git uses SHA-1 hashes to ensure the integrity of your data.
In a distributed version control system (DVCS), every developer has a complete copy of the
repository, including its full history. This allows developers to work offline and commit changes
locally without needing a connection to a central server.
What is a Repository?
A Git repository is a directory where Git stores all the files and their revision history. It can be local
(on your machine) or remote (on a server like GitHub).
A bare repository is a Git repository without a working directory. It is typically used as a central
repository for sharing code.
$ git init --bare my_project.git
Assma Fadhli
How to Configure Git Locally?
Set up your username and email using the git config command:
$ git config - -global user.name "John Doe"
$ git config - -global user.email "john@example.com"
Git Clone
Git Add
Git Commit
Git Push
Git Pull
Git Fetch
Git Merge
Git Rebase
Git Stash
Assma Fadhli
To temporarily store changes that are not ready to be committed:
$ git stash
Git Reset
Git Revert
Git Log
Git Diff
What is Branching?
Branching allows you to work on different versions of your project simultaneously. For example, you
can create a branch for a new feature and switch back to the main branch to fix a bug.
● Create a branch:
$ git branch feature-branch
● Rename a branch:
$ git branch -m old-branch new-branch
● Delete a branch:
$ git branch -d feature-branch
What is Merging?
Merging combines changes from one branch into another. For example, you can merge a feature
Assma Fadhli
branch into the main branch.
When Git cannot automatically merge changes, it will prompt you to resolve conflicts manually. Edit
the conflicting files, then add and commit them:file.txt
What is Rebasing?
Rebasing is an alternative to merging. It reapplies commits from one branch to another, creating a
linear history.
Git Hooks
Git hooks are scripts that run automatically before or after certain Git commands, such as
committing or pushing.
Git Submodules
Submodules allow you to include one Git repository inside another. This is useful for managing
dependencies.
Git Worktrees
Git worktrees allow you to have multiple working directories attached to the same repository.
Git Bisect
Git bisect helps you find the commit that introduced a bug by performing a binary search through the
commit history.
Git Cherry-pick
Cherry-picking allows you to apply a specific commit from one branch to another.
Assma Fadhli
Git Reflog
The reflog records all changes to the repository's references, such as branch tips and HEAD.
Git Tags
Tags are used to mark specific points in the repository's history, such as releases.
Branching Strategies
Pull Requests
Pull requests allow developers to propose changes and discuss them before merging.
Code Reviews
Code reviews ensure that changes meet quality standards before being merged.
Assma Fadhli
Locking Branches
● Use branching strategies to manage multiple developers working on the same project.
8. Git Troubleshooting
● Use Git with Jenkins, GitHub Actions, or GitLab CI/CD for continuous integration.
● These platforms provide hosting for Git repositories and additional collaboration tools.
Assma Fadhli
10. Real-World Scenarios and Exercises
● Use git commit --amend to fix a commit message or add missing changes.
Basic Commands
git init
git clone
git add
git commit git pull
git push
Advanced Commands
git stash
git reset
git revert
git reflog
Assma Fadhli
12. FAQs
Git is not just a tool; it’s a content-addressable filesystem with a version control layer on top.
Understanding its internals can help you use Git more effectively.
● The Git Object Model:
○ Blobs: Store file data.
○ Trees: Represent directories and contain references to blobs and other trees.
○ Commits: Point to a tree and contain metadata like author, date, and commit message.
○ Tags: Point to a specific commit and are used to mark releases.
● The Git Directory (.git):
○ Contains all the metadata and object database for the repository.
○ Key files:
■ HEAD: Points to the current branch or commit.
■ config: Repository-specific configuration.
■ index: The staging area.
● Hashing in Git:
○ Git uses SHA-1 hashes to uniquely identify objects (blobs, trees, commits, tags).
○ Example: e3b0c44298fc1c149afbf4c8996fb92427ae41e4 is a SHA-1 hash.
Assma Fadhli
● Plumbing Commands: Low-level commands that interact directly with Git’s internals (e.g., git
hash-object, git cat-file).
● Porcelain Commands: High-level commands for everyday use (e.g., git add, git commit, git
push).
Different teams use different Git workflows depending on their needs. Here are some of the most
popular ones:
1. Centralized Workflow
● A single central repository where all developers push and pull changes.
● Similar to SVN but uses Git’s branching and merging capabilities.
3. Git Flow
● A branching model with main, develop, feature, release, and hotfix branches.
● Ideal for projects with scheduled release cycles.
4. Forking Workflow
● Each developer forks the main repository and works on their own copy.
● Changes are submitted via pull requests.
● Commonly used in open-source projects.
5. Trunk-Based Development
● Developers work on a single branch (main or trunk).
● Short-lived feature branches are used for small changes.
● Encourages continuous integration and delivery.
GitOps
● A paradigm for managing infrastructure and applications using Git as the single source of
truth.
● Changes to infrastructure or applications are made via Git commits.
● Tools like ArgoCD and Flux automate deployments based on Git changes.
● Shallow Cloning: Clone only the latest commit history to save time and disk space.
$ git clone --depth 1 https://github.com/user/repo.git
● Sparse Checkout: Check out only specific files or directories.
$ git sparse-checkout init --cone
$ git sparse-checkout set dir1
● Git Garbage Collection: Clean up unnecessary files and optimize the repository.
$ git gc --auto
Assma Fadhli
Handling Large Repositories
● Git LFS (Large File Storage): Store large files outside the repository and track them with
pointers.
$ git lfs install
$ git lfs track "*.psd"
● Partial Cloning: Clone only part of the repository to save space.
$ git clone --filter=blob:none https://github.com/user/repo.git
/github.com/user/repo.git
● DVC (Data Version Control): A tool built on top of Git to version large datasets and machine
learning models.
Assma Fadhli
dvc add data.csv
git add data.csv.dvc
git commit -m "Add dataset"
● Mobile and game development often involves large binary files (e.g., images, videos, 3D
models).
● Use Git LFS to track these files without bloating the repository.
Versioning Documentation
Collaborative Writing
Assma Fadhli
23. Git for System Administrators
Automating Backups
24. Conclusion
Git is an incredibly versatile tool that goes beyond version control. Whether you’re a developer, data
scientist, system administrator, or open-source contributor, Git can streamline your workflow and
improve collaboration. By mastering Git’s advanced features and best practices, you can take your
skills to the next level and tackle even the most complex projects with confidence.
Assma Fadhli