'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?
Solution 1:[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:
Fork repository
You forked the project locally
* a (HEAD, master, origin/master) ...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 * aClean your work
It is time to clean your commit with
$ git checkout -b feature/foo $ git rebase -i HEAD~5Eventually 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 |/ * aDo the pull request
Now you can push your work and create a new pull request:
$ git push --set-upstream origin feature/foo
Solution 2:[2]
Assuming the 12 commits history is not important, I will do basically like:
git reset HEAD~12git stashgit pull upstream master --rebaseto sync with upstreamgit stash popto pop up the changes and apply on top of the latest version- Fix conflict if any, and get rid of the old commit
git commit -m "some message"git push -f origin YOUR_BRANCH
If you care about the commits history, then maybe do as @Tim Biegeleisen suggested
- Fetch the latest version from upstream
- Create a new branch
- cherry pick the commits you want to include one by one in correct order
- Send a new PR
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Chuan |
