Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to commit some changes from branches and keep the rest?

Tags:

git

I have written a blueprint for a Machine Learning pipeline that can be reused for many projects. When I encounter a new project, I will create a branch and work on the branch. Often times, when working on the branch, I discover the following:

  1. Some code changes/improvements that was discovered on the branch, and should be merged to master.
  2. Some code changes that should only happen on the branch since every single project will have its nuances, but the template should be more or less the same as the master.

I am having trouble in combining point 1 and 2. Is there a way to merge some changes from branch to main, it seems quite tricky to me as this is a continuous process.

like image 695
ilovewt Avatar asked Nov 06 '22 23:11

ilovewt


1 Answers

If you are the only one working on that branch, you should do a git rebase -i (interactive rebase) in order to re-order your commits, putting first the one that should be merged to master, and leaving as most recent the one for branch only.

git switch myBranch
git rebase -i master

# reorder to get:

m--m--m
       \
        M--M--M1--b--b--b (myBranch)

Once that is done, create a branch at M1, and merge that branch to master

git switch -c tmp M1
git switch master
git merge b1

m--m--m--M--M--M1 (master)
                 \
                  b--b--b (myBranch)

Finally, force push your branch, since the rebase has rewritten its history

git switch myBranch
git push --force

(that is easiest done, again, if you are the only one working on that branch)

like image 88
VonC Avatar answered Nov 15 '22 05:11

VonC