Govinda Sakhare
Govinda Sakhare

Reputation: 5729

How to exclude old commits in new pull request?

I had contributed to an open-source project on GitHub, and PR was declined by a maintainer. After a few months, I raised one PR to fix some other issue but this new PR is included older commits also. How can I exclude the old commits?

I also got the below git commands from the maintainer:

git checkout master
git fetch upstream
git reset upstream/master --hard

Can someone explain what exactly each of these commands is doing? is there any alternate way to remove old commits?

Upvotes: 2

Views: 5827

Answers (2)

nowox
nowox

Reputation: 29066

Git is based on a directed acyclic graph, it means you cannot exclude or ignore part of the history without compromising the integrity of your repository.

However, you may rebase your PR branch locally and either:

  • Force push you PR branch (only if you know what you are doing)
  • Close your PR and create a new one with the rebased history.

To exclude commits from your pull request branch you would perform an interactive rebase locally:

git checkout -b feature/failure-analyzer
git rebase -i HEAD~10 # Where 10 is the number of commits you want to rebase
git push --set-upstream origin feature/feature-analyzer

Let's review this with your scenario:

  1. Fork repository

    You forked the project locally

    * a (HEAD, master, origin/master)
    ...
    
  2. Implement your new feature

    During your development flow you made some mistakes and you have noisy commits. Unfortunately you already pushed your changes on your forked repository.

     * e implementation done (HEAD, master, origin/master)
     * d again oops
     * c oops 
     * b implement feature foo
     * a 
    
  3. Clean your work

    It is time to clean your commit with

     $ git checkout -b feature/foo
     $ git rebase -i HEAD~5
    

    Eventually you get this history:

     * f implement feature foo (HEAD, feature/foo)
     | * e implementation done (master, origin/master)
     | * d again oops
     | * c oops 
     | * b implement feature foo
     |/
     * a 
    
  4. Do the pull request

    Now you can push your work and create a new pull request:

      $ git push --set-upstream origin feature/foo
    

Upvotes: 1

Chuan
Chuan

Reputation: 3443

Assuming the 12 commits history is not important, I will do basically like:

  1. git reset HEAD~12
  2. git stash
  3. git pull upstream master --rebase to sync with upstream
  4. git stash pop to pop up the changes and apply on top of the latest version
  5. Fix conflict if any, and get rid of the old commit
  6. git commit -m "some message"
  7. git push -f origin YOUR_BRANCH

If you care about the commits history, then maybe do as @Tim Biegeleisen suggested

  1. Fetch the latest version from upstream
  2. Create a new branch
  3. cherry pick the commits you want to include one by one in correct order
  4. Send a new PR

Upvotes: 3

Related Questions