Thread (10 messages) flat view 10 messages, 3 authors, 2021-04-29

how to rename remote branches, the long way

From: Antoine Beaupré <hidden>
Date: 2021-04-24 00:30:50

So this is a rather long story, which I plan to expand on in a blog post
when I find the time, but to make that story short:

I wrote a Python script to rename branches in git.

Before people start throwing things (like `git push origin oldref:newref
:oldref`) at me, consider that I've been beating my head against this
for a while, and everywhere I look basically suggests this:

    git branch -m to_branch
    git push origin from_branch:to_branch :from_branch

Now, to be fair, the latter is an optimization I found only after
searching more deeply for the problem (of course, on SO):

https://stackoverflow.com/a/21302474

But while the above look fine to a Stackoverflow user, a more experience
git user might know it has multiple problems:

 1. it will fail if you try to rename the master branch, or any
    branch protected by hooks on (say) GitHub or GitLab (because you
    can't delete a default or protected branch)
 2. it will not update the remote default branch (AKA the remote HEAD)
 3. it will not update the local HEAD pointer for the remote branch
    (noticeable on a `git remote prune origin`)
 4. it will break all links to from_branch on the remote (e.g. on gitweb
    and so on)

I made a script which fixes all those problems except the last one (and
only for GitLab, for problem 1). I am kind of hoping I didn't waste my
time doing so, but I would still be happy to be proven wrong and be
shown an easier way.

This is the script:

https://gitlab.com/anarcat/scripts/-/blob/main/git-branch-rename-remote

Raw/javascript-less version:

https://gitlab.com/anarcat/scripts/-/raw/main/git-branch-rename-remote

Also attached at the end of this email.

Now, I send this in a gesture of good will and spirit of sharing. I
would of course be happy to take contributions, in the form of merge
requests on GitLab or patches by email, as you see fit.

I wrote this primarily with the "master to main" migration, because a
bunch of projects (including mine) are suddenly, actually migrating
their main branch from master to main. Personnally, it's because I'm
tired of being yelled "master" from my shell prompt all the time, but
naturally, I guess opinions on the matter vary. The script, of course,
works with any combination of branch names and remotes -- heck, you can
even rename back from master to main if that's your kink -- but the
defaults are to cover for the "main" migration.

But this also makes me wonder if we there ever was a wider discussion on
(remote) branch renames as a primary operation. It seems like git
doesn't really support branch renames right now. `git branch --move`
*looks* like a rename, but really, it's probably just laying down a new
ref and deleting the old one (I haven't actually looked). With remotes,
anyways, that's certainly the only story there is.

For local branches, that doesn't matter much: no "links" should point
there. But git repos are, nowadays, living web sites on web servers like
GitHub, GitLab, cgit, or whatever. You have no way of telling those
sites "I am renaming a branch", so they don't have an opportunity of
fixing broken links (and, incidentally, bypassing branch protection,
although I actually use the GitLab API to workaround that problem).

So I wonder: could git remote branch renames as an operation on remotes?
How would that look? Is that something that the git project should work
on? Or is this something strictly reserved to the API space of git
forges? In that case, how do I tell my gitolite server that it's okay
to rename a branch since it doesn't have an API? :)

Or, maybe, should this script be sent as a [PATCH] instead and just be
merged into git itself? Maybe in contrib/? I do see some Python in
there, so I don't feel too much like an heretic... Obviously, it could
be rewritten in C, but it would feel such a pain in the back to me... I
already rewrote it from this shell script, which is still available
here:

https://gitlab.com/anarcat/scripts/-/blob/2bd01ae584994b8160b1dff20f3e290ace23265c/git-rename-to-main

So many questions, I hope I don't make a fool of myself here, and thanks
in advance for your time.

A.

-- 
Imagination is more important than knowledge.
Knowledge is limited.
Imagination encircles the world.
                        - Albert Einstein

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help