0

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.

knittl
  • 246,190
  • 53
  • 318
  • 364
Deeksha
  • 35
  • 6
  • 1
    Start your new branch from master, not from your previous branch. – knittl Nov 19 '22 at 10:06
  • I have made this new branch from my master branch already. Previously I was having only one branch i.e master branch then for making this second PR I made a new branch from this master branch – Deeksha Nov 19 '22 at 10:18
  • 1
    In that case, it cannot include the commits of your other branch. Either your master branch includes those other commits already or you didn't create your new branch from master. Without showing the git commands you used to create the branch, we can only guess. – knittl Nov 19 '22 at 10:31
  • I used git checkout -b new_Branch – Deeksha Nov 19 '22 at 10:44
  • 1
    Deeksha, and assuming that your last commit was on old_branch, this will create the new branch from the old branch, not from master. To create a branch from master, you must use `git checkout -b newbranch master`. – knittl Nov 19 '22 at 10:46
  • My master Branch is showing 3 commits which I had done for my first pull Request. My new branch is showing 4 commits of which 3 commits were those previous commits and the 4th one is the commit I have done on this branch. – Deeksha Nov 19 '22 at 10:46
  • I only had two branches in this repo. The first one was the master branch and then I made this new Branch using git checkout -b new_branch – Deeksha Nov 19 '22 at 10:48
  • 1
    https://stackoverflow.com/search?q=%5Bgithub%5D+Old+commits+in+pull-request – phd Nov 19 '22 at 10:48

2 Answers2

1

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).

knittl
  • 246,190
  • 53
  • 318
  • 364
1

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.

SebDieBln
  • 3,303
  • 1
  • 7
  • 21
  • Will this affect the pull request I have made previously using master branch?This PR from the master branch is reviewed but not merged yet – Deeksha Nov 19 '22 at 11:29
  • 1
    @Deeksha Only changes to the branch you used for your pull request will affect the pull request. When you follow my recommendation you create a new branch that will not affect your existing branch nor your pull request. – SebDieBln Nov 19 '22 at 12:20