Take the 2-minute tour ×

I am using git-svn to work with a svn repository. The git master branch is mirroring the svn trunk branch (the only svn branch in use), i.e. there is a one to one relation between elements on the master branch and the trunk branch.

I want to have git tags that corresponds to svn revisions, so that I can do things like for instance git diff r123 workbranch to see what I have done compared to svn revision 123.

The revisions are not tagged when I do git svn rebase, so I have created the following script that I run post svn rebase:


use strict;
use warnings;

open(PIPE, "git log master |");
# Format for the pipe output:
# ...
# commit 6b24b8fdc6a25d35b01140dc1ac054697133423d
# ...
#      git-svn-id: https://server.example.com/svn/project/trunk@594 164096ca-3471-41d1-a9ce-9541462a8c31
# ...
my $commit = "";
my $i = 0;
foreach my $line (<PIPE>) {
    if ($line =~ /^commit ([\da-f]+)/) {
        $commit = $1;
    next unless $line =~ /^\s+git-svn-id: .*\/trunk\@(\d+) /;
    my $git_svn_id = $1;
    run("git", "tag", "-f", "r$git_svn_id", $commit);
    last if $i++ == 20;

sub run {
    return system(@_);

This script gets the job done, but I have to run it manually every time, and I have (arbitrarily) capped it at checking the last 21 revisions so there is a theoretical risk of missing some (but I will see that from the printing).

The question: Is there any way I can automate this so that I just run git svn rebase and all imported revisions will be tagged in git? Any hooks I can use?

PS Yes, I am aware of svn find-rev, but I want in absolutely no way having to write commands as complicated as git diff $(git svn find-rev r123) $(git svn find-rev r124) instead of just git diff r123 r124.

share|improve this question
Why so difficult? If you don't like write complicated commands, you could create an alias. –  kan Feb 25 '13 at 15:17
- Doctor, my arm hurts when I do this. - Well don't do that then! –  hlovdal Apr 12 '13 at 17:29

2 Answers 2

Here's a crazy idea: Trap bash commands and automatically replace any r1234 strings with SHA hashes:

shopt -s extdebug

enhance_rev_parse () {
    case "$BASH_COMMAND" in
        eval $(echo "$BASH_COMMAND" | sed -e 's/r[0-9][0-9]*/$(git svn find-rev &)/g')
        return 1

trap 'enhance_rev_parse' DEBUG
share|improve this answer
This actually sort of works, but it is a bit to hack-ish and "leaks" too much. git tag -d r123 will not work for instance. And I cannot use git tag -l to get a list since they not proper tags. –  hlovdal Feb 28 '13 at 8:00
I even forgot to put git into the case pattern after testing, so it even affects other commands! –  Ben Jackson Feb 28 '13 at 18:49

Short Answer and Caveats

There is not a way to hook the git svn rebase command that I'm aware of (or git svn dcommit).

What you can do is use the post-rebase hook. However, this is only invoked when the history is actually updated by the rebase -- if you run git svn rebase and there's no new SVN commits that affect the current Git branch, no changes will take effect.

Ensure You Used --stdlayout

With regard to creating Git tags for SVN tags in a single command, if you did not originally clone the repository with...

git svn clone --stdlayout

I would recommend that you re-clone the repository. This will make your life much easier.

Telling Git About New SVN Tags

To tell Git about new SVN tags and branches, use the command git svn fetch --all. The command git svn rebase --fetch-all also performs this action.

Automatically git tag all SVN tags

In order to convert thse newly-fetched SVN tags into Git tags, one way is to just walk the the references.

git svn fetch --all &&
git for-each-ref refs/remotes/origin/tags |
while read ref
  tagName=$(echo $ref | cut --delimiter / --fields=5)
  git tag -f "$tagName" "refs/remotes/origin/tags/$ref"

Put this file as .git/hooks/post-rebase.

share|improve this answer
I could not find any reference to a post-rebase hook in neither the documentation (git-scm.com/book/en/Customizing-Git-Git-Hooks, kernel.org/pub/software/scm/git/docs/githooks.html), nor in the latest git source code. Have you extended git in some other way? –  hlovdal Feb 27 '13 at 16:27
Also note that I am looking for importing svn revisions, not svn tags. –  hlovdal Feb 27 '13 at 16:28

Your Answer


