You can't edit git commits. That's part of the contract of git
.
You can replace git commits with new ones, though. That's what git commit --amend
does: it backtracks over the last commit, combines the changes with the new ones you've made, and then makes a brand new commit out of the combination. Same thing with git rebase
and anything else that claims to "change" history.
So you'll have to do the same thing here: move the branch pointer back one commit, reuse the same tree object, and commit it with a different message. Keep in mind that you'll screw up anyone else who's seen that commit; never change published history.
I don't know this library at all, but this combination of examples from the docs may do what you want:
branch = r.head.reference
commit = r.head.commit
branch.commit = commit.parents[0]
# This works because the index is unchanged by the `branch.commit`
# edit, but it won't work if you're trying to change a commit on
# another branch -- you can't really do that
new_commit = repo.index.commit(u"new message")
The new commit will have a different hash but should contain the same changes and have the new message. Do this carefully, my bold words are important, changing history is a
nontrivial operation and you should understand what you're doing.
* Alright, well, there are git "notes" which can be edited independently of the commit they're attached to. But that's clearly not what you mean here, and I've yet to see anyone actually use them anyway.
subprocess
, but at least there's sort of a hook forfilter-branch
in this library. – Beer