Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created February 3, 2022 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twolfson/eddc510eee6ba0e260c3d8ab707c4466 to your computer and use it in GitHub Desktop.
Save twolfson/eddc510eee6ba0e260c3d8ab707c4466 to your computer and use it in GitHub Desktop.
How I stack PRs in GitHub
The following process is rather tedious but removes headaches from dealing with merge conflicts due to using a historically timed commit
When starting stacked work:
- git status # On pr1 branch
- git checkout main
- git checkout -b pr2.base # This will act as base for easy PR review and merge conflict resolution
- git checkout pr1 -p # Pull over all changes from pr1 into a single commit, including file deletion (git checkout -- . would miss this) (single commit removes noisy git history from PR)
- git commit -m "Single commit containing pr1 changes"
- git checkout -b pr2
- # Work on pr2
When opening PR:
- Open with pr2.base version as base and pr2 as branch in GitHub UI
When landing PR:
- Land pr1 first to main
- In PR, change base from pr2.base to main in GitHub UI (so it won't close the PR when we delete the base branch)
- Locally:
- git checkout main
- git pull # Update main to latest
- git checkout pr2.base
- git merge - # Merge last branch (main)
- git mergetool -y # All conflicts should resolve with main winning, since it's supposed to just be a collapsed historical commit
- git diff main # Sanity check pr2.base is 1:1 with main
- git checkout pr2
- git merge - # Merge last branch (pr2.base)
- git mergetool -y # as needed
- git diff origin/$(BRANCH) # Verify PR looks like it did before — $(BRANCH) comes from twolfson/sexy-bash-prompt
- git diff $(BRANCH).base # or main # Verify no unexpected changes
- git push --all
- In GItHub UI, sanity check merge commits are nothing or minimal
- Locally again:
- git delete-branch $(BRANCH).base # git-delete-branch from git-extras
- # PR is now unstacked and with minimal merge conflict wrangling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment