推后更改git commit消息(因为没有人从远程拉出) [英] Changing git commit message after push (given that no one pulled from remote)

查看:147
本文介绍了推后更改git commit消息(因为没有人从远程拉出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个git commit和随后的推送。我想更改提交消息。如果我理解正确,这是不可取的,因为有人可能在我做出这样的更改之前从远程存储库中取出。如果我知道没有人拉扯什么?



有没有办法做到这一点?

解决方案



如果它是最近的提交,您可以简单地执行此操作:

  git commit --amend 

这会带上编辑器的最后一个提交信息,让你编辑消息。 (如果您想清除旧邮件并使用新邮件,您可以使用 -m 。)



推送



然后当您推送时,执行此操作:

  git push -force-with-lease< repository> <分支> 

或者您可以使用+:

  git push< repository> + LT;分支> 

或者您可以使用 - force

  git push --force< repository> <分支> 

使用这些命令时请小心。


  • 如果有人将更改推送到同一分支,您可能希望避免销毁这些更改。 - force-with-lease 选项是最安全的,因为如果存在任何上游更改(


  • <如果你没有明确指定分支,Git将使用默认的推送设置,如果你的默认推送设置是匹配,那么你可以同时销毁多个分支上的更改。




之后拉/取



错误消息,他们需要更新(假设他们自己没有做任何修改):

  git fetch origin 
git reset --hard origin / master#失去本地提交

小心当使用 reset --hard 时,如果您对分支进行了更改,则这些更改将被销毁。

A有关修改历史记录的信息



销毁的数据实际上只是旧的提交消息,但 - force doe不知道这一点,并会高兴地删除其他数据。所以想想 --force 为我想销毁数据,并且我知道数据正在被销毁。但是,当被销毁的数据被提交时,通常可以从reflog中恢复旧的提交 - 数据实际上是孤立的而不是被销毁的(尽管孤立的提交会被定期删除)。



如果你不认为你正在销毁数据,那么远离 - force ... 可能发生的事情



这就是 - force-with-lease 有点安全的原因。


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?

解决方案

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-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.

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.

这篇关于推后更改git commit消息(因为没有人从远程拉出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆