简体   繁体   中英

How to merge my branch to master if some other branch has been merged into the master

I'm new to using git with teammates and I don't understand the workflow very well yet.

Let's say master branch is ABC .

  1. I branch it into dev1 and develop on it and it was now ABCD .
  2. Someone branch it into dev2 and his final value was ACCDE .
  3. Then dev2 merged into master and master branch final value is ACCDE .

Since dev1 was working on ABC version of master so, how to continue development on dev1 which depends on previous version of master when master value was changed from ABC to ACCDE.

Added info: I was still working on dev1 so I can't merge dev1 to master yet, and then dev2 which was edited and added with new features was merged into master. The problem is the feature I was working on on dev1 depends on unedited version of master and now the master was changed and I no longer can work on my development on dev1 Thanks

You have to merge dev1 into master and resolve conflicts.

You need to rebase your dev1 branch on master (and resolve any potential conflicts).

git checkout dev1
git rebase master

See Git Branching - Rebasing .

So, you have three branches:

  • master
  • branch1
  • branch2

There were commits done inside branch1 and branch2. Let's assume that you work on branch2 and branch1 is already merged into master. You could do the following

git checkout master
git merge branch2

However, you may have conflicts if both branches worked on the same files. If you have such a conflict, then you can run

git status

in order to see what the conflicting files are. Opening each and every file you will see sections like

<<<<<<<
=======
>>>>>>>

The first line above signifies the first branch and the last line signifies the end of the last branch. Between the lines you will find the different versions. You will need to figure out which are the correct lines from each and perform modifications until the code works. Finally, remove the lines above along with the content in-between.

However, you may want to avoid solving the conflicts inside master, because that's central and it is better to localize problems in my opinion, so what I actually do, assuming that I'm on branch2:

git status
git add myfile1
git add myfile2
git commit
git pull
git push
git checkout master
git pull
git checkout branch2
git merge master

If there are conflicts, then solve them and then

git checkout master
git merge branch2

EDIT:

Apparently there was an earlier version of master which differs from branch2, yet, branch1 would add commits that are not desired at this point. So solve it, first:

git checkout master

to view what's in master. Then see what the commits into master were via

git log

You will see a list of commits. Find the newest commit that's still good and you will see its hash, which is a long alphanumeric string. Let's call id for now, but you will need its actual value. Now switch back to your branch:

git checkout branch2

and merge that commit into your branch

git merge <hash>

Another option is to merge the current version of master into your branch. You will have to live with those commits eventually, so you might as well merge them into your branch sooner rather than later, to make the merge easier.

Your working branch is dev1. Here is the process I use.

Get the latest version of master

git checkout master

git pull origin master (I like to be specific about what I pull. It keeps my head on straight)

Now that your local copy of master is up to date, merge master into your branch. (This is different than merging your branch into master. You only do that when you are completely done with your branch.)

git checkout dev1

git merge master (If there are conflicts, resolve them as described by Lajos)

If other developers continue to work on master, then you may need to do this periodically. I prefer to merge master into my dev branch frequently so that at the end of developing my branch I don't have a lot of merge conflicts, which can happen if a lot of development has been done on master.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM