Advanced Branch Integration: Git Rebasing

Git avanzato

Amanda Crawford-Adamo

Software and Data Engineer

Git rebase

  • Method to integrate changes
  • Different from merge
  • Removes merge commits for a cleaner history
  • Maintains a linear commit graph for clarity

Git Rebase Command:

git rebase <branch_name>
Git avanzato

Rebase example - before

An image of the data cleanup feature branch and main branch. The feature branch diverges after the second commit in main. The main branch has two additional commits after the data cleanup branch was created.

Git avanzato

Rebase process

  1. Checkout your data-cleanup feature branch.
git checkout data-cleanup
  1. Rebase main branch onto data-cleanup branch.
git rebase main
  • data-cleanup commits are recreated after main's latest commit
  • Rebased data-cleanup commits get new hashes

Note: If there are any conflicts, these would need to be resolved manually.

Git avanzato

Rebase example - after

An image of the data cleanup branch rebased onto the main branch. The data cleanup branch now all of main branch commits and the data cleanup commits now begin after all of main commits. Main branch has not changed.

Git avanzato

Interactive rebase

git rebase -i <commit_hash> 

Functionality

  • Allows developer to make granular changes to multiple commits
  • Opens an editor

Warning!

  • Rebasing public branches can disrupt workflows
    • Do not use rebase on public branches, like main
  • Establish team rules on when to use rebase
Git avanzato

Interactive rebase - before

$$

Here is the data-validation branch history before we edit the commit history.

$ git log --oneline data-validation
abc1234 Optimize validation performance
def5678 Fix validation bug
ghi9101 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit
Git avanzato

Interactive rebase editor

Here's how we edit the commit history using pick and fixup commands in the open editor.

$ git rebase -i HEAD~3 
pick ghi9101 Add data validation function
fixup def5678 Fix validation bug
fixup abc1234 Optimize validation performance

# Rebase xyz1234..abc1234 onto HEAD~3(xyz1234) (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# f, fixup <commit> = like "squash", but discard this commit's log message
...
Git avanzato

Interactive rebase - after

Before

$ git log --oneline data-validation
abc1234 Optimize validation performance
def5678 Fix validation bug
ghi9101 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit

After

$ git log --oneline data-validatoin
mno6789 Add data validation function
xyz1234 New Main Branch Commit
vwx7890 Main Branch Commit
jkl2345 Initial commit

The following commits are combined into one

  • abc1234
  • def5678
  • ghi9101
Git avanzato

Merge versus rebase

The image shows three graphs, a diverged branch graph, the resulting merge graph, and the resulting rebased graph

Merge

  • Preserves full history and branch structure
  • Maintains context of parallel development

Rebase

  • Replays commits on top of the target branch
  • Creates a linear history
  • Loses some contextual information
Git avanzato

When to use merge versus rebase

Merge

For integrating completed features, preserving development context

Rebase

For keeping feature branches updated with main, or for cleaning up before merging

Remember

  • Rebase rewrites history - use it carefully
Git avanzato

Let's practice!

Git avanzato

Preparing Video For Download...