Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging development branch after master branch has changed (committed)

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?

like image 835
Will Avatar asked Jan 26 '23 16:01

Will


2 Answers

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.

like image 89
Romain Valeri Avatar answered Jan 28 '23 11:01

Romain Valeri


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.

like image 23
A Bear Avatar answered Jan 28 '23 11:01

A Bear