Skip to content

Instantly share code, notes, and snippets.

@Denilson-Semedo
Created May 6, 2024 16:49
Show Gist options
  • Save Denilson-Semedo/67d01c1bd4092ecd7faa8be32476916f to your computer and use it in GitHub Desktop.
Save Denilson-Semedo/67d01c1bd4092ecd7faa8be32476916f to your computer and use it in GitHub Desktop.
Git merge

Merging Changes from Master into Your Branch: Rebase vs. Merge

When working on a project with multiple collaborators or branches, integrating changes from the main branch (often master) into your feature branch is a common task. Git offers two primary methods for this: rebase and merge. Both approaches achieve the same goal but have different implications for project history and workflow. Let's explore when to use each method and how to execute them.

Using rebase

Syntax:

git checkout custom_branch
git pull origin master
git rebase master

Explanation:

  1. Checkout Branch: Switch to your feature branch where you want to integrate changes (custom_branch in this case).
  2. Pull Changes: Ensure your local master branch is up to date by pulling changes from the remote repository (origin).
  3. Rebase: Apply the changes from the master branch onto your feature branch, rewriting your branch's commit history.

When to Use:

  • Use rebase when you want to maintain a linear project history.
  • Ideal for feature branches with few or no collaborators.
  • Helps keep commit history clean and easier to understand.

Output:

Successfully rebased and updated refs/heads/custom_branch.

Using merge

Syntax:

git checkout custom_branch
git pull origin master
git merge master

Explanation:

  1. Checkout Branch: Same as in the rebase method.
  2. Pull Changes: Ensure your local master branch is up to date, similar to the rebase method.
  3. Merge: Combine the changes from the master branch into your feature branch, preserving the commit history of both branches.

When to Use:

  • Use merge when preserving the original branching structure and commit history is more important.
  • Suitable for collaborative branches with multiple contributors.
  • Provides a straightforward approach to integrating changes.

Output:

Merge made by the 'recursive' strategy.

USE CASE: Merging changes from master into my branch

First make sure master is up to date, by checking out master and pulling the latest changes:

git checkout master &&
git pull

And then switch back to your branch and rebase it on top of master:

git checkout custom_branch && git rebase master

This will update custom_branch with changes from master branch and will keep the history clean (no merge commits).

This is also possible with:

git checkout custom_branch && git merge master

But this will create a merge commit, which is not always desired.

Deciding whether to use rebase or merge largely depends on your project's workflow and collaboration style. If you prioritize a clean, linear history, rebase might be the preferred option. On the other hand, if maintaining the integrity of branch history is crucial, merge is a better fit. Understanding the implications of each method empowers you to make informed decisions that align with your project's goals and workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment