54

I have my projects in 2 repositories. One under SVN and one under Git. Whenever I change something in SVN I want to do the same thing to the Git repository.

Say I make a change to SVN repository, producing revision 125. How would I apply these same changes to my Git repository (assuming my Git repository is up to date with revision 124).

Thank you.

bahrep
  • 29,961
  • 12
  • 103
  • 150
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272

6 Answers6

55

What I actually did for was:

cd /path/to/svn/repo
svn diff -r 125 > /tmp/patch.diff
cd /path/to/git/repo
patch -p0 < /tmp/patch.diff
2240
  • 1,547
  • 2
  • 12
  • 30
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272
  • 1
    Sorry, this was really specific situation. I suggest you post a question if you are having trouble :( – Andriy Drozdyuk Feb 15 '11 at 00:02
  • 2
    In general, git-svn is a better way of importing (and exporting) svn commits into git repositories. It will keep the commit message and author info, and cope with many edge cases which will break the answer given here. (Of course, this script may be more appropriate for your situation if there are external constraints you haven't discussed here) – Rich Apr 16 '12 at 12:39
  • 6
    It would be more correct to use `svn diff -c 125` for the second line. In this way you will get the change of revision 125 and not the change of this revision from the current code. – amotzg Dec 09 '12 at 08:36
19

Try:

svn diff | patch -d /path/to/git/repo -p0

See svn help diff if you want to export a specific revision's diff.

MattJ
  • 7,924
  • 1
  • 28
  • 33
  • See -pnum in http://linux.die.net/man/1/patch In particular search the page for words "not specifying -p". I think if you don't specify it - patch will ignore the filepath and just use the filename. – Andriy Drozdyuk Oct 23 '10 at 22:56
8

If you are going to generate a patch in SVN and apply it with Git later, don't forget to use --git command-line option:

--git

Enables a special output mode for svn diff designed for cross-compatibility with the popular Git distributed version control system.

For example, run

svn diff --git -r 125 > /tmp/patch.diff

bahrep
  • 29,961
  • 12
  • 103
  • 150
5

Why does no one like git-svn? I cannot assume no-one knows about it.

There is git-svn (and git-hg and git-cvs and git-bzr afaict). At least with git-svn you can simply do

git svn clone --stdlayout http://myrepo/root here

using -s (--stdlayout) assumes standard trunk/ branches/ tags/ layout, but you can have it any which way (man git-svn).

The mapping is bidirectional, so you can push and pull as with a native (git) remote. No questions asked.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • feel free to add a post-commit hook to do an automatic push after commit :) – sehe Mar 16 '11 at 15:33
  • This doesn't work for me with svn-git on Windows as `git apply` fails because it can't parse the relative paths of svn-style diffs correctly. – the_mandrill Feb 11 '13 at 12:14
  • 1
    @the_mandrill erm... use `patch -p1` (or -p0, or whatever it is you need to strip)?. Oh wait. Oooold question. The point is that you don't need svn style diffs. `git cherry-pick` – sehe Feb 11 '13 at 12:21
  • I had to use `patch` directly in the end -- I was just disappointed that `git apply patch` wasn't able to do the same – the_mandrill Feb 11 '13 at 12:35
  • 3
    But that has little to do with git-svn. Git-svn enables you to work with a Subversion repository ***transparently*** (as a non-native remote). If you are going to pass SVN-style diffs _outside of git-svn_ you shouldn't blame git-svn if that doesn't work. It just has nothing to with it – sehe Feb 11 '13 at 12:54
2

Besides using patch as mentioned above you could also consider setting up a post-commit hook so you don't have to do this every time you commit something new.

Federico Builes
  • 4,939
  • 4
  • 34
  • 48
1

The below worked for me.

Source:How to create and apply a patch with Git

First, take a look at what changes are in the patch. You can do this easily with git apply

git apply --stat fix_empty_poster.patch

Note that this command DOES NOT apply the patch, but only shows you the stats about what it’ll do. After peeking into the patch file with your favorite editor, you can see what the actual changes are.

Next, you’re interested in how troublesome the patch is going to be. Git allows you to test the patch before you actually apply it.

git apply --check fix_empty_poster.patch

If you don’t get any errors, the patch can be applied cleanly . Otherwise you may see what trouble you’ll run into.

To apply the patch, I’ll use git am instead of git apply. The reason for this is that git am allows you to sign off an applied patch. This may be useful for later reference.

git am --signoff < fix_empty_poster.patch

Applying: Added specs to test empty poster URL behaviour
Applying: Added poster URL as part of cli output

Okay, patches were applied cleanly and your master branch has been updated. Of course, run your tests again to make sure nothing got broken.

In you git log, you’ll find that the commit messages contain a “Signed-off-by” tag. This tag will be read by Github and others to provide useful info about how the commit ended up in the code.

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62