Skip to content

rebase - A Clean Commit History ​

1. The Situation ​

  • While working on the premium branch, functions that need experimenting are worked on in a separate test branch.
  • Simple functions are added directly on the premium branch, and code whose experiments are finished is added on the test branch and then applied to premium.

2. The Usual Way: git merge ​

  • git merge is mostly used to combine branches.
  • A merge creates a new merge commit.
  • The commit history keeps a record of the branches splitting and joining, which can look a bit complicated.

git merge [branchname]

3. What Is rebase? ​

  • git rebase means "re-placing commits."
  • It changes the base of the branch you're on to a commit from another branch.
  • It makes the commit history a clean, straight line.
  • If a conflict occurs during a rebase, you resolve it yourself just like with merge, and then continue with git rebase --continue.

git rebase [branchname]

4. merge vs. rebase ​

  • merge: a merge commit where the branches join remains in the history → the traces of splitting and joining are visible.
  • rebase: no new commit is created, and the history looks like one continuous line → clean.
  • The result (the code) is the same, but the structure of the commit history differs.

5. When to merge, when to rebase? ​

  • Use merge if you really need a record that the branches were combined.
  • Use rebase if you want to keep the commit history as a neat single line.

Summary ​

mergerebase
HistoryTraces of splitting and joining + a merge commit remainOne continuous line
WhenWhen you need a record that the branches were combinedWhen you want to keep the history clean

Reference: personal Notion study notes (Git study)