Git lends itself to the command line.
While not every developer loves the command prompt or the Bourne Again Shell (BASH), anyone doing version control becomes quickly adept at issuing a git reset or a git commit in the terminal window. However, vendors like GitHub and GitLab do offer supplemental Git features that aren’t available through the command line. GitHub’s fork feature is one of them, along with the ability to perform a pull request.
You simply can’t do a GitHub fork from the command line with only a basic Git installation. To perform such version control hijinks, a user must download and install the GitHub command-line interface (CLI) tools.
How to fork GitHub on the command line
To perform a command-line GitHub fork, follow these steps:
- Download and install the GitHub CLI tools.
- Open a terminal window and log in with the gh CLI command.
- Find the GitHub URL of the repo you wish to clone.
- Use the gh CLI tool to perform a command-line GitHub fork of repository of interest.
The CLI syntax to log into GitHub through the terminal window is as follows:
gh auth login --web
The –web switch will trigger a browser-based authentication. Simply log into your GitHub account and provide the authentication code displayed in the terminal window. This will allow you to then issue subsequent, authenticated, GitHub commands.
|Your intro to GitHub Actions training course|
Here’s how to get started with GitHub Actions:
Follow these tutorials and you’ll learn GitHub Actions fast.
GitHub fork command
Once authenticated, issue the following GitHub fork command. Note that the –clone option is added to force the remote repository to be cloned locally.
gh repo fork https://github.com/learn-devops-fast/spock-lizard-docker.git --clone
Once you issue this command, your cloud-hosted GitHub account will include a fork of the original repository, and a clone will be made on the local file system. From there, you can issue a Git commit, push, fork and even GitHub pull requests just as they would with any ordinary Git or GitHub repository.
And that’s how easy it is to fork a GitHub repo from the command line.