Skip to content

Latest commit

 

History

History
163 lines (113 loc) · 4.45 KB

1. Rebasing.md

File metadata and controls

163 lines (113 loc) · 4.45 KB

Rebasing

Rebasing is a Git operation that moves a branch to a new base commit. It allows you to maintain a cleaner and more linear project history by integrating changes from one branch into another.


1. What is Rebasing?

Rebasing rewrites the commit history of a branch by applying its changes on top of another branch. Unlike merging, which creates a new merge commit, rebasing integrates commits into a single, linear sequence.


2. When to Use Rebasing

  • Clean History: Rebasing creates a straight-line history, making it easier to understand and review.
  • Feature Development: Use rebasing to keep feature branches up-to-date with the main branch.
  • Avoid Merge Commits: Rebasing avoids extra merge commits in the history.

3. How Rebasing Works

Example:

Consider the following branches:

main:    A --- B --- C
feature:         D --- E

When rebasing feature onto main, the commits D and E are replayed on top of main:

main:    A --- B --- C --- D' --- E'

4. Performing a Rebase

Rebasing Onto Another Branch

  1. Switch to the branch you want to rebase:

    git checkout feature
  2. Rebase it onto the target branch (e.g., main):

    git rebase main

Resolving Conflicts During Rebase

If conflicts occur:

  1. Git pauses the rebase and marks the conflicting files.

  2. Resolve the conflicts manually in your editor.

  3. Mark conflicts as resolved:

    git add resolved_file.txt
  4. Continue the rebase:

    git rebase --continue

To abort the rebase:

git rebase --abort

5. Interactive Rebasing

Interactive rebasing allows you to edit, squash, or reorder commits.

Start an Interactive Rebase

git rebase -i HEAD~n
  • Replace n with the number of commits you want to rebase.

Options in Interactive Rebase

  • pick: Keep the commit as-is.
  • edit: Edit the commit message or content.
  • squash: Combine the commit with the previous one.
  • reword: Change the commit message.

Example Workflow

  1. Start an interactive rebase for the last 3 commits:

    git rebase -i HEAD~3
  2. Change the options in the editor, then save and close.

  3. Complete the rebase, resolving any conflicts if needed.


6. Best Practices for Rebasing

  • Rebase Before Pushing: Ensure you haven’t shared the branch with others before rebasing.
  • Use for Local Changes: Avoid rebasing shared branches to prevent rewriting history others rely on.
  • Keep Backups: Create a backup branch before rebasing to avoid accidental data loss.

7. Rebase vs. Merge

Feature Rebase Merge
Commit History Linear Includes merge commits
Complexity Rewrites history Preserves history as-is
Use Case Cleaning up commits, small teams Maintaining complete history, large teams

8. Common Commands

Command Description
git rebase branch_name Rebase the current branch onto another branch.
git rebase --continue Continue a paused rebase after resolving conflicts.
git rebase --abort Cancel the rebase and return to the original state.
git rebase -i HEAD~n Start an interactive rebase for the last n commits.

9. Example Workflow

Step 1: Update a Feature Branch

  1. Switch to the feature branch:

    git checkout feature
  2. Rebase onto main:

    git rebase main
  3. Resolve conflicts (if any) and continue:

    git rebase --continue

10. When to Avoid Rebasing

  • On Public Branches: Rebasing rewrites history, which can confuse collaborators.
  • If Unsure: Use merging instead of rebasing if you're new to Git or uncertain about rewriting history.

Conclusion

Rebasing is a powerful Git feature for maintaining a clean and linear commit history. By using it responsibly and understanding its implications, you can simplify collaboration and code reviews.


Next Steps: Cherry-Picking