Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

git - 如何修改现有的未推送的提交消息?(How to modify existing, unpushed commit messages?)

I wrote the wrong thing in a commit message. (我在提交消息中写错了内容。)

How can I change the message? (如何更改信息?) The commit has not been pushed yet. (提交尚未被推送。)

  ask by Laurie Young translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 you'll 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. (修改提交实质上会将它们重写为具有不同的SHA ID,如果其他人拥有您已重写的旧提交的副本,则会带来问题。) 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. (拥有旧提交副本的任何人都需要将其工作与新重写的提交同步,这有时可能很困难,因此在尝试重写共享提交历史记录时请确保与他人协调,或者只是避免重写共享提交共。)


Use 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: (为了做一个壁球南瓜,请按照下列步骤操作:)

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

Once you squash your commits - choose the e/r for editing the message: (压缩提交后,请选择e/r来编辑消息:)

在此处输入图片说明

Important note about interactive rebase (有关交互式变基的重要说明)

When you use git rebase -i HEAD~X there can be more than X commits. (当您使用git rebase -i HEAD~X提交的次数可能超过X) Git will "collect" all the commits in the last X 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 X+. (Git将“收集”最近X提交中的所有提交,并且如果在该范围之间某处存在合并,您还将看到所有提交,因此结果将为X +。)

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. (如果您需要为多个分支执行此操作,并且在修改内容时可能会遇到冲突,请设置git rerere并让Git为您自动解决这些冲突。)


Documentation (文献资料)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...