4

I have a private repository on github (say "ProjectX"). Project requirements changed midway which meant that the code had to be rewritten. I did not want to lose the progress made already on "ProjectX", so I did the following:

  1. Renamed "ProjectX" to "ProjectX_advanced" in the local repository.
  2. Renamed the remote origin like so:

git remote rm origin
git remote add origin git@github-user:user/ProjectX_advanced.git
Also renamed the project on GitHub.
This was successful. Executing git remote -v on the terminal showed the following:

origin  git@github-user:user/ProjectX_advanced.git (fetch)
origin  git@github-user:user/ProjectX_advanced.git (push)  

I am also able to push changes to the above repository.
3. Created a new repo called "ProjectX" on github.
4. Created a new project called "ProjectX" in my local repository.
5. Initialized git in it like so:
git init
git add .
git commit -m "First commit" .

6. Added a new remote origin like so:
git remote add origin git@github-user:user/ProjectX.git
If I now do git remote -v on the command line, I get the correct remote location.
7. The problem arises when I now try to push changes in the local repo to the github repo with:
git push origin master
The error message seen::

To git@github-user:user/ProjectX.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github-user:user/ProjectX.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.   

I have seen some previous questions here and here and some others that indicate I need to do a git pull and merge before I can push these changes.
My question(s):
1. If I do a git pull, my new "ProjectX" will be over-written by code I do not need. How do I get around it?
2. I have renamed all remotes (output from git remote -v is as expected). Have I missed renaming some remote?
3. How do I solve this?

Community
  • 1
  • 1
Sriram
  • 10,298
  • 21
  • 83
  • 136

4 Answers4

7

First clone the code from github

git clone https://github.com/emample_user/my_repo_project.git

after that

If you want to change my_repo_project to Repo_project

  1. Go to the remote host (e.g., https‍://github.com/emample_user/my_repo_project).
  2. settings
  3. change the repository name

And to push the code to rename repository

$git remote set-url origin https://github.com/emample_user/Repo_project
$git add .
$git commit -m "message"
$git push -u origin master
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Arshad Syed
  • 403
  • 4
  • 9
1

Your new ProjectX repo on GitHub should be empty, but try to clone it, and start working from that clone (rather than initializing it locally, and pushing to the GitHub new repo).

That way, you would check, during the clone, that the new ProjectX repo is indeed empty.
...Which might not be the case!

The OP Sriram's answer shows there is a README.md created by GitHub during the repo initialization on GitHub side.


Plus, As I mentioned in "How do I rename a repository on GitHub?", since May 2013, when you rename a repo on GitHub:

we'll automatically redirect all requests for previous repository locations to their new home in these circumstances.

That is what might happen here, and your push might end up in the (renamed) ProjectX_advanced.
That should resolved shortly, when GitHub realizes there is a new repo with the old name ProjectX, and removes the redirection to ProjectX_advanced.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for the suggestion. Cloning the repo from GitHub did show that it was an empty repo. This meant that `git pull` requests would not change much in the local copy of ProjectX. I have posted an answer to show what worked for me. Thanks for the help! – Sriram Sep 28 '13 at 19:23
0

I followed @VonC's suggestion and cloned ProjectX into a different local directory like so:
cd destDir (directory where a clone of ProjectX should be created)
git clone git@github-user:user/ProjectX.git

This created the clone and showed that ProjectX was really empty even on GitHub. The contents of ProjectX were a README.md file and the gitignore file. Pulling these changes to the repository would not make much of a difference as Readme.md would get added and gitignore would change. Pulling the changes like so:
git pull origin master in the local ProjectX directory updated the above two files. I was then able to push changes using git push origin master.

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • 1
    Nice feedback, in complement of my own answer. +1 – VonC Sep 28 '13 at 19:27
  • 1
    You should update the url of the origin https://stackoverflow.com/questions/45011789/update-origin-after-renaming-the-repository-name – user666 Feb 06 '19 at 16:38
0

If you already have been working with the repo locally and changed the name on GitHub, just run: $git remote set-url origin https://github.com/example_user/new_repo_name