export const frontmatter = {
  title: "How To: Fork a GitHub Repository & Submit a Pull Request",
  date: "2019-04-09 02:17:03-0400",
  description:
    "Walkthrough of forking a GitHub repository, cloning it, committing your changes to a new branch, and pushing it back upstream.",
  tags: ["How To", "Tutorial", "Git", "Pull Request", "Open Source", "GitHub"],
  image: "step7-2.png",
};

<svg width="150" height="150" viewBox="0 0 40 40" style={{ float: "right", marginBottom: "6px", marginLeft: "12px" }}>
  <path d="M6.5 35v-4.8c0-5.4 4.3-9.7 9.7-9.7h7.6c5.4 0 9.7-4.3 9.7-9.7V6M6.5 32.5v-26" fill="none" stroke="#a3b7cc" />
  <path d="M6.5 10.5a4 4 0 110-8 4 4 0 010 8z" fill="#8bb7f0" />
  <path d="M6.5 3a3.5 3.5 0 110 7 3.5 3.5 0 010-7m0-1a4.5 4.5 0 100 9 4.5 4.5 0 000-9z" fill="#4e7ab5" />
  <path d="M33.5 10.5a4 4 0 110-8 4 4 0 010 8z" fill="#8bb7f0" />
  <path d="M33.5 3a3.5 3.5 0 110 7 3.5 3.5 0 010-7m0-1a4.5 4.5 0 100 9 4.5 4.5 0 000-9z" fill="#4e7ab5" />
  <g>
    <path d="M6.5 37.5a4 4 0 110-8 4 4 0 010 8z" fill="#8bb7f0" />
    <path d="M6.5 30a3.5 3.5 0 110 7 3.5 3.5 0 010-7m0-1a4.5 4.5 0 100 9 4.5 4.5 0 000-9z" fill="#4e7ab5" />
  </g>
</svg>

Contributing to an open-source project can be intimidating at first. The convoluted process of submitting your improvements for approval via a [**pull request**](https://help.github.com/en/articles/about-pull-requests) certainly doesn't help.

The following steps to submit a pull request will work on Git repositories hosted anywhere — on [GitLab](https://gitlab.com/), [Bitbucket](https://bitbucket.org/), [Azure DevOps](https://azure.microsoft.com/en-us/services/devops/repos/), etc. — but most open-source repositories one would want to contribute to are likely on [**GitHub**](https://github.com/), which is what we'll be using.

Starting from the very beginning, we'll fork an existing repository to our account, clone the fork locally, commit your changes to a new branch, and push it back upstream to GitHub to submit for approval.

## 1. Forking the Repository

Assuming you're using GitHub, this step is easy. Just find the repository you're contributing to and press the Fork button in the upper right. This will create an exact copy of the repository (and all of its branches) under your own username.

![Step 1](./step1.png)

## 2. Clone your new fork locally

GitHub will automatically redirect you to the forked repository under your username. This is the repository you need to clone to your local development environment, **not** the original. Grab the URL GitHub provides under the green "Clone or Download" button and plug it into the command below.

```bash
git clone git@github.com:jakejarvis/react-native.git
```

![Step 2](./step2.png)

## 3. Track the original repository as a remote of the fork

_This step is technically optional, but important if you plan to continue contributing to a project in the future, so we might as well..._

Once you've forked a repository, changes to the original (or "upstream") repository are not pushed to your fork. We need to tell the new repository to follow changes made upstream to keep it fresh via [remotes](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes).

Switch directories to the forked repository you just cloned and run the following commands. Replace the last part of the first line with the **original** repository clone URL — similar to the how you grabbed the URL in step 2, but this **isn't** the one with your username.

This links the fork back to the original repository as a remote, which we'll name `upstream`, and then fetch it.

```bash
git remote add --track master upstream git@github.com:facebook/react-native.git
git fetch upstream
```

## 4. Create a new branch for your changes

It's possible to make changes directly to the `master` branch, but this might FUBAR things down the road for complicated reasons. It's best to [`checkout`](https://git-scm.com/docs/git-checkout) a new branch for **each** change/improvement you want to make. Replace `fix-readme-typo` with a more descriptive name for your changes, like `add-mobile-site` or `update-dependencies`.

```bash
git checkout -b fix-readme-typo upstream/master
```

## 5. Make your changes!

This is either the easiest part or the hardest part, depending on how you look at it. 😉 At this point, you're isolated in the new branch you just created, and it's safe to open whatever text editor or IDE you use and go wild.

## 6. Add, commit, and push the changes

You're probably used to these commands. Add the files you've changed and commit them with a descriptive message.

```bash
git add .
git commit -m "Fix grammar mistakes in the readme file"
```

The one difference is the branch you're pushing to. You likely usually push to `master`, but in this case, we're pushing to the branch with the name you created in step 4.

```bash
git push -u origin fix-readme-typo
```

## 7. Submit your pull request

You're now all ready to submit the improvement you've made to the project's maintainers for approval. Head over to the original repositories Pull Requests tab, and you should see an automatic suggestion from GitHub to create a pull request from your new branch.

![Step 7.1](./step7-1.png)

![Step 7.2](./step7-2.png)

---

I'll admit, I need to refer back to these notes sometimes when I'm preparing to contribute to an open-source project. It's certainly not the most [intuitive](https://ohshitgit.com/) process, but at least it's **exactly the same** wherever the project is located — for example, I host my own [small Gitea server](https://git.jarv.is/) to back up some of my GitHub account. This instant compatibility between completely different services is precisely what makes Git great! 🏆
