Skip to content

Creating pull requests

Removing unwanted commits / changes

Sometimes we create branches and pull requests that includes too many commits. We are working fast so it's possible to switch branch and forget some history that comes from another ongoing work or branch.

In order to only include the commits relevant to your PR you can use the git cherry-pick [commit-sha] command.

Example

  • Issue: PR contains 2 commits, only 1 is relevant for the built feature. We want to keep the second commit 00812360d9afa433138fc02b2a292b350dd48f4calt text

  • Solution: New branch with cherry-pick. Another solution would be to git push -f if the origin PR branch is yours

    bash
    git fetch origin
    git checkout staging && git reset --hard origin/staging
    git checkout -b {my-branch-cleaned}
    
    # for each commit you want to keep
    git cherry-pick {commit sha}
    
    # check history is what you want
    git push origin {my-branch-cleaned}
    
    # then close old PR and open new one

    Alternatively, if you feel confident, you can override your existing Pull Request

    bash
    git checkout -b {my-branch}
    git fetch origin
    git reset --hard origin/staging
    
    # for each commit you want to keep
    git cherry-pick {commit sha}
    
    # check history is what you want
    git push -f origin {my-branch}

Result alt text