本文介绍了如何修改现有的,未推送的提交消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交消息中写错了东西.

I wrote the wrong thing in a commit message.

如何更改消息?提交尚未被推送.

How can I change the message? The commit has not been pushed yet.

推荐答案

修改最新的提交消息

git commit --amend

将打开您的编辑器,使您可以更改最近一次提交的提交消息.另外,您可以使用以下命令直接在命令行中设置提交消息:

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

git commit --amend -m "New commit message"

…但是,这会使输入多行提交消息或进行小的更正变得更加麻烦.

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

在执行此操作之前,请确保没有已暂存的任何工作副本更改,否则它们也会被提交. (未暂存的更改将不会提交.)

Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

如果您已经将提交推送到远程分支,那么-在本地修改提交(如上所述)之后-您还将:

If you've already pushed your commit up to your remote branch, then - after amending your commit locally (as described above) - you'll also need to force push the commit with:

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f

警告:强行推入将使用您本地的状态覆盖远程分支.如果远程分支上有您本地分支中没有的提交,您将 丢失这些提交.

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.

警告:对修改您已经与其他人共享的提交保持谨慎.修改提交本质上是重写,使它们具有不同的 SHA ID,如果其他人拥有您已重写的旧提交的副本,则会带来问题.拥有旧提交副本的任何人都需要将其工作与新重写的提交同步,这有时可能很困难,因此在尝试重写共享提交历史记录时请确保与他人协调,或者只是避免重写共享提交完全.

Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.

另一个选择是使用交互式变基.这样,即使不是最新消息,您也可以编辑要更新的任何消息.

Another option is to use interactive rebase.This allows you to edit any message you want to update even if it's not the latest message.

要进行Git壁球,请执行以下步骤:

In order to do a Git squash, follow these steps:

// n is the number of commits up to the last commit you want to be able to edit
git rebase -i HEAD~n

压缩提交后-选择e/r来编辑消息:

Once you squash your commits - choose the e/r for editing the message:

当您使用git rebase -i HEAD~n时,可能会比n次提交更多. Git将收集"最近n次提交中的所有提交,如果在该范围之间某处存在合并,您还将看到所有提交,因此结果将为n +.

When you use git rebase -i HEAD~n there can be more than n commits. Git will "collect" all the commits in the last n commits, and if there was a merge somewhere in between that range you will see all the commits as well, so the outcome will be n + .

如果您需要为多个分支执行此操作,并且在修改内容时可能会遇到冲突,请设置,然后让Git为您自动解决这些冲突.

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let Git resolve those conflicts automatically for you.

git-rebase(1)手册页面

git-push(1)手册页面

这篇关于如何修改现有的,未推送的提交消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 20:09