DEV Community

PGzlan
PGzlan

Posted on

Git Basics: 10 Essential Commands for Contributing to Open Source Projects

Git logo
Contributing to open source projects can be an enriching experience for developers, allowing them to collaborate with a global community and make a meaningful impact. Git, the widely adopted version control system, plays a crucial role in facilitating smooth collaboration and efficient code management.
In this article, we will explore 10 essential Git commands that every open source contributor should master.

These commands will empower you to navigate the open source landscape with confidence and make your contributions seamless.

  1. git clone:
    Description: This command is used to create a local copy of a remote repository on your machine.
    Example Usage: git clone https://github.com/open-source-project.git

  2. git branch:
    Description: Branching allows you to create isolated environments to work on specific features or fixes without affecting the main codebase.

Example Usage:

    • Switch to a branch: git checkout new-feature
    • Create a new branch: git branch new-feature
  1. git add: Description: This command adds changes to the staging area, preparing them for commit. The "staging" phase acts as an intermediary step between your working directory and the repository, allowing you to selectively choose and review the changes that will be included in the next commit

Example Usage:

  • - Add all changes: git add .
    • Add a specific file: git add file.js
  1. git commit: Description: Commits the changes made to the repository, creating a new snapshot in the version history.

The -m flag is used to denote the commit message. If you do not pass the commit message using said flag, you'll have to fill it using either nano or vim based on your installation type.

Additionally, instead of making multiple commits to make minor changes such as commenting or uncommenting a line, etc, you can use the --amend flag (Note: amend replaces the most recent commit, it does not just alter it)

Example Usage: git commit -m "Implemented feature X"

  1. git push:
    Description: Pushes your local commits to a remote repository, making them available for others to see and merge.
    Example Usage: git push origin branch-name

  2. git pull:
    Description: Fetches and merges the latest changes from a remote repository to your local repository.
    Example Usage: git pull origin branch-name

  3. git fetch:
    Description: Retrieves the latest changes from a remote repository without merging them into your local branch immediately.
    Example Usage: git fetch origin

  4. git merge:
    Description: Integrates changes from one branch into another.

Example Usage:

  • Merge a branch into the current branch: git merge branch-name

  • Merge changes with a specific commit: git merge commit-hash

  1. git rebase:
    Description: Allows you to incorporate changes from one branch into another by applying commits individually.
    Example Usage: git rebase branch-name

  2. git reset:
    Description: This command is used to undo commits or reset the repository to a previous state.
    Example Usage:

-  Discard the most recent commit: `git reset HEAD~1`

- Reset to a specific commit: `git reset commit-hash --hard`
Enter fullscreen mode Exit fullscreen mode

Here is an example of what a workflow might be in git

# Clone a remote repository
git clone https://github.com/open-source-project.git

# Create a new branch to work on a feature
git branch new-feature

# Switch to the new branch
git checkout new-feature

# Make changes to the codebase and stage them for commit
git add file.js

# Commit the changes
git commit -m "Implemented feature X"

# Push the commits to the remote repository
git push origin new-feature

# Fetch the latest changes from the remote repository
git fetch origin

# Merge changes from a branch into the current branch
git merge branch-name


# Rebase changes from one branch onto another
git rebase branch-name

# Reset the repository to a previous commit
git reset commit-hash --hard
Enter fullscreen mode Exit fullscreen mode

By leveraging Git's capabilities, you become an integral part of the open source movement, working hand in hand with fellow developers worldwide to create amazing software and drive innovation. Embrace these commands, keep learning, and open new doors to endless possibilities in the world of open source.

Happy coding and contributing!

References & readings:
Git --amend
Git docs

Top comments (0)