Home > Back-end >  I made one PR from master branch which is yet to be merged. Now I want to make a second PR without s
I made one PR from master branch which is yet to be merged. Now I want to make a second PR without s

Time:11-19

I made my first Pull request from the master branch which is reviewed but not merged yet. Now I tried making a second Pull Request by making a new branch. But the problem I am facing is that this second Pull Request is showing the commits of the first Pull Request as well. I do not want to have previous commits in this Pull Request

Can anyone please suggest a way so that I can make this second PR having only the commits I made for this PR.

CodePudding user response:

I recommend creating a new branch at the remote(!) master (since your local master might point somewhere else) and then cherry-picking the commits you want from the branch you already have:

git checkout -b my-new-feature-branch remotes/upstream/master

git cherry-pick <sha1-of-a-commit>

Repeat the last step for every commit you want to cherry-pick.

CodePudding user response:

A branch in Git is a label that can be freely moved. It points to a commit. Each commit points to its parent commit(s). When you create a branch and don't specify its starting point, it will be created pointing to HEAD, which is your currently checked-out commit/branch.

Assuming you already had commits made commits while working on your current branch: when you create a new branch, these commits are also reachable from the new branch. As long as they are not merged into your target branch, they will show up as "commits to be merged" (in GitHub PRs or with gitk target..yourbranch).

Visualized:

A-B-C           master, origin/master
     \
      D-E-F     old-branch
           \
            G   new-branch

What's the way out? You can either re-create the branch and its changes on top of the target branch (probably origin/master) or you can rebase (which will rewrite history, make sure you understand the implications before doing this):

git rebase --onto origin/master oldbranch newbranch

Your commit graph after rebasing:

      G'        new-branch
     /
A-B-C           master, origin/master
     \
      D-E-F     old-branch

The old commit from new-branch (G) still exists, but is no longer reachable via named refs (branches or tags). G' is a new, independent commit (which can be seen from its commit id/hash) with the same (or very similar) patch as the old G (i.e. git diff "G'^" "G'" is the same as git diff G^ G).

Any commits reachable from old-branch or master will not be affected by the rebase-operations. (Remember: Git commits are immutable and can never change. You could only make them unreachable).

  • Related