Git Commands for Open-Source Contribution

Git Commands for Open-Source Contribution

Quick reference of git commands during contribution

ยท

3 min read

The step-by-step guide to contributing to open-source projects:

  1. Fork Repository: Top-right corner second button is a Fork.

  2. Clone Repository: Copy the URL of the repo to clone in your local directory. Open Git Bash, select the directory where you want to clone your repo, right-click and open Git Bash.

    git clone paste-your-URL

  3. Create a new branch:

    Change to the repository directory using:

    cd your-repo-name

    Create a branch using git switch or git checkout command:

    git switch -c your-new-branch-name

    or

    git checkout -b your-new-branch-name

  4. Make your changes (here you can find issues or improve code or add your name to the contribution list).

  5. Add those changes to the branch using the git add command.

    To add all changed files use:

    git add .

    Add specific changed file use:

    git add changed-file-name

  6. Commit those changes using git commit command:

    git commit -m "Add your message here"

  7. Push your changes to GitHub using git push command:

    git push -u origin your-branch-name

  8. Go to your repository on your GitHub, you'll click Compare & pull request button to add your PR.

  9. Finally, add your PR title and comment, then you'll click Create pull request button to complete your first pull request (PR).

Congratulations ๐ŸŽ‰ you have completed your first contribution.

Note: If I get code from the main branch to my own branch, I will run the following commands in my branch terminal:

  1. Switch to my own branch: git checkout own-branch-name or git switch own-branch-name

  2. Fetch the latest updates from the origin: git fetch origin

  3. Merge with the main branch: git merge origin/main

You can revert individual commits using git revert <commit_hash>, which creates a new commit that undoes the specified commit's changes. To revert a range of commits, use git revert <oldest_commit_hash>..<latest_commit_hash>.

To discard changes in your working directory and revert to the last committed state, you can use the following Git commands depending on your situation:

  1. If you have uncommitted changes (files not staged for commit):

    Use the git checkout command to discard changes in specific files:

     git checkout -- <file>
    

    Or, to discard changes in all files:

     git checkout -- .
    
  2. If you have staged changes (files added to the staging area):

    First, unstage the files:

     git reset
    

    Then discard the changes:

     git checkout -- .
    
  3. If you want to discard both staged and unstaged changes and revert to the last commit:

    You can use the git reset command with the --hard option:

     bashCopy codegit reset --hard
    

    Be careful with this command, as it will permanently discard all your local changes and cannot be undone.

List of some beginner's Open-Source Contribution Repository:

ย