推送后更改 git commit 消息(假设没有人从远程拉取)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8981194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing git commit message after push (given that no one pulled from remote)
提问by K_U
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?
我做了一个 git commit 和随后的推送。我想更改提交消息。如果我理解正确,这是不可取的,因为在我进行此类更改之前,有人可能已经从远程存储库中提取了数据。如果我知道没人拉过怎么办?
Is there a way to do this?
有没有办法做到这一点?
回答by Dietrich Epp
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.)
这将打开带有最后提交消息的编辑器,并让您编辑该消息。(-m
如果您想清除旧消息并使用新消息,则可以使用。)
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
:
或者你可以使用--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 (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.
如果其他人将更改推送到同一分支,您可能希望避免破坏这些更改。该
--force-with-lease
选项是最安全的,因为如果有任何上游更改,它将中止(如果没有明确指定分支,Git 将使用默认推送设置。如果您的默认推送设置是“匹配”,那么您可能会同时销毁多个分支上的更改。
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.
使用时要小心reset --hard
。如果您对分支进行了更改,这些更改将被销毁。
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 orphanedinstead of destroyed (although orphaned commits are periodically deleted).
销毁的数据实际上只是旧的提交消息,但--force
不知道这一点,并且也会很乐意删除其他数据。因此,请考虑--force
“我想销毁数据,并且我确定要销毁哪些数据”。但是当被破坏的数据被提交时,你通常可以从 reflog 中恢复旧的提交——数据实际上是孤立的而不是被破坏的(尽管孤立的提交会被定期删除)。
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
更安全的原因。
回答by Manish Shrivastava
Just say :
说啊 :
git commit --amend -m "New commit message"
and then
进而
git push --force
回答by Jinsong Li
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
)
步骤 1:git rebase -i HEAD~n
对n
受影响的最后一次提交进行交互式变基。(即,如果您想更改提交消息 3 次提交,请执行git rebase -i HEAD~3
)
git will pop up an editor to handle those commits, notice this command:
git 会弹出一个编辑器来处理这些提交,注意这个命令:
# 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.
第二步:更改pick
到r
为您要更新的消息,这些提交。不要在这里更改提交消息,它将被忽略。您将在下一步中执行此操作。保存并关闭编辑器。
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:
如果要更改用于交互式会话的文本编辑器(例如,从默认的 vi 更改为 nano),请运行:
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.
Step3:Git 会为你r
之前提交的每个修订版弹出另一个编辑器。根据需要更新提交消息,然后保存并关闭编辑器。
Step4: After all commits msgs are updated. you might want to do git push -f
to update the remote.
步骤 4:更新所有提交后消息。您可能想要git push -f
更新遥控器。
回答by Abdul Rizwan
Use these two step in console :
在控制台中使用这两个步骤:
git commit --amend -m "new commit message"
and then
进而
git push -f
Done :)
完毕 :)
回答by Steve Benner
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
refs,它们将因此被修改。请务必注意您的 git 存储库配置为推送到的位置。幸运的是,有一种方法可以通过指定要更新的单个分支来稍微保护该过程。从 git 手册页阅读:
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).
请注意,--force 适用于所有被推送的引用,因此将它与 push.default 设置为匹配或使用远程配置的多个推送目的地一起使用。*.push 可能会覆盖当前分支以外的引用(包括本地引用)严格落后于他们的远程对手)。要强制只推送到一个分支,请在 refspec 前使用 + 进行推送(例如 git push origin +master 强制推送到主分支)。
回答by Carlos
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 messagessection
如果要修改较旧的提交,而不是最后一个提交,则需要使用rebase
此处解释的命令,Github 帮助页面,在修改较旧或多个提交消息的消息部分
回答by Teo Choong Ping
Command 1.
命令1。
git commit --amend -m "New and correct message"
Then,
然后,
Command 2.
命令2。
git push origin --force
回答by Beu
git commit --amend
then edit and change the message in the current window. After that do
然后在当前窗口中编辑和更改消息。在那之后做
git push --force-with-lease
回答by rob_7cc
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 option --allow-empty
。它当然比变基更容易和更安全,它不会修改真实的历史,并且它保持分支树干净(使用amend
如果您要更正最近的提交,这也是一个不错的选择,但勘误提交可能是较旧的提交的不错选择)。这种事情很少发生,简单地记录错误就足够了。将来,如果您需要在 git log 中搜索功能关键字,则原始(错误)提交可能不会出现,因为该原始提交中使用了错误的关键字(原始错字)——但是,该关键字会出现在勘误提交中,它会将您指向具有错字的原始提交。下面是一个例子:
$ 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
回答by Abdallah Awwad Alkhwaldah
additional information for same problem if you are using bitbucket pipeline
如果您使用的是 bitbucket 管道,则相同问题的附加信息
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
然后将 --force 添加到管道上的推送命令
git ftp push --force
This will delete your previous commit(s) and push your current one.
这将删除您之前的提交并推送您当前的提交。
remove the --force after first push
第一次推送后删除 --force
i tried it on bitbucket pipeline and its working fine
我在 bitbucket 管道上试过了,它工作正常