rj.learn
rj.learn

Reputation: 645

Commits from one pull request showing in a second one

I'm new to GitHub and I have a problem that commits from a first pull request are showing in a subsequent pull request. That is:

Pull Request 1 (PR1)

I see commits:

Pull Request 2 (PR2)

I see commits:

I don't know what happened.

Why do the commits from PR1 also appear in PR2, and how can I fix it?

Upvotes: 2

Views: 176

Answers (1)

Alex Harvey
Alex Harvey

Reputation: 15472

It would be because you either didn't branch at all, or you used the PR1 branch as the starting point for PR2.

One option would be simply to close PR1 and have PR2 merged, or just merge PR1 and then PR2. Either of these options will be fine. In what follows, I assume that for some reason you can't do this and you need to them to be merged as two separate PRs.

To fix, you would interactively rebase on the PR2 branch, and "drop" the commits you don't need.

I assume you are on the branch pr2branch.

git rebase -i HEAD~~~~ 

A screen appears with a list of commits like:

pick pr2sha1 Commit message pr2sha1
pick pr1sha2 Commit message pr1sha2
pick pr1sha1 Commit message pr1sha1
pick 4dced1f Commit message 

You should also see instructions underneath in comments. You should read those instructions.

Anyhow, since you want to "drop" the commits relating to PR1, you would change the text to:

pick pr2sha1 Commit message pr2sha1
drop pr1sha2 Commit message pr1sha2
drop pr1sha1 Commit message pr1sha1
pick 4dced1f Commit message 

Then save that, and then update the PR:

git push origin pr2branch --force

For more on interactive rebasing see e.g. here.

To avoid this next time, always do these steps, after you push to raise a PR.

(If the main branch is called master and the new branch for your second PR is new-pr-branch.)

git checkout master
git pull
git checkout -b new-pr-branch

(Although some people recommend not using git pull, e.g. here.)

Upvotes: 2

Related Questions