Home > Blockchain >  How to create a new branch that has only my changes from develop but doesn't reflect any of my
How to create a new branch that has only my changes from develop but doesn't reflect any of my

Time:10-05

Due to how we have automagically handled deploying through our many kubernetees clusters when I'm doing development I'm doing it on my personal branch instead of a branch named after the bug (it's easier to automate deploying and view it on a branch I regularly maintain). I also find myself occasionally doing pointless commits not because I think the code is in a good state but because I need to trigger a new automated deploy.

The net result is I end up with a personal branch with good working code, but with a bunch of ugly/pointless commits cluttering up it's history. I'm wondering if there is a cleaner way to move from my personal branch to the bug branch I'll do the merge request on? I want to transfer all my code changes, but not any of the commit history since I last split off from develop.

Is there a quick/clean way to do this?

CodePudding user response:

You can merge all your changes in a branch as a single commit into another one:

git checkout bugbranch
git merge --squash mybranch
git commit

If you need more control over the commit history, refer to: How do I squash my last N commits together?

CodePudding user response:

Assuming you want to leave the history on branch personal, but copy all the changes over to branch bug as a single commit.

git switch bug
git cherry-pick -n ..personal
git commit

That cherry-pick command takes all the changes on personal that aren't on bug and applies them to bug without (due to -n) committing. Then you can commit them all as a single commit with a new message.

If personal wasn't branched off bug, then you might want to modify the cherry-pick to better control which changes it picks. This takes all changes made on personal since it branched from develop:

git cherry-pick -n develop..personal
  •  Tags:  
  • git
  • Related