Git edit - improving --fixup
14 Jun 2019I’m a big fan of attempting to make my git history useful.
This often means reworking my commits so that work is done in a sensible order and in small logical units.
There are many ways to rework your history in git but one I’ve used a lot for the past year is the --fixup
flag when committing.
This has often led to annoying problems, in this post I’ll look at:
Using --fixup
The --fixup
flag is a bit like the --amend
flag except you can specify the commit you want to squash your work into rather than it just being squashed into the most recent commit.
When given the following history:
If I stage some changes and run git commit --amend
then the last commit Add git-edit post
will be rewritten to include my staged changes.
If instead I want my changes to go into a commit that is not the most recent then I can use the following command git commit --fixup 4564152
.
In this example I would end up with the following
Once I have this I can run git rebase --interactive 9051216
and accept the todo list to have git squash the new changes into the target commit.
Problems with --fixup
I love --fixup
but it doesn’t quite do so well when you have heavy churn in your files (possibly a sign that the work could be reordered).
The issue you run into a lot is that when you make your changes to fix up a commit you are potentially looking at a source file that has been changed in multiple steps.
Any changes you make may only make sense in the current state of the file, this can lead to conflicts when targeting an earlier revision of the file.
A solution git-edit
We can avoid the issues above by:
- rolling back our repo to the target commit
- applying the changes we want
- reapply the remaining commits
This is achievable by using our friend git rebase --interactive <commitish>
.
Going back to our earlier example
If I want to make changes to the commit 4564152
then I can run git rebase --interactive 4564152~1
to be presented with a todo-list in an editor.
I can now change the first pick
line (which is for the commit we passed in) to say edit
instead e.g.
After saving this file I’ll be placed on the commit 4564152
where I can make my edits, add changes and finish up by calling git rebase --continue
.
The above is fine but it’s a little manual so it would be nice if we could automate away some of these steps. Let’s see how we can do that.
Creating your own git commands
The first piece of the puzzle is creating a git command.
I’d like the command to work as if it was a git subcommand by typing git edit <commitish>
.
This is actually not too difficult to do - add an executable command somewhere on your path with the name git-edit
.
Now when I call git edit <commitish>
git will invoke my script.
Let’s test it
~/bin/git-edit
zsh
GIT_SEQUENCE_EDITOR
The next challenge is how do I automate editing the todo list that you get when you call git edit
?
Again git has a nice seam for us to work with - we can set the environment variable for GIT_SEQUENCE_EDITOR
to our own command.
Our command will be invoked with one argument, the path to the todo-list file.
With this knowledge we can write a script that will:
- read a todo-list file
- update the correct todo-list item to be
edit
instead ofpick
- save the file
Let’s do this in ruby:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env ruby
if ENV['GIT_SEQUENCE_EDITOR'] != __FILE__
exec "GIT_SEQUENCE_EDITOR='#{__FILE__}' git rebase --interactive #{ARGV.first}~1"
end
todo_list = ARGV.first
lines = File.readlines(todo_list)
.lazy
.take_while { |line| line != "\n" }
.map { |line| line.gsub("fixup", "pick") }
.to_a
lines[0] = lines[0].gsub("pick", "edit")
File.write(todo_list, lines.join)
- Lines 3-5 allow me to use a single script to handle editing the todo-list and creating my personal
git edit
subcommand- When I call
git edit <commitish>
theGIT_SEQUENCE_EDITOR
will not be equal to this executable script’s name so it will execute the interactive rebase command on line 4 and set theGIT_SEQUENCE_EDITOR
to the current file
- When I call
- Lines 9-13 handle reading in the todo-list, stripping all comments from the file and replacing any
fixup
lines withpick
- It’s important for my usage to leave the
fixup
lines in place until I choose to squash things later
- It’s important for my usage to leave the
- Line 15 edits the line for the commit we passed to the original command to put it in
edit
mode - Line 17 finishes the process off by writing the todo-list back to disk
With all of this in place we can now run a git subcommand that automates the process of rewinding us back to a particular commit to allow us to make changes and then reapply all subsequent commits in order.
Conclusion
There’s a few handy things to take away here around adding your own scripts to git. Knowing that it’s actually not too painful to add quite complex automation means that I’ll really pay attention more to manual tasks that I repeat and look at automating these tasks in future.