2

At work I use Git as a client to SVN, using git svn. Also I push my Git repository to a remote. The SVN repository is used by a team of people in my office, while the Git remote repository is used only by me at the moment. This is all part of the process of moving from SVN to Git.

My workflow consist of committing my changes to my local Git repo, then updating my working copy from the SVN changes by other people, committing to SVN, and finally pushing to the Git remote. Something like this:

$ git commit
$ git svn rebase
$ git svn dcommit
$ git push origin

This has worked without issues.

But today, by mistake, I swapped those two last steps around, so I pushed to origin, and then I did git svn dcommit. This has caused the origin/master to diverge from master, but more importantly to diverge from the git-svn remote. Now a git log looks like this:

$ git log --oneline --decorate --graph --all -5
* 613ffa60 (HEAD -> master, git-svn) Fix typo on message displayed to user
| * db2e67ef (origin/master) Fix typo on message displayed to user
|/
* 24d8fab9 Commit C
* c0c1b598 Commit B
* e31c07c0 Commit A

For simplicity I have modified the commit messages above, but note that the two last divergent commits in master and origin/master actually contain the same change. How can I fix this?

I have tried various things like git rebase origin/master, or git reset --hard origin/master, but all I get with that is master and origin/master to point at the same commit. Like this:

$ git log --oneline --decorate --graph --all -5
* 613ffa60 (git-svn) Fix typo on message displayed to user
| * db2e67ef (HEAD -> master, origin/master) Fix typo on message displayed to user
|/
* 24d8fab9 Commit C
* c0c1b598 Commit B
* e31c07c0 Commit A

Those would be solutions if you are not using git-svn, but how do I get git-svn to also point at the same commit? Will I need an extra commit where everything is merged?

As I said before, currently I am the only user of the Git remote, so I won't mind re-writing history in the remote if necessary.

1 Answer 1

3

Just as a background: the reason you are in this situation is that git svn dcommit changes the local git commit to add the corresponding SVN revision number to the git commit message.

The solution to your problem is fairly simply. You need to make sure your git master is at the same commit as your SVN and then push force it to your git repo (origin).

Solution for Situation 1

If you're here:

* 613ffa60 (HEAD -> master, git-svn) Fix typo on message displayed to user
| * db2e67ef (origin/master) Fix typo on message displayed to user
|/
* 24d8fab9 Commit C
* c0c1b598 Commit B
* e31c07c0 Commit A

You just need to do git push --force and it will overwrite the origin/master branch with your current local branch and it will be in sync again.

Solution for Situation 2

If your here

* 613ffa60 (git-svn) Fix typo on message displayed to user
| * db2e67ef (HEAD -> master, origin/master) Fix typo on message displayed to user
|/
* 24d8fab9 Commit C
* c0c1b598 Commit B
* e31c07c0 Commit A

Do the following to bring you local master back to the SVN commit and then push force it to the remote Git repo:

$ git checkout master && git reset --hard git-svn && git push --force

Remarks

Note that git push --force is no problem only since you are the only user of you remote Git repo. If others used it as well then they would be out of sync with their respective masters and it would be more work.

2
  • It worked! My only comment would be that in "Solution for Situation 2", a simple git svn rebase brings the local master back to the SVN commit. At that point you are like in "Situation 1". So, then after that, all you need is git push --force to fix the issue.
    – alondono
    Commented Nov 7, 2017 at 5:29
  • 1
    This only works because Git detects that the change sets introduces by the two commits 613ffa60 and db2e67ef are identical and drops one. Sometimes this doesn't work and you end up with commit db2e67ef on top of 613ffa60.
    – gucce
    Commented Nov 7, 2017 at 11:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.