2

Tried to see few dozen posts and still not getting what I want.

Preface: We initially had a very big SVN repository with no trunk (standard structure) etc. So, the project I was working had no structure too (inside the sub-folder). I worked on this project for about couple of months and then came across a situation where I needed a branch for production fix. So, I restructured only my project code into trunk, tags and branches (i.e. sub-directory within the SVN repository). Then from history I created a branch named Live and did a fix for production and deployed it. I then merged this fix into my trunk with no issues. We kept on working on trunk and also kept on deploying, but due to unnecessary complexities of SVN didn't merge trunk to Live even though we had few deployments. (This is all in SVN)

Migration: We recently decided to setup a new TFS server and have our version control via GIT. So, after the installation was done, I had a task to migrate the code only for this project to GIT. I started doing it on my local PC using "git svn clone" and was happy to see all my history was migrated. Have branches as:

  • master: originated from trunk and I can see the history from the time I had re-structured it in SVN.
  • Live: Goes back from the beginning of the SVN repository (project folder creation date) and marked as branch name properly.

The problem is the branches are disconnected (no joining node) :-(

Please see image below:

enter image description here

As per the above image, the master branch starts at the highlighted commit (node) but Live branch just starts from the SVN 1st commit.

Ideally I would have wanted to have everything on master and Live shown as branch from master when it was really created. Also, when I try to checkout Live branch, I get warning that it is detached HEAD and I may not get/push the commits to master (i.e. HEAD) branch.

Can someone send me the steps to fix this scenario, please?

Possible solution, I think:

  • Rename the master to svn-trunk and Live to SVN-Live
  • Then create a branch named master from SVN-Live (will it have any conflict at remote?) at the commit when I had done restructure (one before highlight i.e."Live 2656").
  • Merge SVN-trunk on Master.
  • Create new branch named Live from this HEAD (as currently the final code is in production).

The process may impact negative, so I'm cautious.

  • My only worry is will it allow me to merge the commits from disconnected SVN-trunk to my new master? (Right now Git Extensions show me the option to merge Live to Master but not other way round.
  • Also, if I do trial and error locally and screw up things, how should a reset back to current state?

Update: Renamed the branches and Created master based on the "Live@2656". Now, trying to merge "Svn-trunk" to master (new branch) - I get below error:

fatal: refusing to merge unrelated histories
Done
phd
  • 82,685
  • 13
  • 120
  • 165
NikhilP
  • 53
  • 2
  • 11
  • About your last question, I believe you could find an answer here: https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit – Leonardo Alves Machado Aug 23 '17 at 14:47
  • Thanks @LeonardoAlvesMachado, but I think I'm not too worried about reset. I can use hard reset to go back to my current state, but can't see an option to merge master to Live and hence my 2 branches are not having a single tree to work with. I need a solution to fix these disconnected branches (as shown in the image) – NikhilP Aug 23 '17 at 15:30
  • Don't worry that much. While being on local, anything can be fixed in git (see reflog). Or you can always clone the svn again. – petrpulc Aug 23 '17 at 20:13
  • 1
    Unrelated histories cannot be merged but can be rebased. If there is any real common point in the history, just rebase all new revisions to that point of history. And branch you have! – petrpulc Aug 23 '17 at 20:15
  • Thanks @petrpulc. I'm not too worried about reset anymore. I've already tried few things and no luck (as you clearly mentioned we cannot merge unrelated histories). On SVN, if I had the restructure done at revision 10, it means both my branches are same till revision 9 (But trunk doesn't trace back to 9 as its unrelated). My Live is on revision 14 but trunk is on say 21. Can you pls put in answer section how and what to rebase on what branch (with brief steps). Frankly even if I lose history on Live I'm fine, I just want all the revisions pre & post trunk on master. – NikhilP Aug 24 '17 at 08:25

3 Answers3

2

Maybe start form a fresh git svn clone as it may save some headache. Also it is not a bad idea to keep a copy of such acquired git repository somewhere safe :)

Still there are two ways you may try...

Possibility #1 - fix the structure

A cleaner way would be to reconstruct the real structure of the repository. If I understand correctly, trunk was created at some point in history. If you are able to find that spot in the history you may be able to use git rebase <SHA of that spot in branch Live> to reconnect the histories together. Some conflicts may appear, though.

However, if you have merged some development progress back to Live while on SVN you may have a hard time while merging. To circumvent this you may reconstruct these merges as proper merge commits but it may not be worth the time if you do not have to have a proper history...

Possibility #2 - just rebase

Or a much simpler solution if you do not care about the proper history at all...

git checkout master
git rebase Live

The only thing you need to be certain about in there is the resolution of possible conflicts.

petrpulc
  • 940
  • 6
  • 22
2

For a one-time migration git-svn is not the right tool for conversions of repositories or parts of repositories. It is a great tool if you want to use Git as frontend for an existing SVN server, but for one-time conversions you should not use git-svn, but svn2git which is much more suited for this use-case.

There are plenty tools called svn2git, the probably best one is the KDE one from https://github.com/svn-all-fast-export/svn2git. I strongly recommend using that svn2git tool. It is the best I know available out there and it is very flexible in what you can do with its rules files.

You will be easily able to configure svn2gits rule file to produce the result you want from your current SVN layout, including any complex histories like yours that might exist and including producing several Git repos out of one SVN repo or combining different SVN repos into one Git repo cleanly in one run if you like.

If you are not 100% about the history of your repository, svneverever from http://blog.hartwork.org/?p=763 is a great tool to investigate the history of an SVN repository when migrating it to Git.


Even though git-svn is easier to start with, here are some further reasons why using the KDE svn2git instead of git-svn is superior, besides its flexibility:

  • the history is rebuilt much better and cleaner by svn2git (if the correct one is used), this is especially the case for more complex histories with branches and merges and so on
  • the tags are real tags and not branches in Git
  • with git-svn the tags contain an extra empty commit which also makes them not part of the branches, so a normal fetch will not get them until you give --tags to the command as by default only tags pointing to fetched branches are fetched also. With the proper svn2git tags are where they belong
  • if you changed layout in SVN you can easily configure this with svn2git, with git-svn you will loose history eventually
  • with svn2git you can also split one SVN repository into multiple Git repositories easily
  • or combine multiple SVN repositories in the same SVN root into one Git repository easily
  • the conversion is a gazillion times faster with the correct svn2git than with git-svn

You see, there are many reasons why git-svn is worse and the KDE svn2git is superior. :-)

Vampire
  • 35,631
  • 4
  • 76
  • 102
1

I had a similar issue where some (not all) SVN branches and tags were disconnected from the trunk.

Rebase wasn't working for me, but I fixed them by rewriting history.

  1. find the points in trunk where the branches/tags were supposed to be attached.
  2. for tags, simply drop existing tag and recreate it on the correct commit.
  3. for branch you need to report all the commits using cherry-picks. I wanted to preserve the original commit date so that when looking at the history commits don't look like they were done 2 days ago. For this you can use the GIT_COMMITTER_DATE variable.

You first need to export the variable:

# create and checkout a new branch where the disconnected branch should be inserted in trunk
git checkout -b <name of the branch> <sha where to insert>

# generate the list of commands to execute
git log <your_disconnected_branch> | grep -P "(^commit|^Date:)" | tac | sed 's/commit /git cherry-pick /g' | sed 's/Date:   \(.*\)$/GIT_COMMITTER_DATE="\1"/g'

# copy the output of the previous command

# export the GIT_COMMITTER_DATE variable
export GIT_COMMITTER_DATE

# paste the output of the log command, it should look like the following (without leading # sign):
#GIT_COMMITTER_DATE="Wed Jun 1 17:18:55 2016 +0000"
#git cherry-pick 54f30ac8071b5f935bf12595ab922542ce9d348e
#GIT_COMMITTER_DATE="Wed Jun 1 17:20:08 2016 +0000"
#git cherry-pick 5188d54f6d4bf09b06a108a887fdc6ec84f68919
#GIT_COMMITTER_DATE="Wed Jun 1 17:21:22 2016 +0000"
#git cherry-pick d47cbcfbd3fbd4c92e68fa2cfe92b555c7abaf8c

Explanation of the git log command:

  1. git log your_disconnected_branch ----> get a list of all commits in the disconnected branch.
  2. grep -P "(^commit|^Date:)" ----> remove all lines except the ones containing the commit id and Date.
  3. tac ----> Reverse the order of the file so that commits are inserted in the reverse order. This has the additional benefit of setting the commit date before making the commit.
  4. 's/commit /git cherry-pick /g' ----> convert the commit id line into a cherry-pick
  5. sed 's/Date: (.*)$/GIT_COMMITTER_DATE="\1"/g' ----> convert the date line into an assignment of the GIT_COMMITTER_DATE variable

In the end, don't forget to clear your GIT_COMMITTER_DATE with a

GIT_COMMITTER_DATE=

to prevent all your future operations from appearing very old.

scharette
  • 605
  • 1
  • 9
  • 25