Reputation: 5729
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
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:
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
* a
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
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
Reputation: 3443
Assuming the 12 commits history is not important, I will do basically like:
git reset HEAD~12
git stash
git pull upstream master --rebase
to sync with upstreamgit stash pop
to pop up the changes and apply on top of the latest versiongit commit -m "some message"
git push -f origin YOUR_BRANCH
If you care about the commits history, then maybe do as @Tim Biegeleisen suggested
Upvotes: 3