Bash script - Split commits for specific folder modifications

Hello,

I want to create a bash script for manipulating local git history.

The goal is to modify current branch work (in all commits differing from main branch) and split modifications from a specific folder in a different commit.

Imagine a repo :
./README.md
./src/mycode.php
./translations/platform-en_devel.xliff
./translations/platform-en.xliff
./translations/platform-fr.xliff

The developer create a new feature branch from main branch

git checkout -b new-feature

He will modify ./src/mycode.php in commit “c1”

Then modify ./src/mycode.php and ./translations/platform-en_devel.xliff in commit “c2”

And modify again ./src/mycode.php in commit “c3”

I need to get all modifications made in translation folder and commit them on main branch

I think of a creating a bash script who will :

  • Rebase feature branch from main
  • Extract only modifications made to translations folder
  • Commit this modifications directly to main branch
  • Remove modifications from translations folder in current feature branch

Another approach I see is

  • Rebase feature branch from main
  • Extract only modifications made to translations folder
  • Create a separated commit with translations modifications on current feature branch
  • Cherry-pick this commit on main branch
  • Rebase feature branch from main

I have to check github hooks maybe this can help me.

Is it possible ? Has anyone seen this type of script that I could build on?

I’ve done things like this in various ways…

You can do something like:

dest=main
branch=new-feature
git checkout $dest
for directory in translations src .; do 
  git diff $dest..$branch -- $directory | patch -p1
  git commit -m "$directory: $branch" $directory
done

(You’ll want to read about .. to make sure it’s doing what you want.)

Note: You need to be careful about additions and removals, the above will do the wrong thing (and mode changes for that matter).

You could also play w/ git checkout + git reset instead of git diff if you’re being adventurous.

1 Like

Big thanks for the moment I’m using this :

branch=trans-test
repo=origin
dest=testmain

git checkout $dest
for directory in "folder1" "folder2"; do
  git diff $dest..$branch -- $directory | patch -p1
  git add $directory
done
git commit -m "Extraction done: $branch"

git checkout $branch
git rebase -Xtheirs origin $dest
#git push --force $repo $branch