How to keep a GitHub PR's code separate from other branch's PR code?
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 offeature/b
anda
-
feature/b
won't contain the changes offeature/a
Solution 1:
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:
- https://stackoverflow.com/a/2474371/736079
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.