Git Contribution Best Practices
Preparation
- Understand the Project Goals: Familiarize yourself with the project’s mission, goals, and current issues by reading its README,
CONTRIBUTING.md
, and other documentation.
- Review Open Issues: Look through open issues to find tasks that match your skill level or interest. Beginner-friendly issues are often labeled (e.g.,
good first issue
).
- Follow the Project's Style Guidelines: Review any style or coding guidelines provided by the project and adhere to them.
Forking and Cloning
- Always Fork the Repo: Avoid working directly on the main repository. Fork the repo to your GitHub account and clone it locally.
- Add and Verify the Upstream Repository: Set the original repo as the upstream and fetch updates regularly to keep your fork synced.
Working on Your Contribution
- Create a New Branch: Always create a new branch for each contribution (e.g.,
fix-bug-123
or add-feature-x
). Never work on the main
branch.
- Make Small, Focused Changes: Avoid bundling multiple features or fixes into a single PR. Keep each PR focused and limited in scope.
- Write Meaningful Commit Messages: Use clear, descriptive commit messages that explain the why and what of your changes (e.g.,
Fix login button alignment on mobile
).
Testing and Linting
- Run Existing Tests: Before making changes, run the project's tests to ensure the environment is correctly set up.
- Write New Tests: Add unit tests or integration tests for your changes, if applicable.
- Lint Your Code: Use the project’s linting tools to ensure your code adheres to style guidelines.
Syncing Your Fork
- Fetch and Rebase Regularly: Before pushing or creating a PR, fetch the latest changes from
upstream
and rebase your branch to avoid conflicts.
- Resolve Conflicts Gracefully: If conflicts arise during rebasing, carefully resolve them and double-check the changes.
Creating a Pull Request
- Create a Draft PR: If your work is incomplete but you’d like feedback, create a draft PR on GitHub.
- Provide a Detailed Description: Write a clear PR title and description. Explain what your changes do, why they’re necessary, and link any related issues (e.g.,
Fixes #123
).
- Follow PR Templates: Many projects provide templates for PRs. Use them to structure your submission.
Responding to Feedback
- Be Open to Feedback: Accept constructive criticism and be willing to make changes.
- Address Feedback Promptly: Update your branch to incorporate requested changes and push the updates to your fork.
- Communicate Clearly: If you’re unsure about feedback, ask questions to clarify the reviewer’s expectations.
General Etiquette
- Be Respectful: Always communicate politely with maintainers and contributors.
- Contribute Documentation: If you find missing or outdated documentation, consider improving it.
- Avoid Unnecessary Changes: Do not submit changes unrelated to the issue you’re addressing, like reformatting unrelated code.
Technical Best Practices
- Use Descriptive Branch Names: Use branch names that describe your work clearly (e.g.,
fix-typo-in-readme
or add-user-auth
).
- Avoid Force-Pushing to Upstream: Use force-push (
--force-with-lease
) only on your PR branch in your fork, and never on the upstream repo.
- Test Across Environments: Test your changes across all environments supported by the project (e.g., Windows, macOS, Linux).
- Squash Commits: Before finalizing your PR, squash commits to clean up your commit history (if required by the project).
git rebase -i HEAD~<number-of-commits>
After the Contribution
- Follow Up on Your PR: Monitor the status of your PR and respond to comments in a timely manner.
- Celebrate Merges: If your PR is merged, celebrate your contribution and thank the maintainers!
- Clean Up Branches: Delete your local and remote branches after your PR is merged to keep your repo tidy.
Ongoing Contributions
- Stay Active: Continue contributing to the project to build trust and familiarity with maintainers.
- Participate in Discussions: Engage in issue discussions or community forums to help others and learn more.
- Be Patient: Understand that maintainers are often volunteers, and PR reviews may take time.
What to Avoid
- Avoid Premature PRs: Don’t open a PR for incomplete work unless it's a draft and you're seeking feedback.
- Avoid Overwriting Maintainer Code: Respect existing work and discuss significant changes before implementing them.
- Don’t Assume Ownership: Be mindful that the project belongs to the community, not just to you.