Search code examples
ruby-on-railsgitrepositorygithubfork

How to switch repositories from one to the next


I was using a github repository from a previous developer.

I am the only coder on this project, so I forked the project over to my own github repository.

Now I would like to commit soley to my repo.

Unfortunately, I realized that I never changed my .git/config , so I was still committing to the old repo. I just changed it to the appropriate url, and when I type :

$> git status

It returns :

=> Working directory clean.

But I know its not because I have several commits I've made. So my local box has different code then what it is pointed to on my repository.

My question is this. Obviously I'm halfway through the process of doing this. Do I need to re-fork to update, and then I'm good. Or is there a special command I need to run to let my local box know its 'git status' command is targeting a new repo to compare itself to? Equally, am I missing something else very important :D ?

Thank you everyone.


Solution

  • You can use git remote to manage your remote

    • rename origin
        git remote rename origin old_origin
    
    • add a new origin
        git remote add origin git://github.com/my/forked/repo.git
        git fetch origin # will create all the remote branches references 
                         # in your local repo
    

    You can also easily setup a new upstream for your current master branch (git 1.7 and more):

    git branch --set-upstream master origin/master
    

    The "nothing to commit (working directory clean)" message of git status won't prevent you to push.
    After changing the origin, you should see:

    $ git status
    # On branch master
    # Your branch is ahead of 'origin/master' by xxx commits.
    #
    nothing to commit (working directory clean)
    

    That means you have some commits to push to your new origin.


    Note: "git remote"(man) rename failed to rename a remote without fetch refspec, which has been corrected with Git 2.39 (Q4 2022).

    See commit 5a97b38 (22 Sep 2022) by Jeff King (peff).
    (Merged by Junio C Hamano -- gitster -- in commit 20a5dd6, 10 Oct 2022)

    remote: handle rename of remote without fetch refspec

    Reported-by: John A. Leuenhagen
    Signed-off-by: Jeff King

    We return an error when trying to rename a remote that has no fetch refspec:

    $ git config --unset-all remote.origin.fetch
    $ git remote rename origin foo
    fatal: could not unset 'remote.foo.fetch'
    

    To make things even more confusing, we actually do complete the config modification, via git_config_rename_section().
    After that we try to rewrite the fetch refspec (to say refs/remotes/foo instead of origin).
    But our call to git_config_set_multivar() to remove the existing entries fails, since there aren't any, and it calls die().

    We could fix this by using the "gently" form of the config call, and checking the error code.
    But there is an even simpler fix: if we know that there are no refspecs to rewrite, then we can skip that part entirely.