-2

I am working on a project and resolving issues, I am creating a different branch for every single pull request (PR).

Last time I created two different branches with their PRs and resolved both of the issues. Now the problem is that both of my pull requests got messed up with the codes that I pushed on different branches. I created a branch on git bash with the help of the following commands: git branch branch_name and then git checkout branch_name.

My commit graph looks like this at the moment:

aaaaa (feature/c)
bbbbb (feature/b)
ccccc
ddddd (feature/a)
eeeee
fffff
ggggg
hhhhh (master, origin/master)

Can anyone guide me on how can I keep each PR's code separate from each other? So that:

  • feature/c won't contain the changes of feature/b and a
  • feature/b won't contain the changes of feature/a
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • How do you construct the different branches? – Willem Van Onsem Jan 13 '22 at 17:56
  • with the help of cli. git branch branch_name – djangodeveloper Jan 13 '22 at 18:18
  • I don't know if I understand your situation correctly: Do you have one or more pull-request(s) that contain(s) more commits than you actually want to have merged? In this case you would need to look at [git-rebase](https://git-scm.com/docs/git-rebase) and [git-push --force](https://git-scm.com/docs/git-push). – SebDieBln Jan 13 '22 at 20:38
  • You'll need to explain more precisely what you mean by "got messed up", because "keeping changes separate" is what branches do *by definition*. We can't really tell you how to avoid a problem with knowing what the problem is, or what *exactly* you did that caused it. – IMSoP Jan 13 '22 at 21:11
  • hi @SebDieBln thanks for your help, I have more than 1 PRs that contains more than 1 commit. – djangodeveloper Jan 13 '22 at 21:19
  • @IMSoP thanks, I mean I have a master branch, and let suppose there are 3 different issues. now what I am doing is, creating a new branch from the master branch after forking it and then resolving the issue and creating a Pull request after pushing the code to the relevant branch. so same procedure goes for other branches and issues\ – djangodeveloper Jan 13 '22 at 21:23
  • most importantly, there is an issue which is still not closed and I am making new branches to create pull requests after resolving the issues so that repository owner can review my code. – djangodeveloper Jan 13 '22 at 21:28
  • @jessehouwing I faced the same issue again. but I didn't try your way of the solution because I created the PR before your comment. how to remove other PRs codes from new PR code? – djangodeveloper Jan 13 '22 at 21:40
  • actually, I want to keep the code separate each time. it is being mixed with other PR code. I amconfused. – djangodeveloper Jan 13 '22 at 21:54
  • I don't really understand what you're trying to do, but I suspect you may be struggling with what a PR actually is. A PR is a request to merge one of *your* branches into a *shared* branch, such as `master`. You create 1 PR per branch you wish to merge into `master`. If you want to update a PR that is already created, then you simply update the source branch for that PR. For example, if you add more commits to `my-branch-A`, after you `push` those commits, the PR for that branch will auto update. If you rewrite the branch (with rebase for example), then you need to force push that branch. – TTT Jan 13 '22 at 21:54
  • Hi @TTT sorry if it seems ignorant to you. I am struggling with git and couldn't understand what you have described. basically I am trying to create a new branch from base/master and then creating a PR for that new branch. but I am facing an issue here that my PR code is being displayed with other commits that I made for other PRs. means right now I just made some changes in HTML code and pushed it in a new branch and then created a new PR but this created PR also displaying the other files that I modified for other already created PRs( some closed and the other one is not closed yet) – djangodeveloper Jan 13 '22 at 22:01
  • @djangodeveloper no worries. If you have commits appearing in multiple PRs, that just means you branched of your other branches rather than starting fresh from `master` each time. Usually you want to avoid that when possible, but sometimes you have dependencies that require that. In your case, you'll probably need to rewrite some of your branches to cherry-pick just the commits you want, or use the advanced `rebase --onto` command like jessehouwing suggested. – TTT Jan 13 '22 at 22:05
  • so what would be the best way to rebase it so that I can make my new PR separate from other PRs? – djangodeveloper Jan 13 '22 at 22:08

1 Answers1

4

To explain what is happening:

This is where you start:

* ----- * ----- *
                   origin/main

You make your first commits and end up in this situation:

* ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- *
                   origin/main.                                           Feature A

Now you create a pull request and are ready for the next feature.

If you keep working on this branch, the new PR will contain all the changes of Feature A + everything you commit after:

* ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- *
                   origin/main.                                           Feature A.          FB

Instead you should make sure you create a branch for FB from origin/main again. There are many ways to do that:

git branch fb
git checkout fb
git reset origin/main --hard

Or do all of that in a single command:

git checkout -b FB origin/main

That way you end up with this after adding a few commits to FB;

                          * ----- * ----- * ----- * FB
                         /
* ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * ----- * 
                   origin/main.                                           Feature A

To fix your current predicament you can rebase FA...FB onto origin/main or create a new branch and cherrypick the commits after FA into your new branch.

git rebase --onto origin/main fa fb

Or you can do an interactive rebase where you drop the commits between origin/main...FA.

git checkout -b fbfixed origin/main
git cherry-pick fa..fb

See also:

Final trick would be to do an interactive rebase of FB:

git rebase -i origin/main..fb

This should show a list of commits between origin/main and FB. Mark all commits after origin/main up to and including FA as drop then perform the rebase:

k 12344 FB
k 27362
k 37383
d nsnsnsn FA
d hshscd
d rnsees
d rhsnsn
d snrrirh
d rifitbrn
d usbrve

This will drop the commits of FA from branch FB. It will leave the original FA in tact.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • so what I think after reading your answer is that I am going to delete the new branch and then reset the main branch hard and then create a new branch and commit my changes to create a PR. is it a professional way? and thanks a lot for your great effort – djangodeveloper Jan 13 '22 at 22:26
  • by copying commits means all commits that I made for different PRs? or the lates one? – djangodeveloper Jan 13 '22 at 22:34
  • can I show you my commits screenshot? and now I am getting your point after reading your edited answer – djangodeveloper Jan 13 '22 at 22:39