Skip to content

Instantly share code, notes, and snippets.

@ezenity
Created November 11, 2023 18:41
Show Gist options
  • Save ezenity/8d615d822aa59a14898c4e369d408ede to your computer and use it in GitHub Desktop.
Save ezenity/8d615d822aa59a14898c4e369d408ede to your computer and use it in GitHub Desktop.
Git Contribution Cheat Sheet for GitHub Projects

Git Contribution Cheat Sheet for GitHub Projects

1. Setting Up Your Environment

  • Clone the Repository:
    git clone [repository URL]
    
    

2. Creating a New Branch

  • Check Current Branch:
    git branch
    
  • Create a New Branch:
    git checkout -b [new-branch-name]
    
    This creates a new branch and switches to it.

3. Making Changes

  • Add Changes to Stage:
    git add [file-name]
    
    or for all changes:
    git add .
    
  • Commit Changes:
    git commit -m "Your commit message"
    
    Write a clear, concise commit message explaining your changes.

4. Updating Your Branch with Latest Changes from Main Branch

  • Switch to Main Branch:
    git checkout main
    
  • Pull Latest Changes:
    git pull origin main
    
  • Switch Back to Your Branch:
    git checkout [your-branch-name]
    
  • Merge Changes from Main to Your Branch:
    git merge main
    
  • Resolve any merge conflicts if they arise.

5. Pushing Your Branch to GitHub

  • Push Branch:
    git push origin [your-branch-name]
    
    If it's the first push for the branch, Git will provide a command to set the upstream branch.

6. Creating a Pull Request (PR)

  • Open GitHub Repository in a Browser.
  • Navigate to 'Pull Requests' and click 'New Pull Request'.
  • Select your branch and the branch you want to merge into (usually main/master).
  • Fill in the PR template, providing context about the changes.
  • Request reviews from maintainers or team members.

7. Addressing PR Comments

  • Make any requested changes on your local branch.
  • Commit and push these changes as before. The PR will automatically update.

8. Merging the PR (Usually Done by Maintainers)

  • Once the PR is approved, it can be merged into the main branch.
  • Delete your branch from GitHub if it's no longer needed.
    • Optional: Some projects require branches not to be removed for tracking purposes

9. Cleaning Up After Merge

  • Switch to Main Branch:
    git checkout main
    
  • Pull Latest Changes:
    git pull origin main
    
  • Delete Local Branch (optional):
    git branch -d [your-branch-name]
    
    Only do this if you're sure you won't need the branch anymore.

Tips:

  • Keep Your Branches Updated: Regularly pull changes from the main branch to avoid large merge conflicts.
  • Commit Messages: Write meaningful commit messages to document your changes effectively.
  • Handle Merge Conflicts: In case of merge conflicts, carefully resolve them considering the latest code on the main branch.

This cheat sheet covers the typical workflow for contributing to a project on GitHub. Remember, each project may have its own specific guidelines, so it's always good to read the project's CONTRIBUTING.md file if available. Happy coding! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment