How to turn a cloned repository into a fork?

You cannot directly convert a cloned repository into a fork on GitHub, GitLab or any other git platform, as a fork implies a different type of connection between the original repository and your copy of it. In essence, a fork is a request to the original repository to allow tracking and possible merges of your changes back to the original.

However, here are steps you can follow to manually create a fork from a cloned repository:

  1. Go to the original repository that you have cloned.
  2. Click on the “Fork” button to create a fork of the repository under your GitHub username. Now you have a fork of the original repository.

Let’s say that you have commits in your local clone that you would like to push to the new fork:

  1. On your local system, rename or remove the clone (you might want to back up your commits to another branch or to patch files, if applicable). Then clone the fork to the same location. Now you have a local clone of your new fork.

    # rename the original repository
    git mv original_repo original_repo_backup
    
    # clone the forked repository
    git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git
    
  2. If you have changes in the original local clone that you want to have in the new fork, you can push these changes from the old clone to the new fork. Go to the old clone and execute git remote -v to see a list of remote repositories. The output will look something like this:

    origin	https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
    origin	https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
    

    To push the changes, you need to change the origin URL to the fork’s URL. You can do this with the git remote set-url command. Then push the changes:

    # change to the backup repository directory
    cd original_repo_backup
    
    # set new origin URL for the forked repository
    git remote set-url origin https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git
    
    # push changes to the new fork
    git push origin master
    

This way, the newly created fork would have the same changes as in the cloned repository. Just remember that this method is only needed if you have already made changes to the cloned repository that you want to keep in the fork. If you haven’t made any changes yet, you can just delete the clone and fork the repository.

猜你喜欢

转载自blog.csdn.net/m0_57236802/article/details/132040241
今日推荐