Skip to content

Instantly share code, notes, and snippets.

@Bunix
Forked from Jlevyd15/stacked_prs.md
Created August 30, 2020 08:26
Show Gist options
  • Save Bunix/ae7e6b5bc05d9bc301bf32cfbc60c517 to your computer and use it in GitHub Desktop.
Save Bunix/ae7e6b5bc05d9bc301bf32cfbc60c517 to your computer and use it in GitHub Desktop.

Revisions

  1. @Jlevyd15 Jlevyd15 created this gist Nov 17, 2019.
    52 changes: 52 additions & 0 deletions stacked_prs.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    # 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!