Skip to content

Instantly share code, notes, and snippets.

@Jlevyd15
Created November 17, 2019 17:58
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Jlevyd15/66743bab4982838932dda4f13f2bd02a to your computer and use it in GitHub Desktop.
Save Jlevyd15/66743bab4982838932dda4f13f2bd02a to your computer and use it in GitHub Desktop.
Stacked PRs with Git and Github

How to create a stacked PR with git and Github 📚

Create a feature branch from the master brach

  • from the master branch create a feature branch called feat1
  • git checkout master -> git checkout -b feat1

Make some changes and commit them

  • git add .

  • git commit

  • From here we'll start the process of splitting the commits across multiple branches. To do this we'll create another feature branch big_feat1 based off of the original feature branch feat1

Create the first branch

This command creates a new branch called big_feat1 based off of feat1 and checksout the new branch (big_feat1)

  • git checkout -b big_feat1 feat1 The following command will unstage the changes that were pulled over from the original brach so we can review and commit them across other branches. (FYI this leaves the original feat1 branch unchanged)
  • git reset master
  • git status

Add changes to it

Here we'll use the patch option for git add. We use patch so that we have more control over the specific changes we're adding within each file. In case we have changes that need to be added through various commits in a single file.

  • git add --patch
  • use the y command to just add everything
  • use the s command to split the file's changes into various commits
  • there are many other options as well. Each depend on the use case

Stash other changes before committing

Before committing the changes stash the reset of the changes that we'll add and commit later

  • git stash --include-untracked --keep-index
  • --include-untracked will include new files as well
  • --keep-index will tell git to not mess with your already staged changes from the previous step

Commit and push the changes

  • git commit
  • git push origin big_feat1

Create another branch

Now we have our first branch ready for review we can start to create the second branch

  • git checkout -b big_feat2 big_feat1
  • git stash pop

More of the same

  • from here just follow the same steps listed after we created the first branch.

Pro Tip 💡

  • use git merge to resolve merge conflicts across all the stacked braches. If you use rebase you'll have to resolve the same conflicts in all of the stacked branches.

Thats it!

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