Essential Git commands for open source contributions

Essential Git commands for open source contributions

Explore the basic git commands used during open source contributions and collaborations.

Git is the widely used and the most famous version control system for open source projects and collaborative development. Whether you are a seasonal contributor or novice, mastering the essential git commands are valuable in seemless collaboration. In this article, we will cover the essential commands that we frequently encounter during open source contributions.

Cloning a repository

The first step to contributing to an open source project or repository in GitHub is to clone the repository to your local machine. This can be achieved by using the command git clone:

git clone <repository URL>

This command will create a copy of the entire repository for you to make changes in it and contribute to it.

Creating a new branch

A lot of novice forget this step, but it's essential to create a new branch to isolate your work from the main codebase. Use the git checkout -b command to switch to a new branch:

git checkout -b <new-branch-name>

Replace <new-branch-name> with a descriptive name for your branch, such as "feature/add-new-feature" or "bugfix/fix-issue-123".

Staging and commiting changes

After making the changes in the local machine's codebase, stage the modified files for commit using git add command:

git add <file>

You can also use git add . command to stage the changes from current repository and all subdirectories.

Once the changes are staged, commit them with a message using the git commit -m command:

git commit -m "Commit message"

Pushing the changes to the Remote Repository

Now share your changes with others, push your commits to the remote repository using git push:

git push origin <branch-name>

Replace <branch-name> with your branch name. Incase of main development branch replace it with "main" or "master".

Fetching and Merging Remote Changes

Regularly fetch and merge changes from the remote repository to stay up-to-date with the latest developments. Use the git fetch and git merge commands:

git fetch origin
git merge origin/main

Replace origin/main with the remote branch you want to merge into your local branch.

Conclusion

Mastering these essential git commands will come handy during open source contributions and hackathons. By understanding how to clone repositories, create branches, stage and commit changes, push to remote repositories, and fetch and merge changes, you'll be well-equipped to collaborate with others and make meaningful contributions to the open-source community.