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?
-
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– Andrew Marshall2012年01月24日 01:59:52 +00:00Commented Jan 24, 2012 at 1:59
-
3See answer to question "How do I edit an incorrect commit message in git (I've pushed)?" stackoverflow.com/a/457396/444639Rylander– Rylander2013年03月14日 15:08:41 +00:00Commented Mar 14, 2013 at 15:08
-
3help.github.com/articles/changing-a-commit-messageMukesh– Mukesh2016年05月17日 10:47:43 +00:00Commented May 17, 2016 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.irsis– irsis2017年09月13日 09:00:38 +00:00Commented Sep 13, 2017 at 9:00
-
Simple and easy for all commits, not only most recent: stackoverflow.com/a/5032614/7705712Edward D. Wilson– Edward D. Wilson2019年02月25日 06:02:56 +00:00Commented Feb 25, 2019 at 6:02
21 Answers 21
Changing history
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.)
Pushing
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:
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.
20 Comments
git push --force without <repository> and <branch> options works too, if you have your upstream set up.<repository>? Is it origin? org/repo? Or just repo?origin.Just say:
git commit --amend -m "New commit message"
and then
git push --force
11 Comments
QUESTION says - the commit is already PUSHED. Amend works for un-pushed commits.remote: To prevent you from losing history, non-fast-forward updates were rejected. remote: Merge the remote changes (e.g. 'git pull') before pushing again. See the 'Note remote: about fast-forwards' section of 'git push --help' for details. ` [remote rejected] master -> master (pre-receive hook declined)`To edit a commit other than the most recent:
Step1: git rebase -i HEAD~n to do interactive rebase for the last n commits affected. (i.e. if you want to change a commit message 3 commits back, do git rebase -i HEAD~3)
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!
Step2: Change pick to r for those commits that you want to update the message. Don't bother changing the commit message here, it will be ignored. You'll do that on the next step. Save and close the editor.
Note that if you edit your rebase 'plan' yet it doesn't begin the process of letting you rename the files, run:
git rebase --continue
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
Step3: Git will pop up another editor for every revision you put r before. Update the commit msg as you like, then save and close the editor.
Step4: After all commits msgs are updated. you might want to do git push -f to update the remote.
12 Comments
git rebase -i HEAD~3git 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.Use these two steps in console:
git commit --amend -m "new commit message"
and then
git push -f
Done :)
3 Comments
Case 1 : Not pushed + most recent commit:
git commit --amend
This will open your $EDITOR and let you change the message. Continue with your usual git push origin master.
Case 2 : Already pushed + most recent commit:
git commit --amend
git push origin master --force
We edit the message like just above. But need to --force the push to update the remote history.
⚠️ But! Force pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.
Case 3 : Not pushed + old commit:
git rebase -i HEAD~X
# X is the number of commits to go back
# Move to the line of your commit, change pick into reword,
# then change your commit message:
git commit --amend
# Finish the rebase with:
git rebase --continue
Rebase opens your history and let you pick what to change. With edit you tell you want to change the message. Git moves you to a new branch to let you --amend the message. git rebase --continue puts you back in your previous branch with the message changed.
Case 4 : Already pushed + old commit:
Edit your message with the same 3 steps process as above (rebase -i, commit --amend, rebase --continue).
Then force push the commit:
git push origin master --force
⚠️ But! Remember re-pushing your commit after changing it will very likely prevent others to sync with the repo, if they already pulled a copy. You should first check with them.
Reference : https://gist.github.com/nepsilon/156387acf9e1e72d48fa35c4fabef0b4#not-pushed--most-recent-commit
3 Comments
git pushing to the destination origin master, and you may not need to specify a destination at all. Generally, you'll be pushing to origin unless you have more than one remote, but the second part is the name of your branch. If you're on branch master, then yes, it's master, but otherwise it might be something else. For many people, git push will already be configured, too.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).
2 Comments
Command 1.
git commit --amend -m "New and correct message"
Then,
Command 2.
git push origin --force
Comments
git commit --amend
then edit and change the message in the current window. After that do
git push --force-with-lease
1 Comment
Instead of rebasing and force pushing the modified branch, it's possible to replace a commit with a different message without affecting the existing commit hashes:
git replace --edit <commit>
The modified references will then have to be pushed and fetched explicitly with:
git push origin 'refs/replace/*'
git fetch origin 'refs/replace/*:refs/replace/*'
This requires Git 2.1 or higher.
https://git-scm.com/docs/git-replace
git log and tig show the modified commit, but GitHub and GitLab don't, so the usefulness of this feature is still limited.
4 Comments
To make sure you are making changes on the right branch
git checkout
#to make sure you are making changes on the right branch just to be sure:
git checkout branchname
Then
git commit --amend -m "new message"
Then push
git push --force
Comments
Working
We have git amend command using which we can update commit message alone or along with additional files which will be part of last commit so we have to use
git commit --amend -m "New commit message"
and then push with force so that even though commited to local or remote it will update both
git push --force
1 Comment
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
Comments
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:
$ git log commit 0c28141c68adae276840f17ccd4766542c33cf1d Author: First Last Date: Wed Aug 8 15:55:52 2018 -0600 Errata commit: This commit has no substantive code change. This commit is provided only to document a correction to a previous commit message. This pertains to commit object e083a7abd8deb5776cb304fa13731a4182a24be1 Original incorrect commit message: Changed background color to red Correction (*change highlighted*): Changed background color to *blue* commit 032d0ff0601bff79bdef3c6f0a02ebfa061c4ad4 Author: First Last Date: Wed Aug 8 15:43:16 2018 -0600 Some interim commit message commit e083a7abd8deb5776cb304fa13731a4182a24be1 Author: First Last Date: Wed Aug 8 13:31:32 2018 -0600 Changed background color to red
6 Comments
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*’ ‘’’git notes This would serve the same purpose as an "errata commit". Simply add a note to a previous commit to annotate or correct any errors in the commit message: https://git-scm.com/docs/git-notesFWIW in the simplest case, the checked answer is correct. I just wanted to share a recent experience.
This worked.
git commit --amend -m"The new message here"
git push --force origin
And the tester was able to checkout the branch for the first time and has only the amended message.
So in the simplest case (single developer and/or known that no one has fetch/pulled or checked out the branch) that is sufficient.
Comments
Simply use this 2 commands to change the commit message of your last push
- -$ git commit --amend -m "New commit message."
- -$ git push --force-with-lease
2 Comments
Command 1
You need to change your commit message use the Below command
git commit --amend -m "New and correct message"Command 2
After the add a new message and then below command execute
git push -f origin <your_branch_name>
Comments
Event if you commit worng message with -S parameter following would work:
git checkout branch
git commit --amend -m "Your new message"
at this point do not forget that you added a change and your remote is different, this why you need to do:
git push --force
If you are in VSCode you will see the difference, with --force parameter you ignore git pull and you push your changed message to the same commit.
Comments
I'm a little bit new to Git, but I just wanna add my experience.
git commit --amend -m "New and correct message"
This worked great but the next was the problem for me. I already pushed the commit before changing the commit message. Finally, when I tried to push to the remote, it git threw an exception. So I should have pull down again before updating the remote branch.
git pull origin branch-name
git push origin branch-name
Hope my minor experience helps you. Thanks.
1 Comment
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
Comments
I did my first attempt at renaming about 6 old commit messages that were already pushed and I had since done further commits.
So it's the nastiest case 'Case 4 : Already pushed + old commit'.
I'm a Vim (Nvim now) user and a mega fan of Vim's Fugitive. So this is how I did it with that.
This is the general help that you have around rebasing in Fugitive:
Rebase maps ~
ri Perform an interactive rebase. Uses ancestor of
u commit under cursor as upstream if available.
rf Perform an autosquash rebase without editing the todo
list. Uses ancestor of commit under cursor as
upstream if available.
ru Perform an interactive rebase against @{upstream}.
rp Perform an interactive rebase against @{push}.
rr Continue the current rebase.
rs Skip the current commit and continue the current
rebase.
ra Abort the current rebase.
re Edit the current rebase todo list.
rw Perform an interactive rebase with the commit under
the cursor set to `reword`.
rm Perform an interactive rebase with the commit under
the cursor set to `edit`.
rd Perform an interactive rebase with the commit under
the cursor set to `drop`.
r<Space> Populate command line with ":Git rebase ".
r? Show this help.
Get a list of commits.
:Git log --onelineenter image description hereI want to fix the commit message on line 12 (
4d43a1b build(MPL-402): editorconfig fix for messges.json), because its not in the proper Conventional Commit format.I put my cursor over the commit hash
4d43a1band type rwThis will "Perform an interactive rebase with the commit under the cursor set to
reword.". Note how nice this is compared togit rebase -i HEAD~X- knowing what thatXis is not so simple. enter image description hereThis should now give you the correct
git rebasecommand. So write and quit that buffer with:wqThen follow the steps through as git/fugitive guides you. I got a merge conflict for one of my commits which I fixed through my normal fugitve merge conflict process.
Hopefully that's enough to get you over the first "I've never done a rebase before, what the hell am I supposed to do?" hurdle. Leave me a comment if you want help with the later steps.
Comments
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