2

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?

Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74
  • 1
    I can't give a full answer here. If you made one or more commits which fixed the other issue, but those commits are sitting on top of a branch which is somehow out of date, then perhaps the easiest fix here would be to just cherry pick those commits onto a branch/base which is correct. – Tim Biegeleisen Mar 13 '19 at 13:14
  • Possible duplicate of [Create new pull request from fork without having commits of the previous fork](https://stackoverflow.com/questions/42885428/create-new-pull-request-from-fork-without-having-commits-of-the-previous-fork) – phd Mar 13 '19 at 15:08
  • https://stackoverflow.com/search?q=%5Bgithub%5D+old+commits+pull+request – phd Mar 13 '19 at 15:08

2 Answers2

3

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
Chuan
  • 3,103
  • 1
  • 19
  • 23
1

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
    
nowox
  • 25,978
  • 39
  • 143
  • 293