mwotton
mwotton

Reputation: 2200

Ways to merge branch with previous commit on master branch, then reapply later master commits

I have a master branch that was branched from by another dev, who then added features. In the meantime, I have made changes to some of the same files. While there is no direct conflict on lines, I am wondering about the different methods to use to merge the other branch back into master, with minimal conflicts.

ASCII art visualisation:

A-B-C-D
\
 1-2-3

Am I best off checking out commit A, merging the branch at 3 back in, then reapplying the changes in B through D? Or should I just merge 3 back into D? Or I am sure there's some better way that I am missing.

I am fairly new to git, I think I am getting the hang of it, but I just want to make sure I am using it properly.

Upvotes: 0

Views: 89

Answers (2)

me_and
me_and

Reputation: 15634

What you describe—checking out A, merging in 13, then applying BD—is a standard Git technique called rebasing. Assuming there are no conflicts that can't be automatically resolved, you can do it with a single command:

git rebase --onto {commit 3} {commit A} {commit D}

If there are conflicts when doing a rebase, Git will talk you through resolving them.

However, the normal Git workflow is to just merge the branches together. Unless you've some reason not to, I'd advocate just doing the merge.

Merging more accurately reflects the development process; it means you can look at your history and see exactly how a feature was developed then merged into the main code. Rebasing makes your history look artificially clean; sometimes that's useful, but it does hide how things were done.

Upvotes: 1

Penghe Geng
Penghe Geng

Reputation: 14621

Say the original branch name of A-B-C-D is "orig". And you are at "3", with branch name "master". Try this:

git rebase orig

Upvotes: 3

Related Questions