Search code examples
pythongitgitpython

Edit a commit with gitpython


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?


Solution

  • 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.