简体   繁体   中英

Git and SVN via git svn branch merging. Which procedure is the best?

I'am using git svn to sync sources between git and SVN. I like to work with branches and that's the problem.

I'm creating a branch with: git svn branch mybranch

After that i check the branch out. After i modified the sources i'm ready to merge mybranch to the master branch.

How are you doing it? git merge does not work and git merge --no-ff creates a single commit. Are you using cherry-pick to pick those commits? How you integrate your changes from mybranch to master without losing the commits you created in mybranch?

Thank you very much!

Branching and merging in SVN is significantly different from branching and merging in Git. Git's is far superior. SVN cannot handle most of the merges git does. Here is how I would recommend merging be done:

case 1) Creating a personal feature branch then merge it back into a shared SVN branch

In this case, create the feature branch only in Git. Commit as necessary in git. Feel free to rebase/squash as often as necessary. When you are ready to merge your changes back into the SVN shared branch:

case 1a) You want all your separate commits to be stored in SVN

Rebase your feature branch so that all its commits come after the tip of the shared branch. Then fast-forward merge your feature branch into the shared branch. This leaves a linear history in your SVN shared branch. This is important. SVN cannot handle non-linear histories. Now you can push your shared branch to SVN (be sure to update from SVN first).

git checkout myfeature
git rebase master    # we need a linear history on master, so rebase
git checkout master
git merge myfeature --ff-only  # ff-only ensures a linear history
git svn rebase  #update from SVN
git svn dcommit #finally, commit to SVN

case 1b) You only want a single commit in your SVN history.

In this case, do a merge --squash. This will add a single commit to your shared branch which you can push to SVN. Your feature branch should be deleted (or replaced with a tag if you want to keep your separate commits in git history) as git will have no history of the merge, and future merges made from the branch can cause unnecessary conflicts.

case 2) You need to merge two SVN branches.

ALWAYS merge SVN branches in SVN, and not Git. SVN cannot handle Git merges, If you merge in Git. SVN will not be able to merge those branches without problems in the future. Git should still be able to detect a merge if SVN did it.

Alternately, you can do all merges in Git. Since SVN will not know anything about these merges (it will not know that the commits are merge commits) SVN will not be able to merge those branches without a lot of manual intervention. SVN uses "mergeinfo" to keep track of which commits have already been applied to which branches, and merging in Git will not update SVN's mergeinfo. So your choice is always merge SVN branches in SVN, or always in Git.

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