DEV Community

Ericawanja
Ericawanja

Posted on

How to contribute on GitHub

At the heart of GitHub is to make team collaboration seamlessly. You can only contribute to a public repository or a private repository where you have been added as a collaborator.

Step 1: Fork the repository

On GitHub move to the repository, you want to contribute to and fork it by clicking the fork button located on the upper right corner.
Image description

Step 2: Clone the fork

Cloning a git repository copies the content of the remote repository to your local device. While on the repository you have forked, click the green clone or download button and copy the displayed URL.
On your device terminal, run the below command to clone the repository.

git clone URL_OF_Forked_Repository
Enter fullscreen mode Exit fullscreen mode

Step 3: Create a branch

To start working on the codebase, you must first navigate to the project folder and create a branch.

cd projectfolder
Enter fullscreen mode Exit fullscreen mode
git checkout –b branchname
Enter fullscreen mode Exit fullscreen mode

Step 4: Make the changes, stage, commit and push them

After making the changes on your local repository, you need to make them available in the remote repository. Run the below commands
git status to check uncommitted changes
git add * to add all the changes to the staging area
git commit –m ‘the commit message’ command to commit the changes
git push origin branchName to push the changes to GitHub

Step 5: Pull the requests

on your GitHub account move to the repository that you have just committed the changes and refresh the page. You will see a green Compare and Pull request button. Click on it and the maintainer will merge the commits.

How to sync a repository

While pulling the request you may run to some troubles such as having your repository several commits behind the main repository. That happens when a team member pull request gets merged after you had forked the repository.
Consequently, you need to upstream the changes to sync it with your repo.

Step 1: Add a new remote upstream repository

you should add a remote upstream repository as the parent of your local repo. That will give it a reference project where it will inherit any changes made.
First, run git remote -v to check if your repo has a parent
If no parent is listed, run the below command to add one.

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Enter fullscreen mode Exit fullscreen mode

Step 2: Sync the repo

To sync the repo, first, move to the main branch.

git checkout main
Enter fullscreen mode Exit fullscreen mode

Now, fetch the changes from the original repository to your repo, by running the below command;

git fetch upstream
Enter fullscreen mode Exit fullscreen mode

Lastly, merge the changes with the main branch.

git merge upstream/main
Enter fullscreen mode Exit fullscreen mode

That’s it, your forked repo is up to date, you can go on push the changes and pull the request.

Top comments (1)

Collapse
 
jcubic profile image
Jakub T. Jankiewicz

You don't need to do all those things if you're starting. You can just use GitHub website. See How to make your first Open Source contribution in few seconds