0

I need to edit commit message. There are already a few more commits on top of it. Is there a way just edit a particular commit message?

Tried to rebase and reword the commit message. But it won't allows me to push.

Complaining -

remote: To prevent you from losing history, non-fast-forward updates were rejected

asked Mar 9, 2017 at 20:22

2 Answers 2

1

You can't safely do what you are trying to do.

I'll explain how you can push your changes, but if you do everyone else using that repo will have to take additional steps to recover. (See the git rebase documentation for further explanation; generally you shouldn't rebase commits that have been pushed to a remote.)

So first, why is that?

It has to do with how git stores and addresses content, and detects changes. Every commit has a unique ID which is a SHA1 hash of the commit. Say you start with this:

A --- B --- C --- D <--(master)(origin/master)

If you change the commit message on B, the calculated hash must change - so as far as git is concerned, you have a new commit (say B') very similar to the old one.

A --- B --- C --- D <--(master)(origin/master)
 \
 B'

Well, the data for C includes its parent hash (which is B, not B'), so to have a commit whose parent is B' means you have to replace C with a new commit C'. And this will continue all the way to HEAD.

A --- B --- C --- D <--(origin/master)
 \
 B' --- C' --- D' <--(master)

Now git doesn't by default want to let you move origin/master form D to D' because it will mess up assumptions made in any other repos that have cloned from origin.

You can force it to let you push your change anyway by saying

git push -f

but if you do:

1) You will need to double check afterwards that nobody else committed anything after D to master. If they did you'll have to rebase those changes onto D'

2) Everyone else will have to take extra steps to resync their branches with origin, and will have to rebase any local changes they had planned to push to master as well. (If they instead force their push, then they'll move the remote ref back to the old line - restoring the original commit message at B - and you'll be in a broken state. You and anyone who correctly switched to D' will potentially lose changes. This can go back and forth quite messily if allowed to do so; so you need to make sure the entire team is on board with the correct way to recover from the upstream rebase.)

If it's worth all that to change the message, that's how you do it.

answered Mar 9, 2017 at 20:44
Sign up to request clarification or add additional context in comments.

Comments

0

Once you push a commit to a remote repository, it's a bad idea to ever edit or delete that commit, as it will mess up anyone who's pulled from that remote before. If you insist on doing this anyway, git push -f will override this check (unless the remote repository is set up to disallow this).

answered Mar 9, 2017 at 20:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.