gitgit-rebasegit-interactive-rebase

git: automatic line-specific fixup


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.


Solution

  • 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