Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 730
Conrad.Dean Avatar asked Jan 15 '13 01:01

Conrad.Dean


People also ask

How do I edit an existing commit?

If the commit only exists in your local repository and has not been pushed to GitHub.com, you can amend the commit message with the git commit --amend command. On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter.

How do I edit an existing pushed commit message?

Changing the latest Git commit message If the message to be changed is for the latest commit to the repository, then the following commands are to be executed: git commit --amend -m "New message" git push --force repository-name branch-name.

Can you edit a commit message?

To change the most recent commit message, use the git commit --amend command. To change older or multiple commit messages, use git rebase -i HEAD~N .

Can we edit commit message in GitLab?

Users can edit commit messages through GitLab, removing the need to use git rebase for simple use cases.


1 Answers

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.

like image 134
Eevee Avatar answered Sep 29 '22 20:09

Eevee