Interesting project... in the hopes of maybe nerd-sniping you (or having someone tell me where it already exists), I'll tell you the story I'd really like a solution to but can never seem to find time to build:
I have 5 un-pushed commits. I missed a 1-line change to that first one. I add that one line change to staging (gitui/lazygit/whatever), then I want to `git commit --fixup=COMMIT`. Finding that COMMIT is always a manual process for me for two reasons.
First I have to find the commit to which it applies. This commit is almost always the commit that changed the same line, or added lines around it, or something similar. I guess you could say it's the commit with the closest proximity to the change I'm adding.
Second I have to find the hash of that commit. If I've since done something like an autosquash rebase then it'll have changed. I generally end up using the mouse to do a copy/paste at this point.
Why not skip the fixup commits and just rebase right then, you might ask. Well the commits are signed, and signing requires tapping my yubikey for each commit after that.
I would really like to be able to do something like `git commit --I'm-an-idiot-and-missed-this-so-please-apply-a-fixup-to-the-proper-commit`, and have it do the figuring out for me.
I usually solve this by commiting my fixup in a dummy commit, then do a rebase -i HEAD~n where n is the number of commits I want to look back at. Then during the rebase I just move my commit below the one that seem the most appropriate and mark it for squash. Execute the rebase. Done
Another way to handle this if you haven't made the change yet is to do a `git rebase -i head~4` (or however far back) and mark the place where the change needs to happen as "edit". Make your change, add the file, then `git rebase --continue`.
If you've already made the change but haven't committed it, you can stash it before doing the rebase, then pop the stash while editing that commit.
git add file1
git commit -m "Fix some bug in file1"
git add file2
git commit -m "Add a feature flag for file1"
# Oh no, first commit was borked, need to fix it
git add file1
git absorb
# this will now automatically create one or more fixup commits
git rebase -i --autosquash $(git merge-base master)
# voila, tidy history and each commit works
This was my bugbear for a long time too, and then I found fzf-git.sh (actually its PowerShell equivalent, PSFzf).
fzf (absolutely incredible tool with many uses) gives you fuzzy searching of anything you care to name, and fzf-git combines this with shell key bindings to let you pop up a fuzzy-searchable list of branches/commits/tags/whatever while typing your fixup command, then paste the object you select into your in-progress command line.
No more counting commits in log output to know how many ^ to put after HEAD, or copying out segments of commit SHAs!
>> git absorb will automatically identify which commits are safe to modify, and which staged changes belong to each of those commits. It will then write fixup! commits for each of those changes.
Lazygit allows you to easily « add » your stated changes to any commit (doing a rebase under the hood). Stage stuff, find the commit (the UI helps to quickly find the right commit) and press « a » for amend.
It’s not quite as automatic as you describe, but it’s super quick if you vaguely know what commit you want to amend and there’s no magic happening: just a good ui
Ty. I've seen similar in other tools, and I agree that it's super quick... unless you sign your commits with a key that requires a physical touch of a yubikey. Then it's <tap> <wait> <tap> <wait> for every commit that comes after the one you just modified.
I do this a lot in lazygit, or at least something that sounds similar: I’ve got some changes, I stage them by pressing spacebar (or hitting enter and adding lines in a more granular way), and then switch to the Commits tab, arrow down to the one I want to add this change to (all the unpushed ones are red) and hit A to amend.
I can’t solve the whole problem for you, but you can `git commit --fixup HEAD~4` if you know it’s 5 commits back. No need to figure out the hash. `HEAD^^^^` would also work — sometimes if I have to repeatedly fixup the same commit, adding an additional ^ to a previous command is easiest.
Lazygit maintainer here: I've found myself in your shoes quite a bit (without the commit signing part) and a few weeks ago I put up a draft PR where if a file is selected, it highlights the commits that touch that file. Typically you want to amend the most recent commit that changed the file and typically that commit is visible without needing to scroll. But I haven't spent much time thinking about what the ideal UX is, how to activate it, etc.
I want this too. We just shipped "amend from status" allowing you to push staged files into the HEAD commit. It's only a small step to add selection of the commit to amend.
Feel free to make a PR. If not, I'll probably queue it up to work on at some point.
I have 5 un-pushed commits. I missed a 1-line change to that first one. I add that one line change to staging (gitui/lazygit/whatever), then I want to `git commit --fixup=COMMIT`. Finding that COMMIT is always a manual process for me for two reasons.
First I have to find the commit to which it applies. This commit is almost always the commit that changed the same line, or added lines around it, or something similar. I guess you could say it's the commit with the closest proximity to the change I'm adding.
Second I have to find the hash of that commit. If I've since done something like an autosquash rebase then it'll have changed. I generally end up using the mouse to do a copy/paste at this point.
Why not skip the fixup commits and just rebase right then, you might ask. Well the commits are signed, and signing requires tapping my yubikey for each commit after that.
I would really like to be able to do something like `git commit --I'm-an-idiot-and-missed-this-so-please-apply-a-fixup-to-the-proper-commit`, and have it do the figuring out for me.