0

Having the following trivial commits

# define function in one file
echo "function foo () {}" > a
git add a
git commit -m "1st commit"

# call the function in different file
echo "foo()" > b
git add b
git commit -m "2nd commit"

# rename foo to bar in both files
echo "function bar () {}" > a
echo "bar()" > b
git commit -am "fixup"

is it possible to automatically split the fixup commit to update both previous commits?

I'm thinking that if the fixup commit only changes lines that were changed in the original commits, it should indeed be possible to precisely algorithmically split the fixup commit. Is that the case? If so, does git support it?

I know this can be done with multiple rebases and manually splitting the fixup commit, but that is not what I'm after.

Clarification: Imagine working on a feature branch. You have numerous commits. Upon code review it was found that for example method foo should rather be named method bar. Now before pushing to the upstream I'd rather not just change all instances of it add a new commit, but split the last commit and apply it's parts to the appropriate prior commits.

Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99
  • Why not using `git mv` to rename one the files, and commit, then `git mv` to rename the other, and commit? – VonC Oct 07 '14 at 11:21
  • I'm not renaming files, I'm renaming a function and that is just a simplified example (it might be arbitrary text). – Mikulas Dite Oct 07 '14 at 11:24

1 Answers1

1

If I interpret your question to instead be "How can I rename a function in all commits it appears in" then the following works (tested against the two sample commits you provided, but not intended to be fully functional).

When working on feature branch you'd need to pass that in to limit how many commits filter-branch tried to process.

git filter-branch --tree-filter 'git grep --name-only foo | xargs sed -i "s/foo/bar/g"'
Andrew C
  • 13,845
  • 6
  • 50
  • 57
  • Thanks, this would actually work for this simplified example, but imagine the real world example: having a fixup commit that might change more than just a sed would. I'm having trouble articulating the question properly it seems (which is kind of the biggest problem here). – Mikulas Dite Oct 07 '14 at 15:23
  • I can only conjecture without specifics. Generally speaking I would no say (if you can't describe how the patch should be split and how it should be applied there is no way to program that), although you can simplify things a lot by using `git commit --fixup` and `rebase.autosquash true` – Andrew C Oct 07 '14 at 15:27
  • Right I guess I will have to come up with a counterexample where it couldn't work automatically to prove myself wrong. – Mikulas Dite Oct 07 '14 at 16:51