0

I have 2 branches: develop & master. Master is always behind develop. Now I created a new branch called new-branch, which is essentially develop but has different source hierarchy (for example, Java package naming was totally changed), but functionality/logic remains the same. Then I would like to continue development in both branches for a while. What I usually do - push commit into develop and then cherry-pick it to new-branch, and fix any possible conflicts.

Question1: Is there any better way to do this if I really need to keep both branches? Merging/rebasing seems to not work here because it merges entire source tree.

Now after a while I decided that develop branch could be replaced by new-branch. And later on master should also contain same changes. What I usually do - just delete develop & master and rename new-branch to develop and make master point to develop.

Question2: Is there a better way? Merging them together (merge new-branch to develop) at this point is quite difficult and probably unnecessary - so I decided just to replace branches brutally.

jozols
  • 560
  • 7
  • 22
  • 1
    `git checkout develop;git merge new-branch -s ours`. `-s ours` merges the history of new-branch into develop but introduces none of its code. Note that it's different from `-s recursive -X ours`. – ElpieKay Aug 02 '17 at 09:45
  • Thanks, managed to use this for my 2nd question: https://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch `git checkout new-branch` `git merge -s ours develop` `git checkout develop` `git merge new-branch` `git checkout master` `git merge develop` – jozols Aug 03 '17 at 17:19
  • Question 1 is still active. – jozols Aug 07 '17 at 07:36

1 Answers1

1

If it's not necessary version control them in git. You just need to ignore the packages in .gitignore file.

Detail steps as below:

  1. In each branch, create a .gitignore file by touch .gitignore.
  2. Add the files/paths of packages separately in .gitignore for each branch.

    Such as on develop branch, java package located in a/b/* of the root repo. On new-branch, java package located in c/d/*. You just add the relate files/paths in .gitignore.

    In develop branch, the content of .gitignore should be: a/b/*. In new-branch, the content of .gitignore should be c/d/*.

  3. Commit changes on each branch.
  4. Remove packages from version control history.

    In each branch, remove the package files/path you add in .gitignore by git remove /path/or/file --cached.

    Such as on develop branch, you can use git rm a/b/* --cached. on new-branch, you can use git rm c/d/* --cached.

If you need to version control the packages, you can use cherry-pick to apply the latest changes to another branch. Or merge/rebase a branch to another.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • I think this doesn't help much in my case. Java packages was just an example. Merging single folder doesn't help my problem, as changes are usually across same files/folders but on different branches where each file/folder may have different structure. – jozols Aug 07 '17 at 07:38
  • So what's the reason for keeping files about the package totally in different structures? – Marina Liu Aug 07 '17 at 07:50
  • For example, I might need this when migrating to new solution. I actually have Android project which uses Java packages and if you change just package name it infects lot of sources. Package change is just an example, sometimes there are other kinds of refactoring going on. – jozols Aug 07 '17 at 07:54
  • For this reason, do you want to keep the package folders are not be replaced when merging from another branch? – Marina Liu Aug 07 '17 at 08:05
  • Yes, I want to keep all structure intact, just merge functional changes. Cherry picking seems to be only way as it just merges commit not entire branch. – jozols Aug 07 '17 at 08:34
  • You just need to add the files or paths of packages in `.gitignore`. And it's not necessary to version control them. – Marina Liu Aug 07 '17 at 08:55
  • I can't remove my sources from version control as they are needed. – jozols Aug 07 '17 at 09:11
  • It's just remote the packages in version control. Actually the packages will still exist in current directories. – Marina Liu Aug 07 '17 at 09:13
  • Of course I need them in repository for others to access. – jozols Aug 07 '17 at 09:17
  • If so, you should resolve the merge conflicts for the whole source. There is no way to merge part of files from one branch to another. – Marina Liu Aug 07 '17 at 09:19
  • With cherry-pick I need to resolve only parts of code which are changed only in that commit. – jozols Aug 07 '17 at 09:27
  • 1
    Yeah, cherry-pick/rebase is also ok. – Marina Liu Aug 07 '17 at 09:29