7

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?

Conrad.Dean
  • 4,341
  • 3
  • 32
  • 41
  • I'm making some progress off this: http://stackoverflow.com/questions/11987914/how-do-i-reword-the-very-first-git-commit-message 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 '13 at 03:37

1 Answers1

6

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.

0 _
  • 10,524
  • 11
  • 77
  • 109
Eevee
  • 47,412
  • 11
  • 95
  • 127
  • 1
    This doesn't work for me. This just creates a new blank commit. The new commit only has the commit message, but none of the files of the original. It even resets the author. –  Jun 12 '18 at 19:17