Overview
Rebasing is a Git operation that moves or combines commits from one branch onto another. It’s an alternative to merging that creates a linear project history. GitHub Desktop provides a safe and intuitive interface for rebasing.Linear History
Create a clean, linear commit history
Conflict Detection
Preview conflicts before starting the rebase
Interactive Progress
See real-time progress as commits are applied
Force Push Support
Safely force push with —force-with-lease
Understanding Rebase
What is Rebasing?
Rebasing rewrites commit history by:- Taking commits from your current branch
- Temporarily removing them
- Updating the base branch to the target
- Reapplying your commits on top of the new base
Rebase vs. Merge
- Rebase
- Merge
Linear History:Characteristics:
- Creates linear history
- Rewrites commits (new SHAs)
- Cleaner history
- Requires force push if already pushed
When to Use Rebase
Good Use Cases
Update Feature Branch
Keep your feature branch up to date with main before merging
Clean Up History
Create a clean, linear history before creating a pull request
Local Commits
Reorganize commits that haven’t been pushed yet
Interactive Cleanup
Squash, edit, or reorder commits before pushing
When to Avoid Rebase
Avoid rebasing when:- Commits are on a shared branch
- Other developers have based work on your commits
- Working on the repository’s main/default branch
- You’re unsure about the impact
- Multiple people are working on the same feature branch
Rebasing in GitHub Desktop
Basic Rebase Workflow
Review Information
GitHub Desktop shows:
- Number of commits to rebase
- Potential conflict warnings
- Remote commit warnings (if applicable)
Rebase Implementation
From the technical documentation and source code:Testing the Rebase
Before starting, GitHub Desktop determines:Progress Tracking
Rebase State Detection
GitHub Desktop detects ongoing rebases:Handling Conflicts During Rebase
When Conflicts Occur
If a commit can’t be applied cleanly:Shows Conflict Info
Displays:
- Which commit caused the conflict
- Conflicted files
- Progress (e.g., “Commit 3 of 7”)
Resolve Conflicts
Fix conflicts in each file:
- Open in editor
- Choose “ours” or “theirs”
- Manually edit conflict markers
Continue Rebase
After resolving conflicts:If a commit becomes empty after conflict resolution (all changes were already in the base), GitHub Desktop automatically runs
git rebase --skip.Abort Rebase
To cancel the rebase:Force Pushing After Rebase
Why Force Push?
After rebasing commits that were already pushed:- Commit SHAs have changed
- Remote branch has the old commits
- You must force push to update the remote
Safe Force Push
GitHub Desktop uses--force-with-lease for safety:
--force-with-lease?
- Fails if remote was updated by someone else
- Prevents overwriting others’ commits
- Safer than
--force
Force Push State Tracking
GitHub Desktop tracks branches eligible for force push:Rebase Confirmation Dialog
If enabled in preferences, GitHub Desktop shows a warning when:Remote Commits Detected
When rebasing a branch with pushed commits: Warning shows:- Number of commits that will be rewritten
- Explanation of force push requirement
- Impact on collaborators
- Option to continue or cancel
Rebase vs. Merge: Choosing the Right Tool
Use Rebase When:
-
Cleaning Up Local Commits
- Before creating a pull request
- Commits haven’t been pushed
- Want a clean, linear history
-
Updating Feature Branches
- Bringing latest changes from main
- Before merging via PR
- No one else is on the branch
-
Maintaining Clean History
- Project prefers linear history
- Team understands rebase workflow
- Following agreed conventions
Use Merge When:
-
Shared Branches
- Multiple people working on the branch
- Commits are public
- Want to preserve exact history
-
Main/Default Branch
- Never rebase the main branch
- Merge feature branches into it
- Keep stable history
-
Unsure About Impact
- When in doubt, merge is safer
- Can always rebase later locally
- Easier to undo
Best Practices
-
Rebase Often
- Keep feature branches updated with main
- Rebase frequently to avoid large conflicts
- Makes final merge easier
-
Test After Rebasing
- Run tests after completing a rebase
- Conflicts can introduce bugs
- Verify everything still works
-
One Commit at a Time
- Resolve conflicts commit-by-commit
- Don’t rush through conflicts
- Test between conflict resolutions if possible
-
Use Descriptive Commit Messages
- Makes rebasing easier to follow
- Helps identify which commits to keep/skip
- Useful when resolving conflicts
-
Enable Force Push Confirmation
- Requires explicit confirmation before force pushing
- Prevents accidental overwrites
- Enabled by default in Preferences > Git
-
Understand the Consequences
- Know that rebase rewrites history
- Be prepared to force push
- Communicate with team
Common Rebase Scenarios
Update Feature Branch from Main
Clean Up Before Pull Request
Rebase After Main Updated
Troubleshooting
Rebase Button Disabled
Rebase Button Disabled
Too Many Conflicts
Too Many Conflicts
If you encounter overwhelming conflicts:
- Abort the rebase: Better to merge than fight conflicts
- Update more frequently: Smaller, frequent rebases have fewer conflicts
- Consider merge instead: Merging might be more appropriate
- Ask for help: Consult the commit authors
Lost Commits After Abort
Lost Commits After Abort
If commits seem lost after aborting:
- They’re not actually lost
- Check
git reflogto find them - Branch pointer was just moved back
- Can recover with
git reset --hard <sha>
Force Push Rejected
Force Push Rejected
If force push fails:
- Branch is protected: Check branch protection rules
- No force push permission: You may not have rights
- Someone pushed: Another commit was pushed (fetch first)
- Wrong branch: Verify you’re on the correct branch
Rebase Stuck or Frozen
Rebase Stuck or Frozen
If rebase appears stuck:
- Check for hook scripts that are running
- Look for prompts in terminal
- Verify disk space available
- Try aborting and restarting