I am trying to push a new IntelliJ project into a new Git repository on Bitbucket, but it fails with a “divergent branches” message. I really want to use IntelliJ and not step out to the command line for basic version control actions, but it doesn’t seem possible.
This is what I have done:
- I created a new repository in the Bitbucket web application
- I created a new project in IntelliJ: A Kotlin project with Gradle
- In IntelliJ, I selected VCS / Enable version control integration
- In IntelliJ, I went to Git / Manage remotes and added a new remote using the URL from the Bitbucket Clone button.
- In IntelliJ: Git / Fetch
- In IntelliJ: Git / Pull: Now I get the “You have divergent branches and need to specify how to reconcile them” message.
- I ran this on the command line: git config pull.rebase false
- At this point I get into a loop about “divergent branches” or that “main has no tracked branch”.
I am using: IntelliJ IDEA 2024.1.5 (Ultimate Edition)
2
It seems that you have created one or more commits in bitbucket and in IntelliJ, i.e. locally. And since both start from an empty repository, they don’t have a common ancestor and therefore can’t be merged as on would normally do.
Depending on where you start you have various options how to get out of this:
-
Avoid creating any commits in Bitbucket or IntelliJ before setting up git and the remote.
This should avoid the whole problem completely. I haven’t used Bitbucket in ages, but I guess it allows to create stuff like READMEs on repository creation. These are introduced by commits, which will cause the problems you described. -
if you have commits on both sides and already tried the stuff you mentioned above follow this process to
a) fix the situation
b) understand what is going on.
-
open the Git view in IntelliJ (
CMD-9
on MacOS)You should see two branches. Probably called
main
andorigin/main
containing the local and remote commits respectively. -
Use IntelliJ to check out
origin/main
and create a new branch. I assume you name itwork
for now. Make sure that branch is checked out. -
In the Git view select the oldest local commit and
cherry-pick
it. Repeat for all local commits, starting with the oldest. Repeat for all local commits. -
Checkout
main
again. Perform a reset towork
. Choosehard
when asked how to perform the reset.
-
-
Now perform a git push on
main
.
All commands of course can be performed on the command line, and I actually recommend using the command line a lot for Git. Because it is faster, in my opinion often easier to understand, and I’m old.
Also you should be able to do the same thing with rebase
but I have a hard time remembering the correct arguments for these cases.
1