So I've converted an SVN repository to a git repository using svn2git, because I assumed there wouldn't be any more work on the svn repository. However, someone else didn't know that we were switching to git (it's a bit complicated, but he was basically just working on one specific file) so he made some commits to SVN. Now I want to get these commits into git. Can I still do this, or is svn2git completely one-way?
If not, is there a way to generate patches from SVN commits, sort of an analog to git format-patch
? I can't seem to find one from a cursory search; it seems like the SVN developers never thought you would want to convert commits into patches.
svn2git now supports this with --rebase. From the (current) documentation:
As of svn2git 2.0 there is a new feature to pull in the latest changes from SVN into your git repository created with svn2git. This is a one way sync, but allows you to use svn2git as a mirroring tool for your SVN repositories.
The command to call is:
$ cd <EXISTING_REPO> && svn2git --rebase
From your description it sounds like there were only a few commits in Subversion that you want to pick up. The most direct method is to extract and apply each patch manually, which I'll outline here. First, you can use the svn diff
command to extract the diffs (a "diff" is functionally equivalent to a "patch").
svn diff -r 101:102
This will show the diff between revision 101 and 102. You can redirect this output into a file for use in the next step:
svn diff -r 101:102 >r102.patch
In the Git repository, take the diff from the previous step and apply it using the patch
program:
patch -p0 <r102.patch
The -p0
tells the patch
program how to look for the file(s) named in the patch. Finally, commit the changes to Git:
git add -u
env GIT_AUTHOR_NAME="new name" GIT_AUTHOR_EMAIL="new email" git commit
The GIT_AUTHOR_*
environment variables allow you to preserve the original author information, which may be useful for history maintenance.
The above should work easily enough for text files, but if any changed files in Subversion happen to be binary files, you'll have to use a slightly different technique (Subversion will refuse to produce a useful diff in this case). Instead of extracting patches and applying, just copy the file(s) from each Subversion revision to the Git repository before committing. This would actually work just fine for text files too, if you prefer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With