0
0
Gitdevops~5 mins

Fork and pull request workflow in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to contribute to a project you don't own, you use a fork and pull request workflow. This lets you make changes safely and ask the original project to include your work.
When you want to fix a bug in someone else's project without direct write access.
When you want to add a new feature to an open source project.
When you want to experiment with changes without affecting the original code.
When you want to collaborate on a project but keep your changes separate until reviewed.
When you want to submit your improvements for review before they become part of the main project.
Commands
Clone the original project repository to your local machine to start working on it.
Terminal
git clone https://github.com/example-user/example-project.git
Expected OutputExpected
Cloning into 'example-project'... remote: Enumerating objects: 10, done. remote: Counting objects: 100% (10/10), done. remote: Compressing objects: 100% (8/8), done. remote: Total 100 (delta 2), reused 7 (delta 1), pack-reused 90 Receiving objects: 100% (100/100), 12.34 MiB | 2.00 MiB/s, done. Resolving deltas: 100% (30/30), done.
Add your forked repository as a new remote named 'fork' to push your changes later.
Terminal
git remote add fork https://github.com/your-username/example-project.git
Expected OutputExpected
No output (command runs silently)
Create and switch to a new branch named 'fix-typo' to isolate your changes.
Terminal
git checkout -b fix-typo
Expected OutputExpected
Switched to a new branch 'fix-typo'
Stage the file you changed to prepare it for committing.
Terminal
git add README.md
Expected OutputExpected
No output (command runs silently)
Commit your staged changes with a clear message describing what you fixed.
Terminal
git commit -m "Fix typo in README"
Expected OutputExpected
[fix-typo abc1234] Fix typo in README 1 file changed, 1 insertion(+), 1 deletion(-)
Push your branch with changes to your fork on GitHub so you can create a pull request.
Terminal
git push fork fix-typo
Expected OutputExpected
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To https://github.com/your-username/example-project.git * [new branch] fix-typo -> fix-typo
Fetch the latest changes from the original repository to keep your local copy up to date.
Terminal
git fetch origin
Expected OutputExpected
remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 1 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), 1.23 KiB | 1.23 MiB/s, done.
Merge the latest changes from the original main branch into your current branch to avoid conflicts.
Terminal
git merge origin/main
Expected OutputExpected
Updating abc1234..def5678 Fast-forward README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Key Concept

If you remember nothing else from this pattern, remember: fork the project, make changes in a new branch, push to your fork, then create a pull request.

Common Mistakes
Committing changes directly to the main branch
This can cause conflicts and makes it harder to manage multiple changes separately.
Always create a new branch for your changes before committing.
Pushing changes to the original repository instead of your fork
You usually don't have permission to push to the original repository, so the push will fail.
Add your fork as a remote and push your changes there.
Not updating your branch with the latest changes from the original repository
Your pull request might have conflicts or be outdated, making it harder to merge.
Regularly fetch and merge changes from the original repository's main branch.
Summary
Clone the original project to your local machine to start working.
Create a new branch for your changes to keep work organized.
Push your branch to your forked repository to prepare for a pull request.
Keep your branch updated with the original repository to avoid conflicts.