1

Is there a way with git-svn to determine how many commits ahead or behind I am?

Thanks!

Mr Mikkél
  • 2,577
  • 4
  • 34
  • 52
  • Do you mean how many commits have been made in Subversion since you took the branch you're working on, or how many commits you've made in your branch that aren't in the Subversion repository? – me_and Jun 05 '13 at 11:08
  • Yeah, behind = how many commits have been made in subversion since I either did an initial pull (clone) or since I've done a rebase. ahead = just what you said: the number of commits I've made using the git commit that I haven't git svn dcommit-ted. Thanks! – Mr Mikkél Jun 05 '13 at 15:45

2 Answers2

2

Not directly. What you can do, though, is find out how far ahead and/or behind you are from the local copy of the Subversion repository.

So start with git svn fetch to bring your local repository up-to-date, and go from there.

To find out how many commits there are in the branch you're currently on, but which haven't been committed to the Subversion server, there's a whole bunch of tricks, but the most obvious one is to run git svn dcommit --dry-run, and see how many commits are listed to be pushed to Subversion.

Short version: run git svn dcommit --dry-run | grep diff-tree | wc -l.

To work out how many commits are in the Subversion repository, but not in the branch you're working on, you need to first work out or know the name of the Subversion branch you're working from. You can probably work this out from the first line of git svn dcommit --dry-run, or from git log -1 --grep git-svn-id.

Say your branch is master, and the parent Subversion branch is remotes/svn/trunk. You can then run git svn rev-list master..remotes/svn/trunk | wc -l to count all the commits that are in the remote trunk and not in your branch.

As an alternative to all the above, when you create a branch, specify it as tracking a Subversion branch. For example, when I create a local branch based on the remote trunk, I create it as follows:

git checkout -b new-branch-name -t remotes/svn/trunk

Now, whenever I do git status or git checkout new-branch-name, the output will include a line telling me just how far ahead or behind each branch is relative to the other.

For a branch that already exists, you can set it up to track a Subversion branch as below:

git branch --set-upstream existing-branch remotes/svn/trunk
me_and
  • 15,158
  • 7
  • 59
  • 96
  • Thank you for that, sir. And especially thank you for the -set-upstream tip. (Although, it gave me a warning saying that will be deprecated in favor of --set-upstream-to=, which I ended up using.) – Mr Mikkél Jun 10 '13 at 14:22
  • Oh - and one question. Doesn't the git svn fetch do a pull, so that I won't be behind by any commits any more? – Mr Mikkél Jun 10 '13 at 14:24
  • Also, this command: "git svn dcommit --dry-run" yields the following error:update-index --refresh: command returned error: 1 – Mr Mikkél Jun 10 '13 at 14:28
  • Nevermind on the error. It's because I had unstaged changes. Which makes me think this method might not quite work (since I'm trying to build an output display that will always tell me where I am, even with unstaged changes). – Mr Mikkél Jun 10 '13 at 14:31
  • No, `git svn fetch` does a fetch, not a pull or rebase; you'll have all the remote commits locally, but your commits won't be based on the new remote commits. For that, you need a `git rebase`, or you can do a `git svn rebase` to effectively do both steps at once (although `git svn rebase` does a `git svn fetch --parent`, _not_ `git svn fetch`). – me_and Jun 10 '13 at 15:58
  • Also, I didn't know `--set-upstream` was becoming obsolete; that's not warned about in my version of Git (1.7.9). Although my version is somewhat elderly now, so that's not massively surprising. – me_and Jun 10 '13 at 15:59
  • Any thoughts about doing this without doing a dry run dcommit? – Mr Mikkél Jun 10 '13 at 19:57
  • Mr A: sure; I just use that as a convenient way to get the parent branch and the number of commits. You can get both by just looking at `git log`. – me_and Jun 11 '13 at 21:06
0

In git >= 1.9, there seems to be a bug that prevents using --set-upstream/--set-upstream-to

It doesn't work for me either in git 2.23.0

The workaround is to modify the .gitconfig manually : how-to-set-upstream-branch-in-git-svn

owford
  • 61
  • 3