4

I recently learned about git commit --fixup which marks your commit as fixup of an earlier commit and automatically arranges them next to each other during an interactive rebase.

How can I do that automatically and separately for multiple lines?

Take the following example. My git history looks like this:

c5069d5 This commit adds method a() to file A.cpp
ac5db87 This commit does something in file B.cpp
733e2ff This commit adds method b() to file A.cpp
ac5db87 This commit does something else in file B.cpp

And in my working tree I have changed some lines in method a() of file A.cpp and b() (and maybe I added a method c() ). Please note that I only talk about functions here to make the communication easier - of course, git is unaware of the programming language.

Is it possible to automatically generate two separate fixup commits from my two separate changes in the working tree, i.e.:

c5069d5 This commit adds method a() to file A.cpp
a5559d5 fixup! This commit adds method a() to file A.cpp
ac5db87 This commit does something in file B.cpp
733e2ff This commit adds method b() to file A.cpp
a53aad5 fixup! This commit adds method b() to file A.cpp
ac5db87 This commit does something else in file B.cpp

(and now the working tree contains the changes in A.cpp which added c()) It would be great if this worked with multiple files in the working tree (and not only the single A.cpp as in this example)

Update, alternative wording:

git-blame shows me the last commit for each line. If I now change a line, I want to fixup the commit git-blame previously showed me for this line.

PhilLab
  • 4,777
  • 1
  • 25
  • 77
  • For this to work git would need to understand the syntax of your programming language. But git only manages textual diffs. – Johannes Thorn Dec 02 '15 at 17:08
  • @JohannesThorn I was only speaking about functions ``a()``, ``b()`` and ``c()`` to make the example more lively (as a placeholder for "I changed some lines"). What I want should be theoretically possible: git-blame shows me the last commit for each line. If I now change a line, I want to fixup the commit git-blame showed me for this line. Is this a better explanation? – PhilLab Dec 03 '15 at 07:48
  • yes that explanation is much better but I don't know how to achieve it. – Johannes Thorn Dec 03 '15 at 08:15
  • 1
    Just looking for the same thing… – Joachim Breitner Nov 16 '16 at 14:19
  • 1
    This tool claims to do a lot of what I want https://github.com/keis/git-fixup haven't given it a whirl yet – jberryman Dec 11 '17 at 22:48
  • Another script can be found here https://github.com/georgebrock/git-fixup, I did not have much luck with it though. Most of my changes could not be applied and it aborts on any error. – Zitrax May 25 '18 at 11:02

1 Answers1

0

As suggested in an answer by jberrymann, https://github.com/keis/git-fixup does exactly that. Using the command git fixup -c offers you a list of possible fixup targets

PhilLab
  • 4,777
  • 1
  • 25
  • 77