简体   繁体   中英

Retroactively forking a project on github

I was once very stupid and didn't know how forks and clones work. So, in order to work on someone else's github repo, I downloaded the repo's files (using no SC), created my own new repository, and then commited those files to it.

Now I know I should work on a fork from the original project, but if I'll open a new fork, copy-paste all the filse from my private repo to the new fork and commit, it will show it as a single giant commit, and I'll lose all of the comment history of the old repo, which will be terrible.

Is the a way for me to somehow open a fork, and "redo" all of the commits I've made to my local new repo - So they will show like they were done on the fork? Sort of like a rebase, but between project instead of branches.

Probably the best way to go about this would be to fork the repository normally by clicking the Fork button, then set the origin remote URL on your existing local clone to the the URL of the fork.

I would say to make sure to fork the repo at the commit you originally cloned it, but GitHub only allows you to fork the current HEAD. So in that case I recommend you rebase from the new forked origin.

Something like this:

you@your-machine ~/path/to/existing_repo $

git remote add origin git@github.com:you/your-forked-repo
git pull origin master --rebase

TL;DR git rebase is likely the tool for the job

Here are the steps that I suggest:

  1. Create a fork on GitHub. Copy it's URL, which I will refer to as FORK_URL .

  2. Create a remote for the fork:

     $ git remote add origin FORK_URL 
  3. Create a new branch and delete your current master:

     $ git checkout master $ git branch my_changes $ git checkout my_changes $ git branch -D master 
  4. Fetch the commits and branches from the origin and checkout its master:

     $ git fetch origin $ git checkout -b master origin/master 
  5. Rebase your changes onto the original master :

     $ git checkout my_changes $ git rebase master 

OR

You can do what Xavier said, which essentially does everything I just wrote in a single command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM