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.

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, this was really specific situation. I suggest you post a question if you are having trouble :(
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)
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.
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.

1 Comment

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.
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

4 Comments

svn: invalid option: --git
@Nik I guess that your svn client is too old. What's the result of svn --version ?
svn version 1.6.13
@Nik upgrade to svn 1.9
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.

5 Comments

feel free to add a post-commit hook to do an automatic push after commit :)
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 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
I had to use patch directly in the end -- I was just disappointed that git apply patch wasn't able to do the same
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
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.