Merging development branch after master branch has changed (committed)

Viewed 2231

I have been working on a development branch for some time, meanwhile, another developer has been making changes to the master branch (several commits to the master).

The master branch now has content that I do not have in my development branch.

Can I merge the development branch without effecting the new commits in the master branch? i.e. not overwrite any new work in the master?

What is the best way to handle this?

4 Answers

Assuming you guys are working on separate aspects of the project (and depending on your IDE) you can typically specify the files you want to commit/push. I'd suggest pulling from master first and then copy over your changes and commit yours. Then create a pull request and merge the branches. It is always better to play it safe when working with git even if it takes a few more steps.

No, the principle of merges in git is additive, you can't "overwrite" commits with a merge.

Initial situation

A---B---C---D <<< master
     \
      E---F <<< development <<< HEAD

Your coworker has made commits C and D. Let's now merge development into master.

git checkout master
git merge development

A---B---C---D---G <<< development, master <<< HEAD
     \         /
      E-------F

At this point, commits made on your development branch are reachable from either branch, but the commits C and D are still here.

I for one hate manually merging code. I prefer to merge master into my dev branch first. This way, if I mess anything up, the resulting code is in my dev branch, not in master. This also has the benefit of making the code review (which would happen before the merge took place in my case) much easier on the reviewers.

For example:

A---B---C---D---G <<< development, master <<< HEAD
 \          \   /
  -----E------F

An alternative way to handle this problem on your local workspace is to rebase your local branch. this would look like the following:

Inital State:

A---B---C---D <<< master
     \
      E---F <<< development <<< HEAD

After Rebasing your branch should look like this, if an merge conflict would occur, you will see it and can solve it locally, afterwards you can commit as usually.

git rebase master
A---B---C---D <<< master
             \
              E---F <<< development <<< HEAD

Notice that your branch is not commited yet, just up to date with the master branch.

Related