Advanced Git: Rebase, Merge, Conflict Resolution, Squash | Generated by AI

Home PDF

This guide covers advanced usage of git rebase and git merge, resolving conflicts, using ours and theirs strategies, and squashing commits. I’ll provide concise explanations and practical examples for each topic, focusing on advanced workflows that assume familiarity with basic Git commands.

1. Git Merge: Advanced Usage

git merge combines multiple sequences of development into a single unified history. Advanced usage includes handling complex merges, using merge strategies, and managing merge commits.

Key Merge Strategies

Advanced Merge Options

Example: Merging with No Fast-Forward

git checkout main
git merge --no-ff feature-branch
# Creates a merge commit, preserving feature-branch history

2. Git Rebase: Advanced Usage

git rebase rewrites history by moving or modifying commits to create a linear history. It’s powerful for cleaning up branches but alters history, so use with caution on shared branches.

Types of Rebase

Interactive Rebase Commands

Run git rebase -i <base> (e.g., git rebase -i HEAD~3 for the last 3 commits). This opens an editor with commands like:

Example: Interactive Rebase

To squash the last 3 commits:

git rebase -i HEAD~3
# In the editor, change "pick" to "squash" or "fixup" for the commits to combine
# Save and exit to complete

Rebase onto a Different Base

To move a branch to a new base (e.g., moving feature-branch from old-base to main):

git rebase --onto main old-base feature-branch

Rebase with Merge Commits

By default, rebase flattens merge commits. To preserve them:

git rebase -i --preserve-merges main

Aborting a Rebase

If something goes wrong:

git rebase --abort

3. Resolving Merge/Rebase Conflicts

Conflicts occur when Git can’t automatically reconcile changes. Both merge and rebase can result in conflicts, resolved similarly.

Steps to Resolve Conflicts

  1. Identify Conflicts: Git pauses and lists conflicted files.
    • For merge: git status shows files with conflicts.
    • For rebase: Conflicts are resolved commit-by-commit during git rebase -i.
  2. Edit Conflicted Files: Open files and look for conflict markers:
    <<<<<<< HEAD
    Your changes
    =======
    Incoming changes
    >>>>>>> branch-name
    

    Manually edit to keep desired changes, then remove markers.

  3. Mark as Resolved:
    • For merge: git add <file>
    • For rebase: git add <file>, then git rebase --continue
  4. Complete the Process:
    • Merge: git commit (Git may auto-generate a commit message).
    • Rebase: git rebase --continue until all commits are applied.

Example: Resolving a Merge Conflict

git checkout main
git merge feature-branch
# Conflict occurs
git status  # Lists conflicted files
# Edit files to resolve conflicts
git add resolved-file.txt
git commit  # Finalize merge

Example: Resolving a Rebase Conflict

git checkout feature-branch
git rebase main
# Conflict occurs
# Edit conflicted files
git add resolved-file.txt
git rebase --continue
# Repeat until rebase completes

4. Using Ours and Theirs in Conflict Resolution

During conflicts, you may want to favor one side’s changes (ours or theirs). The meaning of ours and theirs depends on the operation.

Merge: Ours vs. Theirs

Rebase: Ours vs. Theirs

Example: Merge with Theirs

To merge feature-branch into main and favor feature-branch changes:

git checkout main
git merge -X theirs feature-branch

Example: Rebase with Ours

While rebasing feature-branch onto main, resolve a conflict by keeping main’s version:

git checkout feature-branch
git rebase main
# Conflict occurs
git checkout --ours file.txt
git add file.txt
git rebase --continue

5. Squashing Commits

Squashing combines multiple commits into one, creating a cleaner history. This is typically done with interactive rebase.

Steps to Squash Commits

  1. Start an interactive rebase for the desired commits:
    git rebase -i HEAD~n  # n = number of commits to squash
    
  2. In the editor, change pick to squash (or fixup) for commits to combine into the previous commit.
  3. Save and exit. Git may prompt to edit the commit message for the combined commit.
  4. Push the updated history (force push if already shared):
    git push --force-with-lease
    

Example: Squashing 3 Commits

git rebase -i HEAD~3
# Editor shows:
# pick abc123 Commit 1
# pick def456 Commit 2
# pick ghi789 Commit 3
# Change to:
# pick abc123 Commit 1
# squash def456 Commit 2
# squash ghi789 Commit 3
# Save and exit
# Edit combined commit message if prompted
git push --force-with-lease

Squashing During Merge

To squash all commits from a branch during a merge:

git checkout main
git merge --squash feature-branch
git commit  # Create a single commit

Best Practices and Tips

Common Pitfalls

Recovering from Mistakes

This guide provides a solid foundation for advanced Git workflows. For specific scenarios or errors, let me know, and I can dig deeper or search for real-time solutions!


Back 2025.05.28 Donate