How to Push a Project and Contribute on GitHub?

GitHub is a powerful platform for hosting and collaborating on code repositories. Whether you’re working on an open-source project or collaborating with a team, knowing how to push a project and contribute on GitHub is essential. This article will guide you through the steps to push your project to GitHub and make meaningful contributions.

Table of Content

  • Setting Up Git and GitHub
  • Pushing a Project to GitHub
  • How to contribute to a repository?
  • How to save our changes to our forked repository?
  • How to create a pull request to the original repository?

Setting Up Git and GitHub

Before you can push a project and contribute, you need to have Git installed on your machine and a GitHub account.

  1. Install Git:
    • Download Git .
    • Follow the installation instructions for your operating system (Windows, macOS, or Linux).
  2. Create a GitHub Account:
    • Sign up for a free account at github.com.
  3. Configure Git:
    • Open your command line interface (CLI) or terminal.
    • Set your username and email with the following command.
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Pushing a Project to GitHub

Once you have Git and GitHub set up, you can push your project to a GitHub repository.

  1. Create a New Repository on GitHub:
    • Go to your GitHub account and click the + icon in the top-right corner.
    • Select New repository.
    • Enter a name for your repository and click Create repository.
  2. Initialize a Local Repository:
    • Navigate to your project directory in the CLI.
    • Initialize a Git repository:
      git init
  3. Add and Commit Your Files:
    • Add all your project files to the staging area:
      git add .
    • Commit the files with a meaningful message:
      git commit -m "Initial commit"
  4. Add the Remote Repository: Link your local repository to the remote repository on GitHub:
    git remote add origin https://github.com/your-username/your-repository.git
  5. Push the Project to GitHub: Push your committed changes to the remote repository:
    git push -u origin main

How to contribute to a repository?

First of all, we need to fork the repository on which we want to contribute to our own Github account. It can be done by clicking the fork option near the top-right corner of the repository. 

After that, we need to open the files of the forked repository on our local PC. For this, we can use Terminal or open Git Bash on that folder, which can be installed separately on our PC. Create a new folder, where we want to store the files from the repository, and then we need to run the following commands:

git init
git remote add origin "link to your forked repository"
git pull origin "branch name"

This will pull all the files from your forked repository to our local PC folder. Here, a typical “link to your forked repository” looks like this: 

https://github.com/username/repo-name.git

The command in the Git Bash will be:

If the repository has more than one branch, then we can shift our branches by running the following command in our Git Bash terminal:

git checkout "branch name"

For checking the branch name we are currently in, we need to run this command:

git branch

For example, if we are in the dev branch and we want to shift to the master branch, then we will use this command:

git branch
git checkout master
git branch

The output of the following command will be like this:

* dev
master
  dev
* master

Thus, in this way, we can change our branch in our local folder.

How to save our changes to our forked repository?

To add all the changes we made in our code to the local repository, we need to log in to our Github account in Git Bash and run the following command in Git Bash:

git add .
git commit -m "test commit"
git push -u origin "branch name"

If the branch name is master, then the last command would be:

git push -u origin master

This successfully copies all the updated files to our forked repository. We can also add files selectively using the specific file name which we want to push in our forked repository. We can check the files which are updated by running the command:

git status

This shows all the files that are updated by the user which are yet not committed. If the file name is “upgraded.js”, then the command will be:

git add upgraded.js
git commit -m "saving changes"
git push -u origin "branch name"

How to create a pull request to the original repository?

Only after we made certain changes and upgradations in our forked repository, the option to Open pull request will be visible on clicking Contribute button that indicates that your forked repository has some commits/upgradations that are yet not added to the original repository. Click on the contribute button, and it will open a page that allows us to open a pull request and set the name of your pull request, and you can write some additional description about the changes made. After submitting the pull request, the request is sent to the maintainer/owner of the original repository, which may be accepted if the maintainer likes the changes/upgrades made by us.

Click on the contribute button once changes are made in the forked repository

Creating a successful pull request

Here, we can write some description about our changes and finally open a pull request

Once he accepts the request, our changes will be visible in the original repository and thus, we successfully contributed to a project.