0

EGit has a lovely feature where I can rename a local branch. For example, from master to fix123. The problem is that after the rename, EGit does not modify the corresponding Upstream Branch to refs/heads/fix123. The consequence is that when I push the local branch to remote it by default goes to the original location (refs/heads/master in this example).

The current behaviour is correct for Gerrit, which expects you to always push to the remote master branch no matter what you name your branch locally.

However, this is annoying when working with GitHub, because GitHub typically expects you to submit to a branch other than master so that you can then do a Pull Request (code review). So if you create your local branch based on origin/master, and give it a new name fix123, then when you attempt to push the branch to origin it will attempt to push directly onto the remote master (which will either fail, or overwrite the master branch without a code review--probably not what you wanted).

Is there a way to tell EGit, on a per-repository basis, to automatically rename the Upstream Branch when you rename the local branch?

Jonathan Benn
  • 2,908
  • 4
  • 24
  • 28
  • Have you tried to rename the remote(!) branch in the _Git Repositories_ view? I guess you have to do a force push for the renaming. To my understanding, the naming of the local branches is up to you and something the remote repository are not aware of (there might be naming conventions for local tooling I'm not aware of). – howlger Oct 15 '21 at 18:57
  • 1
    Those are [separate operations](https://stackoverflow.com/questions/30590083/how-do-i-rename-both-a-git-local-and-remote-branch-name) on git so I don't think EGit would do anything different from that. – LMC Oct 15 '21 at 19:51
  • 1
    I actually recently did almost exactly what you described, and I didn't see this behavior. Is there something special about doing this to the master branch? I had a local branch with a particular name, renamed it, and then pushed that renamed branch, and that resulted in the new branch being created on the remote. This was BitBucket Server, not Github. I have no idea whether that would make a difference. – David M. Karr Oct 15 '21 at 20:26
  • 2
    @LMC EGit uses JGit, not git-core (the command line Git). In EGit switching to the same named branch in multiple repositories is a single operation. EGit also provides the _Git Interactive Rebase_ view that is also a time saver compared to the Git command line. – howlger Oct 15 '21 at 21:44
  • 1
    @howlger thanks for the info. I would expect EGit to be tied to the "specification" so to say and assumed that could be represented by git-core behaviour. – LMC Oct 15 '21 at 21:54
  • 1
    @LMC Sure, but it has not the limitation of a command line. Independent from that and which Git client is used, I doubt _"GitHub expects the local and remote branches to have the same name"_. – howlger Oct 15 '21 at 22:11
  • Hi @howlger, you are right that during a push operation the target branch name is defined in the Upstream Branch setting and has nothing to do with the local branch name per se. I will have to re-word my question. – Jonathan Benn Oct 18 '21 at 14:23
  • Hi @DavidM.Karr - interesting! Perhaps the solution is for me to upgrade to a newer version of EGit (my current version is several years old)? – Jonathan Benn Oct 18 '21 at 14:35

0 Answers0