JoelFan
JoelFan

Reputation: 38714

forgot to do a fork on GitHub before starting my work

Suppose I clone Company C's repo of Project X from github to my local machine. I create a branch for myself locally, do some work on it and make commits to my branch locally.

Now I want to make a pull request to Company C, but I realize that I should have done a fork on gitub to create my own repo of Project X and cloned that instead of Company C's repo.

How do I recover from this without cloning my fork locally and starting over by copying files manually? I'd like to use git's magic to save myself.

Upvotes: 12

Views: 2628

Answers (3)

Philippe
Philippe

Reputation: 31197

If you want to follow the development of the project you've cloned, by convention, we add another remote (named upstream) in addition to your origin remote.

So you have to:

  1. Fork the project on Github.com website.

  2. Rename the current origin remote (that tracks at the moment the upstream project) with a new name like upstream.

     git remote rename origin upstream
    
  3. Add your personal forked GitHub project as the origin remote.

     git remote add origin <URL of your personal github fork>
    

The idea behind that is to follow the naming conventions of:

  • origin for your personal repository
  • upstream for the project repository (to be able to sync your personal repository with developments made in the upstream repository)

Upvotes: 14

Au613
Au613

Reputation: 11

The overall strategy is as follows:

  1. See which remote repos are connected to your local repo
  2. Disconnect the remote repos from your local repos.
  3. Connect the desired remote repository to your local.

git remote -v #shows you the names of the remote repos
git remote remove {Name of the remote repo(s)}
git remote add {url of the desired remote repo}    

Upvotes: 1

SLaks
SLaks

Reputation: 887797

You simply need to add your fork as a remote in your existing clone:

git remote add <any name> <URL of fork>

You may also want to git remote rm the original repo.

Upvotes: 1

Related Questions