git 在 SourceTree Windows 中编辑提交消息(已推送到远程)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17604232/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:32:25  来源:igfitidea点击:

Edit a commit message in SourceTree Windows (already pushed to remote)

gitatlassian-sourcetree

提问by Community

How do I edit an incorrect commit message in SourceTree without touching the command line?

如何在不触及命令行的情况下在 SourceTree 中编辑不正确的提交消息?

Additional details:

额外细节:

  • This is not the latest commit.
  • Everything was already pushed to Bitbucket.
  • This is a private repository and I am the only collaborator.
  • I don't mind losing any of the previous commits, as I can re-commit them anytime.
  • I don't want however to lose any code modification ever made.
  • 这不是最新的提交。
  • 一切都已经推送到 Bitbucket。
  • 这是一个私人存储库,我是唯一的合作者。
  • 我不介意丢失任何以前的提交,因为我可以随时重新提交它们。
  • 然而,我不想丢失任何曾经做过的代码修改。

Outcome:

结果:

  • As it seems impossible at the moment according to your comments and replies, I'm going to create a new repository and start all over. Thanks all for your help!
  • 根据您的评论和回复,目前似乎不可能,我将创建一个新的存储库并重新开始。感谢你的帮助!

回答by

Here are the steps to edit the commit message of a previous commit (which is not the most recent commit) using SourceTree for Windows version 1.5.2.0:

以下是使用适用于 Windows 版本 1.5.2.0 的 SourceTree编辑先前提交(不是最近提交)的提交消息的步骤:

Step 1

第1步

Select the commit immediately beforethe commit that you want to edit. For example, if I want to edit the commit with message "FOOBAR!" then I need to select the commit that comes right before it:

选择要编辑的提交之前的提交。例如,如果我想编辑带有消息“FOOBAR!”的提交。然后我需要选择它之前的提交:

Selecting commit before the one that I want to edit.

在我要编辑的之前选择提交。

Step 2

第2步

Right-click on the selected commit and click Rebase children...interactively:

右键单击选定的提交并单击Rebase children...interactively

Selecting "Rebase children interactively".

选择“以交互方式重新设置子项”。

Step 3

第 3 步

Select the commit that you want to edit, then click Edit Messageat the bottom. In this case, I'm selecting the commit with the message "FOOBAR!":

选择要编辑的提交,然后单击Edit Message底部的。在这种情况下,我选择带有消息“FOOBAR!”的提交:

Select the commit that you want to edit.

选择要编辑的提交。

Step 4

第四步

Edit the commit message, and then click OK. In my example, I've added "SHAZBOT! SKADOOSH!"

编辑提交消息,然后单击OK。在我的示例中,我添加了“SHAZBOT!SKADOOSH!”

Edit the commit message

编辑提交消息

Step 5

第 5 步

When you return to interactive rebase window, click on OKto finish the rebase:

当您返回交互式 rebase 窗口时,单击OK完成 rebase:

Click OK to finish.

单击确定完成。

Step 6

第 6 步

At this point, you'll need to force-push your new changes since you've rebased commits that you've already pushed. However, the current 1.5.2.0 version of SourceTree for Windows does not allow you to force-push through the GUI, so you'll need to use Git from the command line anyways in order to do that.

此时,您需要强制推送您的新更改,因为您已经重新设置了已经推送的提交。但是,当前 1.5.2.0 版本的适用于 Windows 的 SourceTree 不允许您通过 GUI 强制推送,因此您无论如何都需要从命令行使用 Git 来执行此操作。

Click Terminalfrom the GUI to open up a terminal.

Terminal从 GUI单击以打开终端。

Click Terminal

点击终端

Step 7

第 7 步

From the terminal force-push with the following command,

从终端 force-push 使用以下命令,

git push origin <branch> -f

where <branch>is the name of the branch that you want to push, and -fmeans to force the push. The force push will overwriteyour commits on your remote repo, but that's OK in your case since you said that you're not sharing your repo with other people.

哪里<branch>是你要推送的分支名称,-f意思是强制推送。强制推送将覆盖您对远程仓库的提交,但在您的情况下没关系,因为您说您不与其他人共享您的仓库。

That's it! You're done!

就是这样!你完成了!

回答by Franc

On Version 1.9.6.1. For UnPushed commit.

在版本 1.9.6.1 上。对于 UnPushed 提交。

  1. Click on previously committed description
  2. Click Commit icon
  3. Enter new commit message, and choose "Ammend latest commit" from the Commit options dropdown.
  4. Commit your message.
  1. 单击先前提交的描述
  2. 单击提交图标
  3. 输入新的提交消息,然后从提交选项下拉列表中选择“修改最新提交”。
  4. 提交您的消息。

回答by Hosi Golden

If the comment message includes non-Englishcharacters, using method provided by user456814, those characters will be replaced by question marks. (tested under sourcetree Ver2.5.5.0)

如果评论消息包含非英文字符,使用user456814提供的方法,这些字符将被问号替换。(在sourcetree Ver2.5.5.0下测试)

So I have to use the following method.

所以我必须使用以下方法。

CAUTION: if the commit has been pulled by other members, changes below might cause chaos for them.

注意:如果提交已被其他成员拉取,下面的更改可能会给他们造成混乱。

Step1: In the sourcetree main window, locate your repo tab, and click the "terminal" button to open the git command console.

Step1:在sourcetree主窗口中,找到你的repo标签,点击“终端”按钮打开git命令控制台。

Step2:

步骤2

[Situation A]: target commit is the latest one.

[情况A]:目标提交是最新的。

1)In the git command console, input

1)在git命令控制台中,输入

git commit --amend -m "new comment message"

2)If the target commit has been pushed to remote, you have to push again by force. In the git command console, input

2)如果目标提交已经推送到远程,则必须再次强制推送。在 git 命令控制台中,输入

git push --force

[Situation B]: target commit is not the latest one.

[情况B]:目标提交不是最新的。

1)In the git command console, input

1)在git命令控制台中,输入

git rebase -i HEAD~n

It is to squash the latest ncommits. e.g. if you want to edit the message before the last one, nis 2. This command will open a vi window, the first word of each line is "pick", and you change the "pick" to "reword" for the line you want to edit. Then, input :wqto save&quit that vi window. Now, a new vi window will be open, in this window you input your new message. Also use :wqto save&quit.

它是压缩最新的n 次提交。例如,如果要编辑最后一条消息之前的消息,n为 2。此命令将打开一个 vi 窗口,每行的第一个单词是“ pick”,然后将该行的“ pick”更改为“ reword”你想编辑。然后,输入:wq以保存并退出该 vi 窗口。现在,将打开一个新的 vi 窗口,您可以在此窗口中输入新消息。也用于:wq保存&退出。

2)If the target commit has been pushed to remote, you have to push again by force. In the git command console, input

2)如果目标提交已经推送到远程,则必须再次强制推送。在 git 命令控制台中,输入

git push --force


Finally: In the sourcetree main window, Press F5to refresh.


最后:在sourcetree主窗口中,按F5刷新。

回答by Hosi Golden

Update

更新

Note: this answer was originally written with regard to older versions of SourceTree for Windows, and is now out-of-date.

注意:此答案最初是针对适用于 Windows 的旧版本 SourceTree 编写的,现在已过时。

See my new answerfor the current version of SourceTree for Windows, 1.5.2.0. I'm leaving this answer behind for historical purposes.

请参阅我对当前版本的 SourceTree for Windows 1.5.2.0 的新答案。出于历史目的,我将这个答案抛在脑后。

Original Answer

原答案

as I'm on Windows I don't have a command line tool nor do I know how to use one :( Is it the only way to get that sorted out? The GUI doesn't cover all the git's functions? — Original Poster

因为我在 Windows 上,我没有命令行工具,也不知道如何使用 :( 这是解决这个问题的唯一方法吗?GUI 没有涵盖 git 的所有功能?-原始海报

Regarding Git GUIs, no, they don't cover all of Git's functions. They don't even come close. I suggest you check out one of the answers in How do I edit an incorrect commit message in Git?, Git is flexible enough that there are multiple solutions...from the command line.

关于 Git GUI,不,它们没有涵盖 Git 的所有功能他们甚至没有接近我建议您查看如何在 Git 中编辑不正确的提交消息中的答案之一?, Git 足够灵活,有多种解决方案......来自命令行。

SourceTree might actually come with the msysgit bash shell already, or it might be able to use the standard Windows command shell. Either way, you open it up form SourceTree by clicking the Terminal button:

SourceTree 可能实际上已经带有 msysgit bash shell,或者它可能能够使用标准的 Windows 命令 shell。无论哪种方式,您都可以通过单击“终端”按钮从 SourceTree 中打开它:

enter image description here

在此处输入图片说明

You set which terminal SourceTree uses (bash or Windows) here:

您可以在此处设置 SourceTree 使用的终端(bash 或 Windows):

enter image description here

在此处输入图片说明

One way to solve the problem in SourceTree

SourceTree中解决问题的一种方法

That being said, here's one way you can do it in SourceTree. Since you mentioned in the commentsthat you don't mind "reverting back to the faulty commit" (by which I assume you actually mean resetting, which is a different operation in Git), then here are the steps:

话虽如此,这是您可以在 SourceTree 中执行此操作的一种方法。由于您在评论中提到您不介意“恢复到错误的提交”(我假设您实际上是指重置,这是 Git 中的不同操作),那么以下是步骤:

  1. Do a hard reset in SourceTree to the bad commit by right-clicking on it and selecting Reset current branch to this commit, and selecting the hard reset option from the drop down. enter image description here
  2. Click the Commit button, then
  3. Click on the checkbox at the bottom that says "Amend latest commit". enter image description here
  4. Make the changes you want to the message, then click Commit again. Voila!
  1. 在 SourceTree 中对错误提交进行硬重置,方法是右键单击它并选择Reset current branch to this commit,然后从下拉列表中选择硬重置选项。在此处输入图片说明
  2. 点击提交按钮,然后
  3. 单击底部显示“修改最新提交”的复选框。 在此处输入图片说明
  4. 对消息进行所需的更改,然后再次单击“提交”。瞧!

Regarding this comment:

关于此评论

if it's not possible because it's already pushed to Bitbucket, I would not mind creating a new repository and starting over.

如果不可能因为它已经被推送到 Bitbucket,我不介意创建一个新的存储库并重新开始。

Does this mean that you're the only person working on the repo? This is important because it's not trivial to change the history of a repo (like by amending a commit) without causing problems for your collaborators. However, assuming that you're the only person working on the repo, then the next thing you would want to do is force push your changed history to the remote.

这是否意味着你是唯一一个在 repo 上工作的人?这很重要,因为在不给合作者带来问题的情况下更改 repo 的历史记录(例如通过修改提交)并非易事。但是,假设您是唯一一个在 repo 上工作的人,那么您要做的下一件事就是将更改的历史记录强制推送到远程。

Be aware, though, that because you did a hard reset to the faulty commit, then force pushing causes you to lose all work that come after it previously. If that's okay, then you might need to use the following command at the command line to do the force push, because I couldn't find an option to do it in SourceTree:

但是请注意,因为您对错误提交进行了硬重置,然后强制推送会导致您丢失之前的所有工作。如果没问题,那么您可能需要在命令行中使用以下命令来执行强制推送,因为我在 SourceTree 中找不到执行此操作的选项:

git push remote-repo head -f

This also assumes that BitBucket will allow you to force push to a repo.

这还假设 BitBucket 将允许您强制推送到存储库。

You should really learn how to use Git from the command line anyways though, it'll make you more proficient in Git. #ProTip, use msysgit and turn on Quick Edit mode on in the terminal properties, so that you can double click to highlight a line of text, right click to copy, and right click again to paste. It's pretty quick.

不管怎样,你真的应该学习如何从命令行使用 Git,它会让你更精通 Git。#ProTip,使用msysgit,在终端属性中开启快速编辑模式,这样就可以双击高亮一行文字,右键复制,再次右键粘贴。这很快。

回答by Klom Dark

I just had to use your instructions while working with SourceTree, so thank you!

我只需要在使用 SourceTree 时使用您的说明,谢谢!

Was surprised because GitKraken does this with one step.

很惊讶,因为 GitKraken 一步完成了。