DEV Community

Jacob
Jacob

Posted on

Fork, Pull, and Push: A Step-by-Step Guide to GitHub

Originally published here

GitHub is the go-to platform for version control, collaboration, and open-source contributions. Whether you’re working on your own project or contributing to someone else’s, mastering the basics of forking, pulling, and pushing is essential. These core concepts allow developers to collaborate efficiently, manage project versions, and contribute to the open-source community.

In this article, we’ll walk through the process of forking a repository, making changes locally, and pushing those changes back to GitHub, all while using the pull request (PR) workflow.

What Are Forking, Pulling, and Pushing?

Before diving into the guide, it’s important to understand these three terms:

  • Forking: This is the process of creating a personal copy of someone else’s repository. Forking allows you to make changes to a project without affecting the original codebase.
  • Pulling: This involves retrieving updates from the original repository (or a remote branch) into your local codebase to ensure you're working with the latest version.
  • Pushing: Once you’ve made changes to your fork, pushing refers to sending those changes to your remote forked repository on GitHub.

Together, these steps form the basic workflow for contributing to a GitHub project.

Step 1: Forking a Repository

Why Fork a Repository?

Forking a repository is useful when you want to:

  • Contribute to an open-source project.
  • Experiment with the codebase before submitting your changes.
  • Create your version of a project that you don’t own.

How to Fork a Repository:

  1. Find the repository you want to fork on GitHub.
  2. In the top-right corner, click the “Fork” button. GitHub will create a copy of the original repository in your GitHub account.
  3. You now have a forked version of the project that you can modify without affecting the original repository.

Step 2: Cloning Your Fork Locally

Now that you've forked the repository, you need to clone it to your local machine to make changes.

  1. Navigate to your forked repository on GitHub.
  2. Click the “Code” button and copy the HTTPS URL.
  3. Open your terminal or command prompt and run the following command:
git clone <your-forked-repository-URL>
Enter fullscreen mode Exit fullscreen mode

This command will clone the repository to your local machine.

  1. Change to the repository directory:
cd <repository-name>
Enter fullscreen mode Exit fullscreen mode

Step 3: Making Changes Locally

Now that you have the project on your machine, you can start making changes.

  1. Create a new branch: It’s best practice to make changes on a separate branch, especially when contributing to a shared project. To create and switch to a new branch, run:
git checkout -b <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace <new-branch-name> with a descriptive name for your changes.

  1. Make your changes: Edit files, add new features, or fix bugs in your local environment.
  2. Check the status of your changes by running:
git status
Enter fullscreen mode Exit fullscreen mode

This command shows which files have been modified. 4. Stage your changes for commit by running:

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

Or, to add all changed files:

git add .
Enter fullscreen mode Exit fullscreen mode

Step 4: Committing Your Changes

After making changes and staging them, the next step is to commit those changes locally.

  1. Run the following command to commit the changes:
git commit -m "Your detailed commit message"
Enter fullscreen mode Exit fullscreen mode

Replace the placeholder text with a meaningful description of the changes you made.

Step 5: Pushing Your Changes to GitHub

Now that you’ve committed the changes locally, you need to push them to your forked repository on GitHub.

  1. To push your branch to GitHub, run:
git push origin <new-branch-name>
Enter fullscreen mode Exit fullscreen mode

This command pushes your changes to your forked repository under the new branch you created. 2. After the push is complete, GitHub will display a link in your repository where you can open a Pull Request (PR).

Step 6: Creating a Pull Request (PR)

A pull request is the process of notifying the original repository’s maintainers that you’ve made changes and want them to review and potentially merge those changes into the main project.

  1. Go to your forked repository on GitHub.
  2. You’ll see a message prompting you to create a pull request for the branch you just pushed. Click the “Compare & pull request” button.
  3. In the pull request form:
    • Write a title and description of your changes.
    • Include any necessary context to help the maintainers understand what your PR addresses.
  4. Submit the pull request by clicking “Create pull request.”

Step 7: Updating Your Fork with New Changes

Over time, the original repository may receive new commits. It’s important to keep your fork up to date with these changes.

Syncing Your Fork:

  1. Add the original repository as a remote in your local repository:
git remote add upstream <original-repository-URL>
Enter fullscreen mode Exit fullscreen mode
  1. Fetch the latest changes from the original repository:
git fetch upstream
Enter fullscreen mode Exit fullscreen mode
  1. Merge the changes into your local fork:
git merge upstream/main
Enter fullscreen mode Exit fullscreen mode

Replace main with the correct branch name if the project uses a different default branch (e.g., master).

  1. Push the updated changes to your forked repository on GitHub:
git push origin main
Enter fullscreen mode Exit fullscreen mode

Conclusion

Forking, pulling, and pushing are fundamental GitHub concepts that allow developers to contribute to projects in an organised and efficient way. By following this guide, you’ll have the necessary skills to fork repositories, make changes locally, and submit pull requests to contribute to open-source projects or collaborate on private repositories.

The more you practice this workflow, the more seamless it becomes, making you a more effective and confident contributor to any GitHub project.

Top comments (5)

Collapse
 
programmerraja profile image
Boopathi

This is a great breakdown of the essential GitHub workflow. I especially appreciate the step-by-step instructions and clear explanations. I'm now ready to dive into my first open-source contribution!

Collapse
 
0101001 profile image
Unknown Dev • Edited

In all my years of using github and reading articles, never have I come across such detailed breakdown of fork, push and pull.
Well-done!
Nice work 🌟
Also
Will you be doing more github tips like this??

Collapse
 
chiragagg5k profile image
Chirag Aggarwal

great beginner guide!

Collapse
 
navneet_verma profile image
Navneet Verma

Great it was so confusing before I real your article!

Collapse
 
sivaram_yarasuri_8352e89a profile image
Sivaram Yarasuri

Nice breakdown. Appreciate the efforts !!