Forking a project to your own namespace is useful if you have no write access to the project you want to contribute to. If you do have write access or can request it, we recommend working together in the same repository since it is simpler.
The Git forking workflow is a popular approach used for managing code contributions in distributed version control systems like GitLab. It is particularly effective in environments where multiple contributors work on a project independently, often in open-source settings.
Benefits of the Forking Workflow
This workflow is especially common in open-source projects but is also used in private or corporate settings where developers need a structured process for proposing changes.
“Fork” is not a Git operation — it just means you have made a copy of an existing repository and are doing new development on your copy.
You are the contributor. The guy who created the original repo is the maintainer.
The workflow begins by forking the main (upstream) repository to your own Git account. This creates a personal copy of the repository, which allows you to freely experiment without affecting the main project.
When working with GitLab, forking a project is a two-step process:
1. Click on the fork button between the star and clone buttons on the project’s home page.
2. Once you do that, you’ll be presented with a screen where you can choose the namespace to fork to. Only namespaces (groups and your own namespace) where you have write access to, will be shown. Click on the namespace to create your fork there.
If you're trying to fork a repository on code.hampel-soft.com and receive an error message saying you cannot create projects in your own namespace, please see our GitLab FAQ for more details.
After forking, you clone your repository to your local machine. This gives you a working directory where you can make changes to the codebase.
# clone own private repo (this is remote 'origin') git clone https://user@gitlab.com/user/repo.git
You can also connect your local clone of the repo with the original repository of the maintainer by adding a second remote:
# add the public repo as a remote called 'upstream' git remote add upstream https://user@gitlab.com/maintainer/repo.git
Before making any changes, it's a good practice to create a new branch that isolates your feature or bug fix. This branch is separate from the main branch of both your fork and the upstream repository.
git checkout -b feature-branch
If you added the maintainer's repo as a second remote, you can check for changes and bring them into your local clone.
# keep my private repo up to date with changes from public repo git pull upstream master
You can now make the necessary changes to the code, add them to the staging area, and commit them to your feature branch.
# Stage changes git add . # Edit some code git commit -a -m "this is my change"
Once you’ve committed your changes, you push the branch to your remote fork on GitHub (or another Git hosting service).
# publish my changes to my private repo git push origin feature-branch
With the changes pushed to your fork, the next step is to create a merge request (MR) to the original repository. The MR allows the maintainers of the upstream repository to review your changes and decide whether to merge them into the main codebase.
The repository maintainers will review your MR, possibly provide feedback, and request changes. After addressing any feedback, they can merge the changes into the upstream repository.
# update my private repo from the public repo after merging my changes git pull upstream master
You are the maintainer. The guy who created the MR is the contributor.
When reviewing and merging a merge request (MR) in a Git forking workflow, maintainers follow several important steps to ensure that the proposed changes are well-integrated into the main repository.
When a contributor submits a merge request (MR), their code changes are automatically made accessible to the maintainer within the platform's interface (GitHub, GitLab, Bitbucket, etc.). The maintainer can see:
From here, the maintainer can review all files and see a summary of added, modified, or deleted code.
If the maintainer prefers to examine the code changes in a local environment, they can pull the contributor’s branch from the contributor’s forked repository and test it locally. Here’s how:
i. Add the Contributor’s Fork as a Remote: First, the maintainer needs to add the contributor's fork as a remote in their local repository. They use the git remote add command to point to the fork’s URL.
git remote add contributor https://gitlab.com/user/repo.git
ii. Fetch the Branch: Once the contributor’s repository is added, the maintainer can fetch the branch associated with the merge request:
git fetch contributor feature-branch
iii. Checkout the Branch Locally: The maintainer can then check out the branch locally to explore and test the changes:
git checkout feature-branch
This allows the maintainer to run tests, inspect the code, or even make changes locally.
To test how the contributor’s changes integrate with the latest version of the upstream (main) repository, the maintainer can perform a local merge without pushing it to the remote. This helps to identify merge conflicts or issues before the official merge.
i. Fetch the Latest Upstream Changes:
git fetch origin
ii. Merge Contributor’s Branch:
git merge contributor/feature-branch
If there are merge conflicts or issues, the maintainer can resolve them locally, test the code, and provide feedback to the contributor.
On platforms like GitHub, GitLab, or Bitbucket, the maintainer can also directly access and browse the code through the contributor’s fork via the web interface. They can:
This method allows maintainers to quickly assess the contributor’s overall code structure, branch management, and history.
By following these steps, maintainers ensure that the contribution process is smooth and that the codebase remains stable and maintainable over time.