Day 9 - Git Interview Questions, Answers & Commands (DevOps-Focused)
Below is the simplest and cleanest breakdown of all key Git concepts used daily by DevOps, SRE, and software engineers, including real-world examples, use-cases, and interview-ready answers.
1️⃣ What is Git?
Interview Answer:
Git is a distributed version control system (DVCS) used to track code changes, collaborate with teams, manage versions and branches, and ensure rollback capability.
Key Points:
Distributed → every developer has full repository copy
Supports branching, merging, rollback, auditing
Provides collaboration between developers + CI/CD pipelines
2️⃣ How do you initialize a Git repository?
Command:
git init
What happens?
Creates a .git directory in the project folder
This folder stores:
commits
branches
logs
configuration
hooks (pre-commit, pre-push etc.)
Interview Answer example:
“When we run git init, Git creates a .git directory that stores all metadata needed for version control — such as commit history, branches, configuration, and hooks.”
3️⃣ What does “git status” do?
Shows:
untracked files
modified files
staged files
current branch
Command:
git status
4️⃣ How do you track files in Git?
Using the git add command.
git add filename
git add . # add all files
Purpose:
Stages the changes so Git starts tracking them.
Interview Answer:
"git add moves changes to the staging area so Git knows which files should be included in the next commit."
5️⃣ Why do we use commits?
Commit = snapshot + message
git commit -m "Added addition function"
Why required?
Provides history
Enables rollback
Helps to track “who changed what & why”
Crucial for CI/CD pipelines
Interview Answer:
“Commits help maintain proper version history. Each commit contains an author, timestamp, and commit message, making debugging and rollback easier.”
6️⃣ View commit history
git log
7️⃣ Pushing Code to Remote
git push
If remote not configured:
git remote add origin <repo-url>
git push -u origin main
8️⃣ What is Git Clone?
Interview Answer:
"git clone is used to download or copy an existing remote repository into your local machine."
Command:
git clone <repo-url>
9️⃣ Difference: Git Clone vs Git Fork
| Feature | Clone | Fork |
| Purpose | Download repo | Create independent copy on your account |
| Use case | Work on same project | Start your own version or contribute |
| Ownership | Not yours | Belongs to your GitHub account |
| Updates | Stays linked | Independent unless synced manually |
Interview Answer:
“Clone downloads a repo as-is, while fork creates an independent copy under your GitHub account — often used in open-source contributions.”
🔟 HTTPS vs SSH in Git
HTTPS
Uses username + password
Easy to start, but asks credentials repeatedly
SSH
Uses public/private keys
Best for DevOps teams
More secure
Generate SSH key:
ssh-keygen -t rsa
Copy public key:
cat ~/.ssh/id_rsa.pub
Add it to GitHub → Settings → SSH Keys
1️⃣1️⃣ Branching (Real-World DevOps Example)
Why use branches?
To avoid breaking the production code.
Real Example:
Amazon developers create a separate branch for “Carpenter Service Feature” instead of changing main directly.
Create a new branch
git branch feature-division
Create & switch (most used)
git checkout -b division
Switch branches
git checkout main
1️⃣2️⃣ Merge, Rebase, Cherry-Pick
These are important interview topics.
A) git merge
Used to combine two branches.
git checkout main
git merge division
Pros:
Safe
Maintains commit history
Best for team collaboration
B) git rebase
Moves branch commits to top of another branch (linear history).
git checkout division
git rebase main
Pros:
Clean history
Cons:
Can cause conflicts
Don’t rebase shared branches
C) git cherry-pick
Pick specific commit(s) from another branch.
git cherry-pick <commit-id>
Use Case Example:
Production bug fix committed in feature branch → pick only that commit to main.
1️⃣3️⃣ git diff
Shows changes line-by-line.
git diff
1️⃣4️⃣ Rollback Commands
Undo unstaged changes
git checkout -- filename
Undo staged changes
git reset filename
Rollback a commit (safe)
git revert <commit-id>
Dangerous (history rewrite)
git reset --hard <commit-id>
1️⃣5️⃣ Git Stash
Used daily by DevOps.
Saves uncommitted work temporarily:
git stash
Restore:
git stash pop
1️⃣6️⃣ Git Hooks (Real DevOps Example)
Located in .git/hooks
Examples:
pre-commit→ prevent secrets/credentials from being committedpre-push→ run tests before pushingpost-merge→ notify CI/CD
Summary: Daily Git Workflow
git pull
git checkout -b feature-x
git add .
git commit -m "Added feature"
git push
Create Pull Request → Merge to main