Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a Git commit into two if I can come up with the intermediate commit?

Tags:

git

A change was applied to a large codebase, then a formatter was applied, touching nearly every file.

A --------- B

A single commit B combining those two actions lead to a gigantic diff that is a pain to review, and also difficult to split up manually with an interactive rebase and picking out hunks. However, I could easily contrive an intermediate commit I by checking out A and running the code formatter on that.

A --------- B
 \ 
  \
   -- I

Given that I just made the commit I, how can I easily generate a commit B' that ends up with the exact same tree content as B?

A --------- B
 \            
  \           
   -- I --- B' # (has exact same contents as B)

If I naively do

git checkout B
git rebase I

I end up with a ton of conflicts. I don't want to have to be clever at all, I just want to end up at the exact same directory as B, with the assumption that the I..B diff will be dramatically smaller than A..B.

like image 489
Nick T Avatar asked Jan 26 '23 10:01

Nick T


1 Answers

Ok, if you already have I and B, you can do this:

git checkout --detach B
git reset --soft I # move branch pointer (HEAD pointer, in this case) to I, set all differences in index ready to be committed
git commit -m "blah blah"

Now you can point whatever branch you want where you are if you like the results:

git branch -f blah
git checkout blah
like image 57
eftshift0 Avatar answered Jan 28 '23 09:01

eftshift0