In these days I’ve heard lot of rumors around Git. After reading some manual/tutorial/guide I discovered that it can be really useful, especially if you spend lot of time coding off-line (that’s my situation).

This is a really small howto that describes how to work on a project versioned with svn (maybe taken from KDE repository ;) ) using git.

What’re the advantages?

Since Git is a distributed revision control system (while svn is a centralized one) you can perform commits, brances, merges,… on your local working dir without being connected to internet.

Next time you’ll be online, you will be able to “push” your changes back to the central svn server.

Steps to follow:

You’ve to:

  1. install git and git-svn
  2. create the working dir: mkdir strigi
  3. init your git working dir:
    cd strigi && git-svn init https://svn.kde.org/home/kde/trunk/kdesupport/strigi
    git-svn init command is followed by the address of the svn repository (in this case we point to strigi’s repository)
  4. Find a commit regarding the project (you can get it from cia version control). Warning: the command git-log will show project’s history starting from this revision.
  5. Perform the command git-svn fetch -rREVISION
    Where REVISION is the number obtained before.
  6. Update your working dir: git-svn rebase

Now you’ll be able to work on your project using git as revision control system.

To keep update your working copy just perform:

git-svn rebase

You can commit your changes to the svn server using the command:

git-svn dcommit

In this way each commit made with git will be “transformed” into a svn one.

Solve git-svn rebase problems

While adding new cool features to your program, you may experiment some problem when synchronizing with the main development tree. In fact you have to commit all local modifications (using the git-commit command) before invoking git-svn rebase.

Sometimes it isn’t reasonable since your changes are not yet ready to be committed (you haven’t finished/tested/improved your work). But don’t worry, git has a native solution also for this problem, just follow these steps:

  1. put aside your changes using the command: git-stash
  2. update your working copy using: git-svn rebase as usual
  3. take back your changes typing: git-stash apply
  4. clear “the stash” typing: git-stash clear

After the first step all your uncommitted changes will disappear from the working copy, so you’ll be able to perform the rebase command without problems.

For further informations read git-stash man page.

That’s all.

Greetings: I would like to thank Thiago Macieira for his help.

Tags: , ,
34 Responses to “Howto use Git and svn together”
  1. Really? The git commits do not get merged into one SVN commit? So git-svn preserves my local (git) history? That is cool!

  2. [...] there’s a “cleaner” solution, if you’re interested read the last part of my git-svn howto. Tags: , git, Howto, KDE, [...]

  3. [...] these instructions exist in one piece anywhere else. UPDATE: As it turns out, I practically stole this. Flavio’s version is better anyways. I recommend his blog to all KDE [...]

  4. fpauser says:

    How to create a tag in svn-repository using git-svn/git?

  5. [...] Howto use Git and svn together | Flavio Castelli [...]

  6. Thanks this is a pretty cool tool.

  7. Allen Winter says:

    Hi Flavio,

    Would you be willing to copy this onto Techbase?

  8. Allen Winter says:

    nevermind. I just found http://techbase.kde.org/Development/Tutorials/Git

    you might review that tutorial and see if there’s anything to add

  9. [...] Actualmente uso GIT y estoy aprendiendo más sobre el, a mi gusto es muy rápido y me da muchas libertades aun pesar de no ser centralizado, espero en un futuro se integre más a los IDE’s pero mientras tanto estoy contento con la línea de comando, por si su equipo usa SVN, aquí un tutorial de como usar GIT mientras tu equipo usa SVN, GIT+SVN [...]

  10. [...] a good’un. Thanks to Flavio Castelli Possibly related posts: (automatically generated)Partial git clone of SVN subdirectoryGIT – The [...]

  11. [...] How to Use Git with SVN — A blog article showing how to use git when interacting with an existing svn repository. [...]

  12. pj says:

    Actually the second step did not exactly behave as you described.

    When I did a ‘git-svn fetch -rREVISION’ ,

    ‘git log ‘ shows only the comment for that revision. Not all the revisions starting from that revision as you implied. Which is the behaviour I want too.

    Any ideas?

  13. Mauvis says:

    Just an FYI for others – if you experience any errors after running “git svn init” (such as a closed connection during a big import), you can continue with your import using:

    “git svn fetch”

  14. Vikas says:

    Great article. Git is now my svn client.

    One question. Why can’t i do `git svn fetch` instead of `git svn rebase` for synchronizing with the main development tree?

    • Ruud says:

      Because you most times only want to quickly get the latest revisions and worry about merge problems later; use fetch. With rebase you first fetch and then merge HEAD with your working tree, with possible merge problems.

  15. [...] transferred all my repos over from Subversion to Git and have become familiar enough with the Git SVN bindings to even work on my work projects in Git while still being able to check-in as a regular SVN [...]

  16. Fernando says:

    Hi, I’m working in a project that is officially maintained in a SVN repository. What I’m doing now is the following:

    1) make local copy using ‘git svn clone’ from the SVN repository (which I delete after (2)).
    2) make local copy again to a different directory using just ‘git clone’ from (1).
    3) since I work in different computers (work/home/laptop) I have a local git-repository in each of them that I maintain doing pulls from one to another every time I switch computer.
    4) in the meantime the SVN repository gets updated by other people contributions.

    Now in order to merge all the changes I’ve been working on with the latest version of the SVN repository I do the following:

    1) I get a fresh copy of the SVN repository using ‘git svn clone’ (new directory)
    2) pull it from the latest local git-repository using ‘git pull –rebase’
    3) fix all the conflicts and continue merging until is done.
    4) pull the new merged version into the SVN copy with ‘git pull’
    (this runs smoothly — no complains at all)
    5) upload the changes to the SVN repository using ‘git svn dcommit’

    I found this procedure really messy. Is there any other more-cleaner way to do this ?

    Thanks a lot,
    Fernando

    • Pradeep says:

      I follow this procedure :
      1. git svn clone on the server
      2. git clone on my local machine
      Do all the development on the local repository
      To keep track of latest version of repository I do :
      1. git svn init
      2. git svn fetch
      3. git svn rebase
      Hope this helps

  17. [...] en forma rotunda por su velocidad y poca utilización de espacio extra en el disco*. Acá parece haber un tutorial cortito para utilizarlo (Google me devuelve muchísimos enlaces más por [...]

  18. nomex says:

    Wow Great article. Git is now my svn client.

    One question. Why can’t i do `git svn fetch` instead of `git svn rebase` for synchronizing with the main development tree?

  19. Prosto says:

    I like this article! Thanks
    Would you be willing to copy this onto Techbase?

    And is it good to use them together?

  20. Jocelyn says:

    I was wondering if this would not be easier to work on its own branch
    something like that

    > git init ….
    > git fetch -r 12345
    > git rebase
    > git checkout -b my_branch master
    then .. work on your own branch using git
    In the meantime, you can always update (rebase) the master branch
    and when you are ready to commit, merge your changes to the master branch, and use the git svn dcommit.

    It might be safer this way, … may be …
    Any comment on this?
    (Note that I am not currently using git svn, but I am thinking about it.)

  21. Horny says:

    Hi, I’m working in a project that is officially maintained in a SVN repository. What I’m doing now is the following:

    1) make local copy using ‘git svn clone’ from the SVN repository (which I delete after (2)).
    2) make local copy again to a different directory using just ‘git clone’ from (1).

  22. Daily says:

    Great article. Git is now my svn client.

    One question. Why can’t i do `git svn fetch` instead of `git svn rebase` for synchronizing with the main development tree?

  23. [...] Howto use Git and svn together (Flavio Castelli) [...]

  24. For some reason my browser doesn’t display this page correctly…However,

  25. [...] Howto use Git and svn together | Flavio Castelli (tags: versioning git) [...]

  26.  
Leave a Reply