Changing git commit message after push (given that no one pulled from remote)
I have made a git commit and subsequent push. I would like to change the commit message. If I understand correctly, this is not advisable because someone might have pulled from the remote repository before I make such changes. What if I know that no one has pulled?
Is there a way to do this?
8 Solutions collect form web for “Changing git commit message after push (given that no one pulled from remote)”
If it is the most recent commit, you can simply do this:
git commit --amend
This brings up the editor with the last commit message and lets you edit the message. (You can use
-m if you want to wipe out the old message and use a new one.)
And then when you push, do this:
git push --force-with-lease <repository> <branch>
Or you can use “+”:
git push <repository> +<branch>
Or you can use
git push --force <repository> <branch>
Be careful when using these commands.
If someone else pushed changes to the same branch, you probably want to avoid destroying those changes. The
--force-with-leaseoption is the safest, because it will abort if there are any upstream changes (
If you don’t specify the branch explicitly, Git will use the default push settings. If your default push setting is “matching”, then you may destroy changes on several branches at the same time.
Pulling / fetching afterwards
Anyone who already pulled will now get an error message, and they will need to update (assuming they aren’t making any changes themselves) by doing something like this:
git fetch origin git reset --hard origin/master # Loses local commits
Be careful when using
reset --hard. If you have changes to the branch, those changes will be destroyed.
A note about modifying history
The destroyed data is really just the old commit message, but
--force doesn’t know that, and will happily delete other data too. So think of
--force as “I want to destroy data, and I know for sure what data is being destroyed.” But when the destroyed data is committed, you can often recover old commits from the reflog—the data is actually orphaned instead of destroyed (although orphaned commits are periodically deleted).
If you don’t think you’re destroying data, then stay away from
--force… bad things might happen.
This is why
--force-with-lease is somewhat safer.
Just say :
git commit --amend -m "New commit message"
git push --force origin <BRANCH-NAME>
Might be late to the party, here is an answer I do not see here.
git rebase -i HEAD~n to do interactive rebase for all those commits affected.
git will pop up an editor to handle those commits, notice this command:
# r, reword = use commit, but edit the commit message, that is exactly we need.
r for those commits that you want to update the msg.
Save and close the editor.
Step3: in the following commit files, update the commit msg as you like
Step4: after all commits msgs are updated. you might want to do
git push -f to update the remote.
Use these two step in console :
git commit --amend -m "new commit message"
git push -f
It should be noted that if you use
push --force with mutiple refs, they will ALL be modified as a result. Make sure to pay attention to where your git repo is configured to push to. Fortunately there is a way to safeguard the process slightly, by specifying a single branch to update. Read from the git man pages:
Note that –force applies to all the refs that are pushed, hence using
it with push.default set to matching or with multiple push
destinations configured with remote.*.push may overwrite refs other
than the current branch (including local refs that are strictly behind
their remote counterpart). To force a push to only one branch, use a +
in front of the refspec to push (e.g git push origin +master to force
a push to the master branch).
If you want to modify an older commit, not the last one, you will need to use
rebase command as explained in here,Github help page , on the Amending the message of older or multiple commit messages section
git commit --amend -m "New and correct message"
git push origin --force
This works for me pretty fine,
git checkout origin/branchname
if you’re already in branch then it’s better to do pull or rebase
git -c core.quotepath=false fetch origin --progress --prune
Later you can simply use
git commit --amend -m "Your message here"
or if you like to open text-editor then use
git commit --amend
I will prefer using text-editor if you have many comments. You can set your preferred text-editor with command
git config --global core.editor your_preffered_editor_here
Anyway, when your are done changing the commit message, save it and exit
and then run
git push --force
And you’re done