Ensuring Correct Remote is Used for Git Branch Push in Worktree Setup

Answered

Hi,

I use Git work trees concept and have a base repository with two remotes: base_repo and repo_1. I opened the repo_1 project and created a new branch under repo_1/DEV (a remote tracking branch) via IntelliJ. To do this, I selected the branch, right-clicked, and chose "New branch from remote name/branch name" from the context menu. A dialog popped up asking for the new branch name, titled "Create branch from repo_1/DEV," which is correct.

After checking out, I made a few commits. When pushing, I expected that since the remote tracking branch was not set for the local branch, it would create a new remote branch with the name of the current local branch and then push the commits. However, when I tried to push, the default remote was set to base_repo instead of repo_1, despite the branch being created from repo_1. It's inconvenient to manually change the remote every time I push.

Is there a solution to ensure the correct remote (repo_1) is used by default when pushing a branch created from that remote? If yes, please let me know.

Note: This issue is not observed in a regular Git setup.

Thank you!

0
5 comments

Hi,

How do you open the project exactly?

Which remote does the work tree copy have listed under “Manage remotes” (double shift, search) 

There should be no difference between opening a regular project and a worktree copy. 

 

 

0

Hi,

I opened the project in the usual way—nothing special.
In the worktree copy, under "Manage remotes", all the remotes from the parent repository are listed.

I hope my explanation above is clear. I also tested this on a smaller repo and was able to reproduce the issue. If the steps are unclear, please let me know.

When I tested this on smaller data, I realized we don’t even need a worktree. The issue is reproducible without it. Please find the updated steps below:

Steps to Reproduce:

  1. Open Git Bash on a Git repository.
  2. Ensure the repo has two or more remotes configured. If not, add another remote.
  3. Fetch a branch from one of the remotes (this is necessary for the later steps).
  4. Open IntelliJ in the Git workspace.
    • You’ll see all the available remotes and the branch you fetched.
  5. Click on the fetched branch and select the option ‘New Branch from remote_name/Fetched branch name.’
  6. A popup will appear asking for the new branch name.
    • Enter a name and ensure the Checkout checkbox is checked before clicking OK.
  7. After the branch is checked out, try to Push your changes.

Observed Issue:

When pushing, you’ll notice a mismatch in the remote name.

  • It should default to the remote where the branch was created, but it doesn't.

I’m not sure if this behavior existed before, but if there’s an issue in my steps, please let me know.
Please note: This issue occurs only once, as the newly created branch doesn’t have a tracking branch set initially. Once a tracking branch is set, this issue no longer occurs.

However, I create branches this way to avoid the manual steps of creating a remote branch, fetching it, and then setting it to a local branch. Using this option, the remote branch will be created automatically under the specified remote (inherited from the parent branch where it was created) and set as the tracking branch for the local branch. Unfortunately, this is not happening, and that’s the problem.

Thank you!

0

Hi,

I didn’t understand what you meant. Are you saying there is already a tracker for this issue, or is this an FAD?

Thank you!

0

Hi,

Yes, the issue is already opened. Please feel free to vote it up! Thanks. 

0

Please sign in to leave a comment.