简体   繁体   中英

How to fix 'git-svn' branch and 'origin/master' have diverged

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.

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 master s and it would be more work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM