4

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.

omer mazig
  • 865
  • 2
  • 10
  • 15

2 Answers2

2

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
Xavier
  • 3,255
  • 23
  • 34
  • When I run the second line it says: git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Which makes sense cause it's not the same repo. How can I fix this? – omer mazig Jan 05 '18 at 23:34
  • Also "remote add" doesn't work cause origin exists already so I did set-url instead. Hope that's OK – omer mazig Jan 05 '18 at 23:37
  • Hey Omer, yeah set-url works just fine. For the permission denied error you could use the HTTPS URL intead of the SSH, or you can set up SSH access with your account. The first is quicker to get started, but you have to enter your GitHub login every now and then. Check out this article: https://help.github.com/articles/which-remote-url-should-i-use/ For the former: `git remote set-url origin https://github.com/you/your-forked-repo.git` – Xavier Jan 05 '18 at 23:46
  • Mostly worked. My case was even more complicated than described so I had to do a lot of other shit but eventually it worked. Thanks! – omer mazig Jan 06 '18 at 02:17
0

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.

Code-Apprentice
  • 76,639
  • 19
  • 130
  • 241