Hub: Rename repository

Created on 30 Aug 2013  路  6Comments  路  Source: github/hub

When I'm working on a new project, I find myself renaming it after I realize what it's really about. This is a multi-step manual process:

  1. Navigate to the project settings page
  2. Type in the new name
  3. Copy the new Git URL
  4. git remote set-url origin ...

Would be great to have this as a one-liner (with a confirmation) using hub. My proposal:

$ hub rename new-repo
Are you sure you want to rename this repository [Ny]? y
Your repository can now be found at https://github.com/USER/new-repo.
feature

Most helpful comment

or with an alias

[alias]
  set-url = remote set-url origin
  rename-repo = "!f() { hub api --flat -XPATCH repos/{owner}/{repo} -f name=$1 | grep .ssh_url | awk '/.ssh_url/ {print $2}' | xargs git set-url; }; f"

so that you run git rename-repo <something>

P.S. thank you to @mislav and GitHub for hub api. It's amazing!

All 6 comments

Yeah, a 2-step process including web + command-line sucks. It would be great to encapsulate this, but I'm wary of adding new commands with generic-sounding names. I don't want to clash with somebody's custom commands or aliases.

I'm also not sure about the usefulness of the command. Sure, it's nice to have that one time when you rename a repo, but maybe my time implementing it and maintaining it will be greater than the total time I save for people in their workflows. It just doesn't happen that often. I need to think about this idea, so I'll leave this open.

My strategy for the set of plugins I've started collecting has been to make longer, more descriptive names for each command, then encourage users to create their own more convenient aliases. In this case, could be something like rename-repository.

After some consideration, I have decided that鈥攆or now鈥攖his is best implemented in user scripts, rather than maintained in hub core. Now that there is a new hub api command, I will close this feature request and leave implementing this as an exercise to the reader. https://github.com/github/hub/releases/tag/v2.8.3 https://github.com/github/hub/pull/2016

For example:

hub api -XPATCH repos/{owner}/{repo} -f name=my-new-name
# (parse out the new URL from server response)
git remote set-url origin <NEW-URL>

Thank you for suggesting!

or with an alias

[alias]
  set-url = remote set-url origin
  rename-repo = "!f() { hub api --flat -XPATCH repos/{owner}/{repo} -f name=$1 | grep .ssh_url | awk '/.ssh_url/ {print $2}' | xargs git set-url; }; f"

so that you run git rename-repo <something>

P.S. thank you to @mislav and GitHub for hub api. It's amazing!

A slight modification to @gko's alias to rename the current directory to match the repo name:
(note that if your prompt has the current directory in it, the change won't be reflected there unless you cd back into the new dir. Doing so in the alias does not work)

[alias]
   set-url = remote set-url origin
   rename-repo = "!f() { \
                          hub api --flat -XPATCH repos/{owner}/{repo} -f name=$1 | grep .ssh_url | awk '/.ssh_url/ {print $2}' | xargs git set-url ; \                       
                          mv $(pwd) $(pwd)/../$1 ; \
                       }; f "

A slight modification to @gko's alias to rename the current directory to match the repo name:
(note that if your prompt has the current directory in it, the change won't be reflected there unless you cd back into the new dir. Doing so in the alias does not work)

[alias]
   set-url = remote set-url origin
   rename-repo = "!f() { \
                          hub api --flat -XPATCH repos/{owner}/{repo} -f name=$1 | grep .ssh_url | awk '/.ssh_url/ {print $2}' | xargs git set-url ; \                       
                          mv $(pwd) $(pwd)/../$1 ; \
                       }; f "

Putting this in my .gitconfig gave me a bad config line. I removed the backslashes and put it all on one line, and it worked. Leaving this comment here for posterity's sake

Was this page helpful?
0 / 5 - 0 ratings