Changing git commit message after push (given that no one pulled from remote)
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?
See answer to question "How do I edit an incorrect commit message in git (I've pushed)?" stackoverflow.com/a/457396/444639
– Mike Rylander
Mar 14 '13 at 15:08
Declaring it - Google Question of git commit Rank no 1 !
– Manish Shrivastava
May 6 '16 at 7:56
help.github.com/articles/changing-a-commit-message
– Muk
May 17 '16 at 10:47
If you amend the HEAD commit and push usually (without --force) then surprisingly it does not fail. HEAD commit message is updated with the changed commit Id. It means other commit IDs except HEAD remains intact. I noticed this behavior with git 2.8.1 version.
– Rahul
Sep 13 '17 at 9:00
11 Answers
11
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.)
-m
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 --force
:
--force
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-lease
option is the safest, because it will abort if there are any upstream changes (
--force-with-lease
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.
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.
reset --hard
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).
--force
--force
If you don't think you're destroying data, then stay away from --force
... bad things might happen.
--force
This is why --force-with-lease
is somewhat safer.
--force-with-lease
Be careful with that "fix", as if they have any local, unpushed commits they will be "lost" (lost truly meaning orphaned, but recovering them is non-obvious).
– Andrew Marshall
Jan 24 '12 at 2:07
you probably want to specify the branch name when you push --force, otherwise you may push more than you expected.
– user693960
Mar 12 '13 at 0:25
@user693960: Git will only push what you configure it to push.
– Dietrich Epp
Mar 12 '13 at 2:37
@Leniel Macaferi: Using
-m
here doesn't let you edit the old message, it only lets you specify the new message.– Dietrich Epp
Oct 29 '13 at 0:38
-m
Simply
git push --force
without <repository> and <branch> options works too, if you have your upstream set up.– ahnbizcad
Apr 29 '14 at 9:16
git push --force
Just say :
git commit --amend -m "New commit message"
and then
git push --force
In my case
git push origin <BRANCH-NAME>
did not work, I had to use git push --force
as explained in the accepted answer.– Gabriel
Feb 21 '14 at 19:06
git push origin <BRANCH-NAME>
git push --force
this doesn't work for me. gotta us
git push --force
, or else the push doesn't go through.– ahnbizcad
Apr 29 '14 at 9:17
git push --force
@ahnbizcad, it should work. Just make sure that the branch name is correct.
– William
Oct 13 '16 at 23:32
I cheer for the simplicity in your explanation ! I use it more than often
– Vasikos
Apr 19 '17 at 7:01
I successfully applied these command only after temporarily "unprotect" my branch, which happened on my GitLab-hosted project. If you have this issue, before applying these commands, please refer to this stackoverflow.com/a/32267118/1423345 to "unprotect" the branch, and you can "protect" it again after done amending the commit message :)
– John
Jul 3 '18 at 14:33
Might be late to the party, here is an answer I do not see here.
Step1: git rebase -i HEAD~n
to do interactive rebase for the last n
commits affected.
git rebase -i HEAD~n
n
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, reword = use commit, but edit the commit message
Step2: change pick
to r
for those commits that you want to update the msg.
Save and close the editor.
pick
r
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.
git push -f
This should be accepted answer as it gives possibility to change other commits than most recent commit, unlike accepted answer. You saved my day. Thank you!
– xZero
Dec 3 '17 at 17:10
I get error invalid upstream HEAD~n
– Čamo
Dec 13 '17 at 15:31
Choose n=3 for the last 3 commits:
git rebase -i HEAD~3
– HeikoS
Mar 8 '18 at 15:10
git rebase -i HEAD~3
If you edit your rebase 'plan' yet it doesn't begin the process of letting you rename the files, run
git rebase --continue
. And if you want to change the text editor used for the interactive session (e.g. from the default vi
to nano
), run GIT_EDITOR=nano git rebase -i HEAD~n
.– Jamie Birch
Nov 20 '18 at 16:39
git rebase --continue
vi
nano
GIT_EDITOR=nano git rebase -i HEAD~n
Use these two step in console :
git commit --amend -m "new commit message"
and then
git push -f
Done :)
You are a Jedi. Thanks, nothing else worked!
– lopezdp
Jun 20 '17 at 13:07
Simple and working. I think this one should be the chosen answer
– CarlosCarucce
Jun 21 '17 at 16:25
Thank for your valuable comments :)
– Abdul Rizwan
Jul 25 '17 at 10:51
Thx, Is that steps only for amending the last comment or can it be used for older comments too?
– Jay
Aug 2 '17 at 13:43
nicely done, thx @AbdulRizwan, upvoted for clean and clear answer.
– Ravi Vaniya
Jul 21 '18 at 11:26
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:
push --force
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).
Very important note.
– peterh
Jan 19 '17 at 16:14
none of the force answers work for me, because I don't have ForcePush permissions on the server. Instead, I want to perform a commit which changes a previous commit message. I could write "commit message changed" to that commit's comment section.
– nurettin
Sep 12 '17 at 6:20
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
rebase
Command 1.
git commit --amend -m "New and correct message"
Then,
Command 2.
git push origin --force
git commit --amend
then edit then change the message in the current window. After that do
git push --force-with-lease
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 pull
or
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
Another option is to create an additional "errata commit" (and push) which references the commit object that contains the error -- the new errata commit also provides the correction. An errata commit is a commit with no substantive code changes but an important commit message -- for example, add one space character to your readme file and commit that change with the important commit message, or use the git option --allow-empty
. It's certainly easier and safer than rebasing, it doesn't modify true history, and it keeps the branch tree clean (using amend
is also a good choice if you are correcting the most recent commit, but an errata commit may be a good choice for older commits). This type of thing so rarely happens that simply documenting the mistake is good enough. In the future, if you need to search through a git log for a feature keyword, the original (erroneous) commit may not appear because the wrong keyword was used in that original commit (the original typo) -- however, the keyword will appear in the errata commit which will then point you to the original commit that had the typo. Here's an example:
--allow-empty
amend
rob, this looks promising. can you show the commands needed to do an "errata commit". only this post shows up in google on these terms.
– Jim
Aug 10 '18 at 13:05
An “errata commit” is simply a normal commit with a message that references the previous erroneous commit, documenting and providing a correction for the previous mistake.
git commit -m “fixed feature A”
(Let’s assume git gives this a commit ID of e3ab7312... ... (later you realize your message was incorrect so now make an inconsequential change to a file like adding a space to the readme file, or use the —allow-empty
git option) ... git commit -m “Errata commit for previous commit e3ab7312... original message should have been ‘fixed feature *B*’
‘’’– rob_7cc
Aug 11 '18 at 22:58
git commit -m “fixed feature A”
—allow-empty
git commit -m “Errata commit for previous commit e3ab7312... original message should have been ‘fixed feature *B*’
...if you later need to search the git log for references to “feature B”, the errata commit will show up, but the errata commit message contains a reference to the original commit ID which provides full traceability. BTW the term “errata commit” is nothing special (there is no “errata” command nor option in git)...it is just my terminology for a normal commit that provides a correction to a previous commit that had an error/typo.
– rob_7cc
Aug 11 '18 at 23:02
rob, that worked great. I was able to add a new empty commit with the correct description, that points to the original commit, by using the SHA. now, both are shown in my 'git chain' for the modules. thanks!
– Jim
Aug 13 '18 at 12:48
additional information for same problem if you are using bitbucket pipeline
edit your message
git commit --amend
push to the sever
git push --force <repository> <branch>
then add --force to your push command on the pipeline
git ftp push --force
This will delete your previous commit(s) and push your current one.
remove the --force after first push
i tried it on bitbucket pipeline and its working fine
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What have you tried? Assuming you know how to change the commit message already, and then try and push, Git will tell you what you need to do to make it happen.
– Andrew Marshall
Jan 24 '12 at 1:59