Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: split commit in other branch

Tags:

git

I have a following situation:

root -- A -- D ------------- H ------master
    \          \              \
     \          \              \
      - B -- C -- E -- F -- G -- I -- J -- dev

Commit 'F' contains an bugfix that at first seemed to be unrelated to master branch, but after few weeks it proved to be important. Everything was already commited and pushed.

But another problem is that 'F' commit contains a few changes and only one of them is relevant to master. So what I would want to is:

  • Split F into 2 separate commits (one for relevant change [F1], another for other [F2])
  • Merge F1 with master

I don't need to change dev branch history.

Can I do anything besides manual diff + patch?

I imagine something like

root -- A -- D ------------- H ---K--master
    \          \      /-------\--/    
     \          \  /-F1-F2-\   \-----\        
      - B -- C -- E ------- F -- G -- I -- J -- dev

(sorry for sloppy line art)

like image 203
user986654 Avatar asked Feb 23 '23 15:02

user986654


1 Answers

I would do the following: first cherry-pick commit F with --no-commit to your master branch, then stage the changes you need in master, drop everything else. I know it does not sound like the perfect solution, but when merging F to master, you would also get all ancestor commits of F (from dev branch: E C B):

git checkout master
git stash
git cherry-pick --no-commit F
git reset && git add -p
# alternatively reset already staged changes:
git reset -p
git commit
git reset --hard
git stash pop
like image 138
knittl Avatar answered Feb 26 '23 08:02

knittl