Git Workflow for Contributing

Difficulty: Intermediate

Question

Describe the Git workflow you use when contributing to an open-source project. How do you keep your fork in sync and handle merge conflicts?

Answer

A disciplined Git workflow is essential for smooth contributions, especially when working across multiple PRs or with active upstream repositories.

Start by forking the upstream repository and cloning your fork. Add the upstream repository as a remote (git remote add upstream URL). Before starting any work, sync your main branch: git checkout main, git fetch upstream, git merge upstream/main (or rebase).

Create a feature branch from the latest main: git checkout -b fix/issue-description. Make small, atomic commits with clear messages. I use Conventional Commits format (feat:, fix:, chore:, docs:, refactor:).

When the upstream main has moved forward while you're working, rebase your branch: git fetch upstream, git rebase upstream/main. This keeps your commits on top of the latest changes. If conflicts arise, resolve them carefully, testing after each resolution.

Before opening a PR, squash related commits into logical units using git rebase -i. Force push to your branch (git push --force-with-lease). Open the PR against the upstream repository.

During review, make additional commits for requested changes. After the PR is approved, the maintainers may squash-merge, so don't squash prematurely.

Key points

Concepts covered

Git, Rebase, Merge, Squash, Sync, Fork