Search code examples
gitversion-controlgithub

Renaming a repo and pushing new code


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?


Solution

  • 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.