Git hints: using apply

2022-08-15

Git is very powerful tool, and you can discover new techniques when working with it forever. Today I found the new one for me.

The problem

Given

o-o-o-o [master]
      ╰-o-o-o [branch-x]

Need

Create the new branch (branch-y), and have changes of branch-x on it. u stands for uncommited changes.

o-o-o-o [master]
      ╰-o-o-o [branch-x]
      ╰-u [branch-y]

Solution

git checkout master
git checkout -b branch-y
git diff master branch-x | git apply

Solution check

git checkout branch-y
[[ `git diff branch-x | wc -l` == 0 ]] && echo "OK" || echo "FAIL"

When used

Sometimes you have to create the copy of the branch with all commits squashed into the one.