How to apply SVN diff to Git?

26,057

Solution 1

What I actually did/looking for was:

cd /path/to/svn/repo
svn diff -r 125 > /tmp/patch.diff
cd /path/to/git/repo
patch -p0 < /tmp/patch.diff

Solution 2

Try:

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

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

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
26,057
Andriy Drozdyuk
Author by

Andriy Drozdyuk

I like AI and in particular Reinforcement Learning. I used to like Erlang, Scala, python, Akka and Zeromq! Did my undergrad in Computer Science at University of Toronto. Did my masters in Data Mining at University of New Brunswick. Working as a programmer at NRC and doing PhD part time at Carleton University.

Updated on July 09, 2022

Comments

  • Andriy Drozdyuk
    Andriy Drozdyuk almost 2 years

    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.

  • Andriy Drozdyuk
    Andriy Drozdyuk over 13 years
    See -pnum in 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
    Andriy Drozdyuk over 13 years
    Sorry, this was really specific situation. I suggest you post a question if you are having trouble :(
  • sehe
    sehe over 13 years
    feel free to add a post-commit hook to do an automatic push after commit :)
  • Rich
    Rich about 12 years
    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)
  • amotzg
    amotzg over 11 years
    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.
  • the_mandrill
    the_mandrill over 11 years
    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.
  • sehe
    sehe over 11 years
    @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
  • the_mandrill
    the_mandrill over 11 years
    I had to use patch directly in the end -- I was just disappointed that git apply patch wasn't able to do the same
  • sehe
    sehe over 11 years
    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
  • niken
    niken over 7 years
    svn: invalid option: --git
  • bahrep
    bahrep over 7 years
    @Nik I guess that your svn client is too old. What's the result of svn --version ?
  • niken
    niken over 7 years
    svn version 1.6.13
  • bahrep
    bahrep over 7 years
    @Nik upgrade to svn 1.9