0

My colleague and I have been trying to put together a clear, consistent procedure for renaming a git branch locally and remotely.

Assuming we have a branch oldname both locally and on remote, and want to rename it to newname, and we have checked out locally branch oldname, following https://stackoverflow.com/a/30590238/1592322 (and other similar suggestions across the internet), these steps work for my colleague:

git branch -m newname
git push --set-upstream origin newname 

Whereas I have to follow an intermediate step of explicitly unsetting the upstream information (as suggested by user:22992 in that same stack overflow link I provided above):

git branch -m newname
git branch --unset-upstream
git push --set-upstream origin newname 

If my colleague uses my (slightly longer) procedure, then that second additional step git branch --unset-upstream results in an error:

Fatal: The current branch newname has no upstream branch.

So we can't use the same consistent procedure, and guess we just have to watch out for this in general. It seems to me that the behavior of git branch -m differs between different git versions. Is this correct? Why is there a difference in behavior?

My colleague is running git version version 2.18.0.windows.1 . Whereas I am running 2.17.2 (Apple Git-113) on a macOS 10.13.6.

H3007
  • 46
  • 5
  • Renaming a branch should not affect its upstream. Perhaps his branch did not have an upstream set initially. You should be able to use `git push -u origin newname` without first running `git branch --unset-upstream` anyway, though. (`-u` is the short spelling for `--set-upstream` with `git push`) – torek Jul 09 '20 at 16:52
  • Thanks @torek. My colleague had already pushed her branch `oldname` previously to remote before the rename. So I would have thought the upstream was set correctly. To your second question, I had tried running `git push -u origin newname` to remote, I get the message `Branch 'newname' set up to track remote branch 'oldname' from 'origin'.` . The relationship between the local branch and remote is also clear when you run `git branch -vv`. – H3007 Jul 09 '20 at 19:46
  • When she ran `git push `, that would not have set an upstream for `` unless she used the `-u` or `--set-upstream` option, so if it had no upstream, it would continue to have no upstream. Basically the upstream of some branch is either set (to one value, typically something like `origin/master`) or unset: `git branch --unset-upstream` makes it unset, and `git push -u` makes it set—changing or setting it as needed—based on the push. `git branch --set-upstream-to` also makes it set, to whatever you choose. – torek Jul 09 '20 at 20:24
  • I see your point @torek regarding my colleague's specific `git push` command, and could answer that particular source of confusion, thanks! Regarding the other point, I pushed my initial branch `oldname` to remote using `git push --set-upstream origin oldname`. So there is an upstream. But then using `git branch -m newname; git push --set-upstream origin newname` results in `Branch 'newname' set up to track remote branch 'oldname' from 'origin'.`, which is what confuses me. – H3007 Jul 09 '20 at 21:04
  • Ah - `git push -u` just *always* prints that message, after updating the upstream (perhaps to whatever it already was set to!). The odd thing is, it should be set up to track `newname` from `origin` now, since that is the target of the push. – torek Jul 09 '20 at 21:15
  • Thanks again for the fast response. Unfortunately running `git branch -vv` shows master 7dcd2fe [origin/master] added test file * newname 7dcd2fe [origin/oldname] added test file and looking at remote via the GitHub web interface lists only `oldname` (and `master`) as branches. – H3007 Jul 09 '20 at 21:29
  • 1
    Could it be a case sensitivity issue ? do `oldname` and `newname` only differ in case ? e.g : renaming from `Fix/Something` to `fix/something` ? – LeGEC Jul 09 '20 at 21:47
  • Thanks @LeGEC .Unfortunately not. For this test, I'm _literally_ using `oldname` and `newname` as my old and new branch names respectively. – H3007 Jul 10 '20 at 15:46

0 Answers0