nonopolarity
nonopolarity

Reputation: 151006

How to make a GitHub repo the same as if it was originally forked from another repo?

Say, I saw some sample project on GitHub that I want to try out. If I actually forked from that repo, and then git clone that new repo to my hard drive and make changes and git push, everything is good.

Let's called the above Scenario 1.

But what if I didn't fork, but just git clone the original repo. Now after 5 hours of project changes of file edit, git add, and git commit, how do I make it back to Scenario 1 (as if I forked and git clone from that newly forked repo). And on GitHub, it should also show that the repo is forked from that original repo as well. All the 5 hours of changes and commit history should be preserved as well, if possible.

Upvotes: 3

Views: 76

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83527

First fork the GitHub repo as normal. Now on your local machine open Git Bash and cd to the directory with your cloned version of the repo. You can change the URL for the origin remote with

$ git remote set-url origin <url for the forked repo>

To maintain a connection with the original repo you can add it as a remote with a different name:

$ git remote add upstream <url for original repo>

Upvotes: 3

rthbound
rthbound

Reputation: 1343

Just fork it after the fact, then add a remote using the remote url for your new fork:

git remote add myremote [email protected]:some/something.git

After that, you can push your commits to your fork:

git push myremote my-branch

If you would instead like to make your new remote into origin, you should first rename the original origin to something different:

git remote rename origin something_different
git remote add origin [email protected]:some/something.git

Upvotes: 0

Related Questions