-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.txt
More file actions
84 lines (57 loc) · 2.44 KB
/
git.txt
File metadata and controls
84 lines (57 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Nice — here’s the commit graph diagram you asked for. This shows how your history looks depending on whether you merge or rebase.
⸻
A) Merge workflow (preserves full branch graph)
Upstream main: A──B──C───────────────┐
│
Your branch: A──B──C──D──E─────────┴─M
↑
Merge commit (M)
• You branched from commit C.
• You added commits D and E.
• Upstream added commits after C.
• When you git merge upstream/main, Git creates a merge commit (M) joining both histories.
• History shows branching + merging — good for context but can look messy.
⸻
B) Rebase workflow (linear history, no merge commit)
Upstream main: A──B──C──F──G──H
│
Your branch after rebase: D'──E'
• Same start at commit C.
• Upstream added commits F, G, H.
• When you git rebase upstream/main, your commits (D, E) are replayed on top of the new tip H, becoming D' and E'.
• Result: looks like you developed your feature after the latest upstream work — clean linear history.
• Requires --force-with-lease when pushing, because commit hashes change.
⸻
C) Side-by-side: Merge vs Rebase
Merge history (graphy):
A──B──C───────────────F──G──H
└──D──E──────────────┘
Rebase history (linear):
A──B──C──F──G──H──D'──E'
⸻
D) When to use what
• Merge →
• If you want to preserve the “real” history (showing when branches diverged).
• Safer for beginners (no rewriting commits).
• Good for long-lived feature branches.
• Rebase →
• If you want a clean, straight line history.
• Preferred in open-source PR workflows.
• Requires confidence resolving conflicts & force-pushing.
⸻
E) Common commands
Merge workflow:
git checkout feature-branch
git fetch upstream
git merge upstream/main
git push origin feature-branch
Rebase workflow:
git checkout feature-branch
git fetch upstream
git rebase upstream/main
# fix conflicts if needed
git push origin feature-branch --force-with-lease
⸻
So: Merge = “historical diary” (records exactly what happened).
Rebase = “rewrite the story” (makes it look like you developed cleanly on top).
⸻