How to git-svn clone the last n revisions from a Subversion repository?
Problem
How do you create a shallow copy with git-svn from a Subversion repository, e.g. how do you pull only the last three revisions?
The git clone
command can get the last n revisions from a Git repository if you use the option --depth
, i.e. you get a shallow copy of the repository. Example:
git clone --depth 3 git://some/repo myshallowcopyrepo
Is there a similar option for git-svn?
My discoveries so far
So far I've only found the -rN
option where N
is the revision to pull. Example:
git svn clone -rN svn://some/repo
According to the documentation there is the possibility to use -r$REVNUMBER:HEAD
. I tried the following to get the last 3 revisions which returned an error message.
$ git svn clone --prefix=svn/ -s -rHEAD~3:HEAD http://some/svn/repo .
revision argument: HEAD~3:HEAD not understood by git-svn
So I replaced HEAD~3
with the actual number of the third but last revision 534. That worked, but that requires me to first figure out the revision number of the third but last commit.
$ git svn clone --prefix=svn/ -s -r534:HEAD http://some/svn/repo .
Documentation
git-clone
git-svn
Solution 1:
You've already discovered the simplest way to specify a shallow clone in Git-SVN, by specifying the SVN revision number that you want to start your clone at ( -r$REV:HEAD
).
For example: git svn clone -s -r1450:HEAD some/svn/repo
Git's data structure is based on pointers in a directed acyclic graph (DAG), which makes it trivial to walk back n
commits. But in SVN ( and therefore in Git-SVN) you will have to find the revision number yourself.
Solution 2:
I find myself using the following often to get a limited number of revisions out of our huge subversion tree (we're soon reaching svn revision 35000).
# checkout a specific revision
git svn clone -r N svn://some/repo/branch/some-branch
# enter it and get all commits since revision 'N'
cd some-branch
git svn rebase
And a good way to find out where a branch started is to do a svn log
it and find the first one on the branch (the last one listed when doing):
svn log --stop-on-copy svn://some/repo/branch/some-branch
So far I have not really found the hassle worth it in tracking all branches. It takes too much time to clone and svn and git don't work together as good as I would like. I tend to create patch files and apply them on the git clone of another svn branch.
Solution 3:
... 7 years later, in the desert, a tumbleweed blows by ...
I wasn't satisfied with the accepted answer so I created some scripts to do this for you available on Github. These should help anyone who wants to use git svn clone
but doesn't want to clone the entire repository and doesn't want to hunt for a specific revision to clone from in the middle of the history (maybe you're cloning a bunch of repos). Here we can just clone the last N revisions:
Use git svn clone
to clone the last 50 revisions
# -u The SVN URL to clone
# -l The limit of revisions
# -o The output directory
./git-svn-cloneback.sh -u https://server/project/trunk -l 50 -o myproj --authors-file=svn-authors.txt
Find the previous N revision from an SVN repo
# -u The SVN URL to clone
# -l The limit of revisions
./svn-lookback.sh -u https://server/project/trunk -l 5