# 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:**

```plaintext
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:**

```plaintext
git status
```

---

# **4️⃣ How do you track files in Git?**

Using the **git add** command.

```plaintext
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

```plaintext
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**

```plaintext
git log
```

---

# **7️⃣ Pushing Code to Remote**

```plaintext
git push
```

If remote not configured:

```plaintext
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:**

```plaintext
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:**

```plaintext
ssh-keygen -t rsa
```

Copy public key:

```plaintext
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**

```plaintext
git branch feature-division
```

### **Create & switch (most used)**

```plaintext
git checkout -b division
```

### **Switch branches**

```plaintext
git checkout main
```

---

# **1️⃣2️⃣ Merge, Rebase, Cherry-Pick**

These are important interview topics.

---

## **A) git merge**

Used to combine two branches.

```plaintext
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).

```plaintext
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.

```plaintext
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.

```plaintext
git diff
```

---

# **1️⃣4️⃣ Rollback Commands**

### **Undo unstaged changes**

```plaintext
git checkout -- filename
```

### **Undo staged changes**

```plaintext
git reset filename
```

### **Rollback a commit (safe)**

```plaintext
git revert <commit-id>
```

### **Dangerous (history rewrite)**

```plaintext
git reset --hard <commit-id>
```

---

# **1️⃣5️⃣ Git Stash**

Used daily by DevOps.

Saves uncommitted work temporarily:

```plaintext
git stash
```

Restore:

```plaintext
git stash pop
```

---

# **1️⃣6️⃣ Git Hooks (Real DevOps Example)**

Located in `.git/hooks`

Examples:

* `pre-commit` → prevent secrets/credentials from being committed
    
* `pre-push` → run tests before pushing
    
* `post-merge` → notify CI/CD
    

---

# **Summary: Daily Git Workflow**

```plaintext
git pull
git checkout -b feature-x
git add .
git commit -m "Added feature"
git push
Create Pull Request → Merge to main
```
