LinuxCommandLibrary

hub-fork

Fork a GitHub repository on the command line

TLDR

Fork a GitHub repository by its slug

$ hub fork [tldr-pages/tldr]
copy

Fork a GitHub repository by its URL
$ hub fork [https://github.com/tldr-pages/tldr]
copy

Fork current GitHub repository, set remote name to origin
$ hub fork --remote-name [origin]
copy

SYNOPSIS

hub fork [--no-remote] [USER/REPOSITORY]
hub fork --remote-name <NAME>

PARAMETERS

--no-remote
    Prevents hub-fork from automatically adding the newly created fork as a remote in your local Git repository. The fork will still be created on GitHub.

--remote-name <NAME>
    Specifies a custom name for the remote that will be added for the forked repository. This overrides the default naming conventions (username or origin).

[USER/REPOSITORY]
    The specific GitHub repository to fork, specified in the format user/repository (e.g., octocat/Spoon-Knife). If omitted, hub-fork defaults to forking the repository associated with the current Git working directory's origin remote.

DESCRIPTION

The hub-fork command creates a GitHub fork of the current repository, or a specified remote repository, directly from the command line. This eliminates the need to manually navigate to the GitHub website to perform the forking action. After successfully creating the fork on GitHub, hub-fork automatically adds this new fork as a Git remote in your local repository.

By default, if your existing remote is named origin, the newly added fork remote will be named after your GitHub username (e.g., YOUR_USERNAME). If your current remote is named upstream or parent, the fork will be named origin. This intelligent naming scheme helps you manage your personal fork alongside the original upstream repository. It's particularly useful for contributing to open-source projects by streamlining the workflow of creating a personal copy of a project.

CAVEATS

This command requires hub to be installed and properly configured, and assumes you have valid GitHub authentication credentials for creating repositories. If a fork of the specified repository already exists under your GitHub account, hub-fork might simply add the existing fork as a remote rather than creating a new one.

DEFAULT REMOTE NAMING LOGIC

hub-fork intelligently assigns a name to the new remote it adds. If your current main remote is named origin (pointing to the original project), your fork will be added with a remote name matching your GitHub username (e.g., YOUR_USERNAME). If your main remote is named upstream or parent, your fork will typically be added as origin. This logic aims to provide a sensible default setup for contributing to projects.

INTEGRATION WITH HUB-CLONE

While hub-fork creates a fork, hub-clone is also aware of forks. When you hub clone a repository that is itself a fork, hub-clone often automatically adds the original upstream repository as a remote named upstream. hub-fork complements this by allowing you to create your own personal fork at any time, even after cloning a project, and have it automatically configured locally.

HISTORY

hub was developed by Mislav Marohnić as a command-line wrapper for Git, designed to extend its capabilities specifically for interacting with GitHub. The hub-fork command is a fundamental part of hub's mission to streamline common GitHub workflows, providing a convenient way to perform an action (forking a repository) that traditionally required navigating the GitHub web interface, followed by manual Git remote setup.

SEE ALSO

hub(1), git(1), hub-clone(1), git-remote(1)

Copied to clipboard