How to Fork a Repository, Make Changes, and Submit a Pull Request on GitHub
When contributing to an open-source project or collaborating with others, you often work with forks and pull requests. This blog walks through the entire process of forking a repository, making changes locally, and submitting a pull request back to the original project.
Step 1: Fork the Repository
Forking a repository allows you to create your own copy of someone else’s repository. This is useful when you want to make changes to the project without affecting the original repository.
- Go to GitHub and navigate to the repository you want to contribute to.
- In the top right corner, click the Fork button.
This will create a copy of the repository under your GitHub account. You now have your own version of the project to work on.
Step 2: Clone the Forked Repository Locally
After forking the repository, you need to clone it to your local machine to make changes.
- In your forked repository on GitHub, click the Code button.
- Copy the HTTPS or SSH URL.
- Open your terminal or command prompt and run the following command to clone the repository:
git clone https://github.com/your-username/repository-name.git
4. Navigate into the cloned directory:
cd repository-name
Step 3: Set Up a Remote for the Original Repository
To ensure you can pull the latest updates from the original repository (sometimes called the “upstream” repository), you need to add a new remote:
- Run the following command to add the upstream remote:
git remote add upstream https://github.com/original-owner/repository-name.git
Replace original-owner
with the username of the owner of the original repository, and repository-name
with the name of the repository.
2. Verify that the upstream remote has been added correctly:
git remote -v
You should see something like this:
origin https://github.com/your-username/repository-name.git (fetch)
origin https://github.com/your-username/repository-name.git (push)
upstream https://github.com/original-owner/repository-name.git (fetch)
upstream https://github.com/original-owner/repository-name.git (push)
Step 4: Create a New Branch
Before making any changes, it’s best practice to create a new branch. This ensures that your changes are isolated from the main branch, making it easier to manage and review.
Create a new branch with a descriptive name:
git checkout -b feature-branch-name
Replace feature-branch-name
with a name that reflects the feature or fix you're working on (e.g., bug-fix
, add-feature-x
).
Step 5: Make Changes Locally
Now, make your changes in the code. This could involve editing files, adding new files, or making improvements. You can use your favorite code editor (e.g., VS Code, Atom, etc.) to make these changes.
For example, if you’re working on a file called index.html
, open it in your code editor:
nano index.html
After making changes, save the file.
Step 6: Stage and Commit Your Changes
Once your changes are made, stage and commit them to your local repository:
- Stage the changes:
git add .
2. Commit the changes with a meaningful commit message:
git commit -m "Descriptive message about what you changed"
Step 7: Push the Changes to Your Fork
After committing your changes, push the branch to your forked repository on GitHub:
git push origin feature-branch-name
This will push the feature-branch-name
branch to your forked repository.
Step 8: Create a Pull Request (PR)
Now that your changes are pushed to your forked repository, you can create a pull request to merge your changes into the original repository.
- Go to your forked repository on GitHub.
- You’ll see a message prompting you to create a pull request. Click on Compare & pull request.
- On the pull request page, provide a title and description of the changes you made. Be detailed and explain why your changes are necessary.
- Choose the base repository (the original repository) and the branch you want to merge into (usually
main
ormaster
), and select your feature branch as the one to merge from. - Click Create pull request.
Step 9: Review and Update (If Necessary)
The repository owner or maintainers will review your pull request. They may ask for changes or improvements. If they do, simply make the changes on your local branch, commit, and push them again to your fork.
The pull request will automatically update with your new changes, making it easy for reviewers to see what you’ve updated.
Step 10: Merge the Pull Request
Once the maintainer reviews and approves your pull request, they will merge it into the original repository. If they give you permissions, you may be able to merge it yourself.
After the merge, your changes will be part of the original project!
Step 11: Keep Your Fork Updated
To keep your fork in sync with the original repository, regularly pull the latest changes from the upstream repository:
git fetch upstream
git checkout main
git merge upstream/main
Then, push the updates to your fork:
git push origin main
Conclusion
Congratulations! You’ve successfully forked a repository, made changes locally, and submitted a pull request to the original repository. This process is essential for contributing to open-source projects and collaborating with others on GitHub. Mastering these steps will help you effectively contribute to various projects while keeping your workflow organized.