Skip to content

Commit 57b3b3e

Browse files
committed
Rebashing in git
1 parent 2c42fce commit 57b3b3e

8 files changed

Lines changed: 2097 additions & 0 deletions

04-Rebasing/01-what-is-rebase.md

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
2+
# 🔁 What Is Rebase?
3+
4+
---
5+
6+
## 🎯 Why This Matters
7+
8+
Rebasing is used to:
9+
10+
- keep Git history clean
11+
- avoid unnecessary merge commits
12+
- make commit history easier to read
13+
14+
---
15+
16+
## ✅ Definition
17+
18+
Rebase is:
19+
20+
> moving a branch to a new base and replaying its commits
21+
22+
---
23+
24+
## 🧠 Mental Model
25+
26+
Instead of combining histories like merge:
27+
28+
👉 rebase **rebuilds history**
29+
30+
---
31+
32+
## 📊 Before Rebase
33+
34+
```text
35+
main: A --- B --- C
36+
\
37+
feature: D --- E
38+
````
39+
40+
---
41+
42+
## 📊 After Rebase
43+
44+
```text id="reb102"
45+
main: A --- B --- C --- D' --- E'
46+
```
47+
48+
---
49+
50+
## 📊 Key Difference
51+
52+
* D, E → replaced by D’, E’
53+
* new commit hashes created
54+
55+
---
56+
57+
## 📊 Visual (Mermaid)
58+
59+
```mermaid id="reb103"
60+
gitGraph
61+
commit id: "A"
62+
commit id: "B"
63+
commit id: "C"
64+
branch feature
65+
checkout feature
66+
commit id: "D"
67+
commit id: "E"
68+
```
69+
70+
(Rebase → commits replayed on top of C)
71+
72+
---
73+
74+
## 🏗 Internal Architecture
75+
76+
---
77+
78+
### 1. Merge Base
79+
80+
Git finds:
81+
82+
```text id="reb104"
83+
common ancestor = C
84+
```
85+
86+
---
87+
88+
### 2. Commit Extraction
89+
90+
Git extracts:
91+
92+
```text id="reb105"
93+
D, E
94+
```
95+
96+
---
97+
98+
### 3. Reset Branch
99+
100+
```text id="reb106"
101+
feature → C
102+
```
103+
104+
---
105+
106+
### 4. Replay Commits
107+
108+
```text id="reb107"
109+
D → D'
110+
E → E'
111+
```
112+
113+
---
114+
115+
## 🔬 What Happens Internally
116+
117+
Command:
118+
119+
```bash id="reb108"
120+
git rebase main
121+
```
122+
123+
Steps:
124+
125+
1. find merge base
126+
2. save commits as patches
127+
3. move branch pointer
128+
4. apply commits one by one
129+
130+
---
131+
132+
## ⚡ Key Insight
133+
134+
> Rebase rewrites history by creating new commits
135+
136+
---
137+
138+
## 🧩 Merge vs Rebase (Quick)
139+
140+
---
141+
142+
### Merge
143+
144+
```text id="reb109"
145+
creates merge commit
146+
```
147+
148+
---
149+
150+
### Rebase
151+
152+
```text id="reb110"
153+
creates linear history
154+
```
155+
156+
---
157+
158+
## 🧩 Real Use Cases
159+
160+
---
161+
162+
### 🔹 Update feature branch
163+
164+
```bash id="reb111"
165+
git rebase main
166+
```
167+
168+
---
169+
170+
### 🔹 Clean commit history
171+
172+
Before PR
173+
174+
---
175+
176+
### 🔹 Avoid merge clutter
177+
178+
Cleaner logs
179+
180+
---
181+
182+
## 🛠 Command Variants
183+
184+
---
185+
186+
### Basic rebase
187+
188+
```bash id="reb112"
189+
git rebase main
190+
```
191+
192+
---
193+
194+
### Interactive rebase
195+
196+
```bash id="reb113"
197+
git rebase -i HEAD~3
198+
```
199+
200+
---
201+
202+
### Continue after conflict
203+
204+
```bash id="reb114"
205+
git rebase --continue
206+
```
207+
208+
---
209+
210+
### Abort rebase
211+
212+
```bash id="reb115"
213+
git rebase --abort
214+
```
215+
216+
---
217+
218+
## ⚠️ Common Mistakes
219+
220+
---
221+
222+
### ❌ Rebasing shared branch
223+
224+
👉 breaks team history
225+
226+
---
227+
228+
### ❌ Ignoring conflicts
229+
230+
Must resolve manually
231+
232+
---
233+
234+
### ❌ Not testing after rebase
235+
236+
Replayed commits may break code
237+
238+
---
239+
240+
### ❌ Confusing merge and rebase
241+
242+
---
243+
244+
## 🧠 Best Practices
245+
246+
* rebase local branches only
247+
* use before PR
248+
* resolve conflicts carefully
249+
* verify history after rebase
250+
251+
---
252+
253+
## 🧠 Interview-Level Explanation
254+
255+
**Q: What happens during a rebase?**
256+
257+
Answer:
258+
259+
> During rebase, Git identifies the common ancestor, temporarily removes commits from the current branch, resets the branch to the new base, and reapplies those commits one by one, creating new commit hashes.
260+
261+
---
262+
263+
## 🧠 Memory Trick
264+
265+
> Rebase = replay commits
266+
267+
---
268+
269+
## ✅ Quick Recap
270+
271+
* moves branch to new base
272+
* rewrites commits
273+
* creates linear history
274+
* safer for local work
275+
276+
---
277+
278+
## Check Yourself
279+
280+
1. What happens to commits during rebase?
281+
2. Why do commit hashes change?
282+
3. When should you avoid rebasing?
283+
4. What is the difference from merge?
284+
285+
---
286+
287+
## ➡️ Next Step
288+
289+
Go to: `02-merge-vs-rebase.md`

0 commit comments

Comments
 (0)