Reputation: 972
I googled and also searched on stackoverflow for the below question, however was unable to find any satisfactory answer.
So say, I have a remote repository: https://github.com/<username>/foo
and I
wish to rename it to https://github.com/<username>/bar
at the remote site as well as in local config entries from command line, and right now I have the repo cloned locally at '~/my_github_repos/foo'.
So basically, I want all the things for this repo to be converted from foo
to bar
( urls / configurations / local folder )
For that, I renamed the folder ~/my_github_repos/foo/
to ~/my_github_repos/bar/
and tried doing,
git remote set-url origin https://github.com/<username>/bar.git
But that just did it locally and not remotely. So when I made some changes in the files inside the now renamed folder, bar
and did a git push origin master
, It gave me error:
remote: Repository not found.
fatal: repository 'https://github.com/<username>/bar.git/' not found
So, what all things do I need to do so that I make the changes locally and get it reflected remotely, for the renaming of repo ?
I know Github.com provides a very nice UI for renaming the repos, however I just want to know if there is a way to do so in few steps from command line. If not, I agree that it's simple to just rename it from github.com website.
The reason for this question is that, I like to do things mostly from commandline rather than go here and there for doing tasks as simple as renaming a repo.
P.S. I searched stackoverflow and found this: Rename Github Repository, which may seem similar to this question, but it's different.
Upvotes: 2
Views: 2617
Reputation: 1323125
I like to do things mostly from commandline rather than go here and there for doing tasks as simple as renaming a repo.
No need to use the API command anymore, with the new gh 2.3.0.
It does rename the remote repository, with the same API call.
But: it does also rename the remote "origin
" URL to match the new renamed repository URL.
See PR 4450 and commit 67d1a90, with pkg/cmd/repo/rename/rename.go#updateRemote() git.UpdateRemoteURL()
But you can go even further with gh extension, as illustrated here by Nate Smith.
gh
2.6.0 (March 2022) add interactive repository edit functionality by @g14a, which include repository renaming.
Upvotes: 2
Reputation: 13923
The local repository and the remote repository are separate in this regard. There is no Git-command that will allow you to change the path/name of a remote repository.
You mention in your question that you already changed the name of the local directory and updated the repository URL with git remote set-url
. Because you cannot rename the remote repository with Git, you need to do this in a way that is supported by the type/software/platform of the actual remote repository you have.
This is tagged with GitHub so your question boils down to: Is there a way to change the name of a GitHub repository without using the web-interface? Yes, this is possible with the GitHub API by sending a PATCH
request to /repos/{owner}/{repo}
and specifying a different name
parameter (docs).
I suggest to use the api
-command of GitHub CLI which allows to make authenticated request to the GitHub API in a secure way:
gh api -X PATCH --raw-field name=new-name repos/account-name/current-name
There is a feature request for GitHub CLI to allow renaming of a repository with a dedicated command rather than using the api
-command.
Another option is to use curl
directly. However, passing credentials in the command line has security implications and I don't recommend doing this. For the sake of the example, here is how the request would look like:
curl -X PATCH \
-d '{"name": "new-repo-name"}' \
-H "Authorization: token ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" \
"https://api.github.com/repos/account-name/current-repo-name"
Upvotes: 4