I come from IBM RTC, so I need to get used to Git.
I have forked a repository, done a couple commits on my master branch and opened a pull request.
Pull request:
original-repository/master <- my-repository/master
commit-1
commit-2
I then created a new branch and pushed a change. I opened another pull request starting from the new branch, and that's what I find.
Pull request:
original-repository/master <- my-repository/newbranch
commit-1
commit-2
commit-3
What if I want to have a pull request with only commit-3
?
I'm really surprised it's not a duplicate... It turns out I already answered almost the same question. Commit-wise it is a duplicate (i.e. there is a branch with 2 unnecessary commits). However, the problem is stated in quite a different way (e.g. the other question doesn't mention pull requests), and I wouldn't find it if it wasn't my answer there.
Use git rebase --onto
.
In your local repository:
git checkout newbranch
git rebase master --onto origin/master
git push origin +newbranch # May look slightly different for you; The point is, you need to force push (the + does that).
NB, in the second line, master
could be replaced by HEAD~
or other reference to commit-2
.
Short explanation: git takes commits between the one pointed to it (in this case master
) and current commit, and replicates them one-by-one at the point defined be --onto
. If some commits cannot be applied automatically, the process is suspended and you have to resolve conflicts manually.
Caveat: do not force-push unless you are sure it's safe. If you push to your own fork repository and can assume no one else uses it, you are probably fine (even if it's technically public). It is your private branch, which sometimes may be enough to mean it's fine. But it's important to know risks related to deleting the history (or attempting to).
Alternatives (cherry-pick): my answer to the other question details some alternatives. The situation there is essentially the same, branch F1 contains unnecessary commits.
来源:https://stackoverflow.com/questions/50144367/git-branching-pull-request-on-head-branch-takes-also-prior-branch-commits