Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Lets say you're in the terminal, currently sitting at the root of a git repo.

If you've got GitPython installed, you can do this to get the last committed commit, or rather, the commit that HEAD points to:

>>> import git
>>> r = git.Repo('.')
>>> c = r.head.commit
>>> c
<git.Commit "62e71e8210d0d0275b1f5845bf3033a7bfa3ed73">
>>> c.message
u'old message'
>>> c.message = "new message"
>>> c.message
'new message'

But if you exit python and drop into git log here, you'll see the commit message remains untouched. How do I edit commits with this library?

share|improve this question
 
I'm making some progress off this: stackoverflow.com/questions/11987914/… still trying to get the api cleaned up though. This feels like resorting to subprocess, but at least there's sort of a hook for filter-branch in this library. –  Conrad.Dean Jan 17 at 3:37

1 Answer

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 changed published history.

edit: 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.

share|improve this answer
 
I'm totally okay with breaking the sha-chain in every commit to head. –  Conrad.Dean Jan 15 at 14:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.