A Beginner's Guide to Contributing to Open Source Using Git Commands:

Abhishek Jaiswal - Oct 19 - - Dev Community

Introduction

Contributing to open source can be one of the most rewarding experiences for developers, no matter their level of expertise. It allows you to work on real-world projects, collaborate with developers across the globe, and give back to the community. One of the key skills you'll need to master when contributing to open source is Git, a powerful version control system that helps developers manage changes to code and collaborate efficiently.

In this guide, I'll walk you through the essential Git commands and steps you need to know to start contributing to open source projects, from forking a repository to submitting your first pull request. We'll use a real-world example of how I recently contributed to Hacktoberfest 2024 by making a pull request to adjust the sidebar and navbar in a ReactJS project.

Let's dive in!


Why Contribute to Open Source?

Before jumping into the technical details, let’s talk about why open source contributions matter:

  1. Learn and Improve: You get to work on real-world projects, which helps you improve your coding skills and learn best practices.
  2. Collaboration: You get a chance to collaborate with developers globally, learn how teams work, and improve communication skills.
  3. Build Your Portfolio: Open source contributions make a great addition to your GitHub profile and resume.
  4. Giving Back: Open source projects often power much of the software we use every day. Contributing helps support and improve these projects for everyone.

Getting Started with Open Source

1. Choose a Project

Finding the right project to contribute to is key. Start by exploring projects that align with your interests or skill set. For example, if you're a frontend developer, you might want to contribute to repositories that use HTML/CSS, JavaScript, ReactJS, or similar technologies.

Some platforms that can help you find open source projects:

Once you find a project you're interested in, look for issues labeled as "good first issue" or "beginner-friendly" to start.


Essential Git Commands for Open Source Contributions

Now, let’s break down the essential Git commands and workflow for making your first open source contribution.


Step-by-Step Guide: Contributing Using Git Commands

1. Fork the Repository

When contributing to an open source project, you generally won't have direct write access to the repository. Instead, you need to "fork" the repository to make changes.

  • Fork: A fork is a copy of the repository that lives under your own GitHub account, which allows you to freely make changes without affecting the original project.

Command:

# No Git command for this, simply click "Fork" on the repository page on GitHub
Enter fullscreen mode Exit fullscreen mode

2. Clone the Forked Repository

Once you've forked the repository, you need to clone it to your local machine to work on it.

Command:

git clone https://github.com/your-username/repo-name.git
Enter fullscreen mode Exit fullscreen mode

This will create a local copy of the repository on your machine.

3. Create a New Branch

It’s good practice to create a new branch for every feature or bug fix. This keeps your changes isolated from the main codebase and allows for easier collaboration.

Command:

git checkout -b my-feature-branch
Enter fullscreen mode Exit fullscreen mode

This creates and switches to a new branch called my-feature-branch. Use a descriptive branch name that reflects the work you're doing (e.g., fix-sidebar-navbar).

4. Make the Necessary Code Changes

Now it's time to make your changes to the codebase. You can edit the files directly in your favorite code editor (e.g., Visual Studio Code, Sublime Text).

Once you’ve made your changes, you can check the status of the files you modified using:

Command:

git status
Enter fullscreen mode Exit fullscreen mode

This will show you which files have been modified and are ready to be committed.

5. Stage the Changes

After making your changes, you need to "stage" them before committing. This tells Git which changes you want to include in the next commit.

Command:

git add .
Enter fullscreen mode Exit fullscreen mode

The . adds all changes in the current directory. Alternatively, you can stage specific files by specifying the file name:

git add path/to/your/file.js
Enter fullscreen mode Exit fullscreen mode

6. Commit the Changes

Once your changes are staged, it's time to commit them with a meaningful message that explains what you've done.

Command:

git commit -m "Fixed sidebar and navbar adjustment in ReactJS"
Enter fullscreen mode Exit fullscreen mode

The -m flag allows you to include a commit message directly in the command. Make sure your message is clear and concise.

7. Push Your Changes to GitHub

Next, push your changes from your local branch to your remote (GitHub) repository.

Command:

git push origin my-feature-branch
Enter fullscreen mode Exit fullscreen mode

This pushes the my-feature-branch to your GitHub repository.

8. Open a Pull Request

Once your changes are pushed to GitHub, navigate to the original repository (not your fork) and click the "Compare & Pull Request" button that GitHub shows after you've pushed your changes.

Here, you'll create a pull request (PR) that summarizes the changes you’ve made and why they’re needed. It’s helpful to include:

  • What changes were made
  • Why the changes are important
  • How the changes were implemented

After creating your PR, the repository maintainers will review your contribution. If everything looks good, your code will be merged into the main project!


Summary of Key Git Commands

  • Fork the repo (done on GitHub)
  • Clone the repo: git clone https://github.com/your-username/repo-name.git
  • Create a new branch: git checkout -b my-feature-branch
  • Check status of changes: git status
  • Stage the changes: git add .
  • Commit the changes: git commit -m "Your commit message"
  • Push the changes: git push origin my-feature-branch

. . . . . . . . . . . .