如何修改现有的、未推送的提交消息? [英] How to modify existing, unpushed commit messages?

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

问题描述

我在提交信息中写错了.

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

解决方案

修改最近的提交信息

git commit --amend

将打开您的编辑器,允许您更改最近提交的提交消息.此外,您可以直接在命令行中设置提交消息:

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

……然而,这会使多行提交消息或小的更正输入更麻烦.

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

更改已推送到远程分支的提交的消息

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

关于交互式 rebase 的重要说明

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

好的提示:

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


文档

I wrote the wrong thing in a commit message.

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

解决方案

Amending the most recent commit message

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

Changing the message of a commit that you've already pushed to your remote branch

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.

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.


Perform an interactive rebase

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.

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

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

Important note about interactive rebase

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

Good tip:

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.


Documentation

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

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