简体   繁体   中英

Merge changes from branch into different branch without merging the branch

I had a process in place where I would work on separate 'development' branches then merge them into the 'stable' master branch when finished. I would then create a new branch for the next development branch created from master. This ensured each development branch would always contain the most recent stable changes. But I made a mistake. I forgot to merge a development branch into master, but then I created a new branch from master and worked on it a lot. So this new branch that has a lot of changes is not up to date with the latest stable changes.

I went ahead and merged the previous development branch into master - the one I forgot to merge. Now I need to merge the changes from master into my newest development branch. But I don't actually want to merge the master branch into the dev branch, bringing along all of its history. I want these branches to remain separate, I just want to compare the differences between the two branches and merge the appropriate code from master into the dev branch as a single new commit. I know there will be conflicts. How can that be accomplished?

If possible, I would prefer to do this visually with Xcode's Source Control to compare and select the changes, but if not using Terminal is just fine.

If I understand your scenario:

  • You created a feature branch off of master.
  • You merged new work into master ("new" is being used here as work that differs from when you created the feature branch).
  • You now want to ensure that your feature branch is up to date with what's currently on master, but don't want to actually merge master in.

What you can do in this scenario is rebase your branch against master.

# When on your feature branch:
git rebase master

This will ensure that the work you have done beforehand appears after any new work that exists on master. It also rewrites history, so if you've pushed that feature branch at any point, you'll have to force-push it to ensure that the correct history is maintained.

To clarify my last point: when you rebase, you overwrite the history of your commits by creating a commit that's similar to another one, but isn't the same as it (different ancestor).

The main guiding principle here is that history has changed, and if that history has been published, you will want to ensure that the history with the rebased changes are published instead so that history appears as it should with respect to the rebase operation.

You may want to use the --squah modifier when merging. This will merge the code but will not create a commit merging the histories of the two branches.

See http://git-scm.com/docs/git-merge

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