Fork
Форк
Forking a repository creates a separate copy under your own account or organization. You can make changes there without touching the original and optionally ask its maintainers to accept them.
Also known as: форкнуть, Fork репозитория
What a fork is
Suppose you spot a typo in an open-source project's README. You can read the repository, but you can't push to it. If the hosting platform allows it, you can fork the repository and push to your own copy instead. The fork starts with the commit history of the branches that were copied, and a host such as GitHub or GitLab records which repository it came from. In this workflow, the original project is commonly called upstream.
Fork, clone, or branch?
- A fork is a separate hosted repository. It gives you a place to publish changes without write access to the original. Forking is a hosting-platform feature, not a built-in Git command.
- A clone is usually the local copy you make with
git clone. Cloning a repository you can read doesn't create a hosted copy or give you permission to push back to it. - A branch is a pointer to a commit within a repository. Teams with write access can often work in branches in a shared repository. In a fork-based workflow, contributors typically create branches in their forks instead.
Example: send a change upstream
Use the host's Fork button first. Then replace MY_FORK_URL and ORIGINAL_URL below with the addresses of your fork and the original repository:
git clone MY_FORK_URL docs-tool
cd docs-tool
git remote add upstream ORIGINAL_URL
git switch -c fix-readme
# Edit README.md before staging it.
git add README.md
git commit -m "Fix typo in README"
git push -u origin fix-readme
Here, origin is the remote Git set up for the repository you cloned: your fork. upstream is the name you chose for the original repository. On GitHub, open a pull request with your fork's fix-readme as the source and the original project's target branch as the destination. Pushing publishes your branch in the fork; the destination branch changes only if the proposed change is merged.
Where forks are used and what can go wrong
Forks are useful for contributing to projects where you don't have write access, experimenting away from a shared repository, and maintaining a customized version. In that last case, people may also call the independently maintained project a fork, even if it no longer uses the host's fork feature. Check the original license before distributing your version.
A fork doesn't necessarily stay current when the original changes. git fetch upstream updates your local view of the original's branches, but it doesn't move your local main or update the fork on the server. If your local main can be fast-forwarded, use git switch main and git merge --ff-only upstream/main, then git push origin main to update the hosted fork. If the histories have diverged, the fast-forward-only merge will stop; decide how to integrate the changes before pushing.
How it comes up in interviews
A common question is: “How would you contribute a fix without write access to the project?” Walk through the fork, the local clone, the topic branch, and the cross-repository pull request. Be ready to explain where you'd push your commits and where you'd fetch the project's latest changes. The key is knowing which repository each remote points to, not memorizing the remote names.