After creating a personal branch for a Git project, is it okay to commit prototypes, debugging changes, or other experiments that will never be merged into the main branch? If those changes should stay local, what is the usual workflow for keeping them around without accidentally including them in a pull request?
4 Answers
Only the commits and file changes that are part of the branch history compared with the target branch will appear in a pull request. You do not need to upload individual files selectively. If an experimental commit is already mixed into the branch, you can create a clean branch from the appropriate base and cherry-pick only the commits you want, or interactively rebase to remove or edit commits. Keeping experiments on a separate branch from the beginning is usually easier.
Branches are cheap, so a clean workflow is to make a separate experimental branch from your working branch. Keep the feature work on one branch and use another for prototypes or rabbit holes. When you are finished, you can delete the experiment or reset it to an earlier commit. Avoid committing passwords, tokens, or other secrets even on a private local branch.
For changes that are only temporary or machine-specific, use .gitignore for files Git should never track and stash for work you want to set aside briefly. If the experiment is worth preserving, commit it on a local-only branch. Just remember that anything committed on a branch can become difficult to merge later if the main codebase changes significantly.
A branch is yours to use however you want, especially if it stays local. The important distinction is whether you push it or open a pull request. A pull request compares your branch with its target branch, so unrelated commits and changes can make the review confusing. For experiments, keep them uncommitted, stash them, or create a temporary branch that you never push.
So before opening a pull request, would I need to manually remove anything experimental from the branch?

That clears up the difference between selecting files and selecting commits. It sounds like separate branches are safer than trying to clean up a mixed branch later.