Bird
Raised Fist0
Gitdevops~5 mins

Fork and pull request workflow in Git - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of forking a repository in the fork and pull request workflow?
easy
A. To clone the repository locally without any changes
B. To delete the original project
C. To create a personal copy of the project to work on independently
D. To merge changes directly into the original repository

Solution

  1. Step 1: Understand the concept of forking

    Forking creates a personal copy of the original project on your GitHub account, allowing you to work independently without affecting the original.
  2. Step 2: Identify the purpose in the workflow

    This personal copy lets you safely make changes and experiment before proposing them back to the original project.
  3. Final Answer:

    To create a personal copy of the project to work on independently -> Option C
  4. Quick Check:

    Fork = personal copy for safe work [OK]
Hint: Fork means copy project to your account for safe changes [OK]
Common Mistakes:
  • Confusing fork with clone
  • Thinking fork deletes original
  • Assuming fork merges changes automatically
2. Which command correctly pushes a new branch named feature1 to your forked repository?
easy
A. git push origin main
B. git push upstream feature1
C. git push fork feature1
D. git push origin feature1

Solution

  1. Step 1: Identify the remote name for your fork

    By default, your fork is set as the remote named origin. The original repository is usually upstream.
  2. Step 2: Use the correct push syntax

    To push a branch named feature1 to your fork, use git push origin feature1.
  3. Final Answer:

    git push origin feature1 -> Option D
  4. Quick Check:

    Push branch to origin (your fork) = git push origin branch [OK]
Hint: Push new branch to origin, not upstream [OK]
Common Mistakes:
  • Using upstream instead of origin for push
  • Pushing main branch instead of feature branch
  • Using incorrect remote name like fork
3. After forking a repo and pushing a branch fix-bug to your fork, what is the next step to propose your changes to the original project?
medium
A. Directly push fix-bug branch to the original repository
B. Create a pull request from your fork's fix-bug branch to the original repo
C. Merge fix-bug branch locally without pushing
D. Delete your fork and clone the original repo again

Solution

  1. Step 1: Understand the pull request purpose

    A pull request asks the original project to review and merge your changes from your fork's branch.
  2. Step 2: Identify the correct action after pushing

    After pushing your branch to your fork, you create a pull request targeting the original repository's branch.
  3. Final Answer:

    Create a pull request from your fork's fix-bug branch to the original repo -> Option B
  4. Quick Check:

    Push branch then create pull request [OK]
Hint: Push branch, then open pull request to original repo [OK]
Common Mistakes:
  • Trying to push directly to original repo without permission
  • Merging locally without sharing changes
  • Deleting fork before proposing changes
4. You forked a repo and created a branch update-docs. You pushed it but forgot to sync your fork with the original repo first. What problem might occur when creating a pull request?
medium
A. Merge conflicts due to outdated fork base
B. Your pull request will be automatically merged
C. Your branch will be deleted automatically
D. No changes will be visible in the pull request

Solution

  1. Step 1: Understand syncing forks

    If your fork is behind the original repo, your branch may not include recent changes from the original.
  2. Step 2: Identify the pull request impact

    This can cause merge conflicts when the original repo tries to merge your changes because the base is outdated.
  3. Final Answer:

    Merge conflicts due to outdated fork base -> Option A
  4. Quick Check:

    Outdated fork causes merge conflicts [OK]
Hint: Always sync fork before starting new branch [OK]
Common Mistakes:
  • Assuming pull request merges automatically
  • Thinking branch deletes itself
  • Believing changes won't show without sync
5. You forked a project and created two branches: featureA and featureB. You pushed both branches to your fork. How do you create pull requests so the original repo can review and merge these features independently?
hard
A. Create separate pull requests for each branch targeting the original repo's main branch
B. Create one pull request combining both branches
C. Push both branches to upstream and wait for automatic merge
D. Merge featureB into featureA locally, then create a single pull request

Solution

  1. Step 1: Understand pull request scope

    Each pull request represents changes from one branch to the original repo, allowing independent review.
  2. Step 2: Apply to multiple branches

    To keep features separate, create one pull request per branch targeting the original repo's main branch.
  3. Final Answer:

    Create separate pull requests for each branch targeting the original repo's main branch -> Option A
  4. Quick Check:

    One pull request per branch for independent review [OK]
Hint: Make one pull request per branch for clear reviews [OK]
Common Mistakes:
  • Combining branches in one pull request
  • Pushing branches directly to upstream without PR
  • Merging branches locally before PR