如何修改特定提交?
我有以下提交历史记录:
-
HEAD
-
HEAD~
-
HEAD~2
-
HEAD~3
git commit - -amend
修改当前的 HEAD
提交。 但如何修改 HEAD~3
呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有以下提交历史记录:
HEAD
HEAD~
HEAD~2
HEAD~3
git commit - -amend
修改当前的 HEAD
提交。 但如何修改 HEAD~3
呢?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(22)
使用很棒的交互式变基:
找到所需的提交,将
pick
更改为e
(edit
),然后保存并关闭文件。 Git 将回退到该提交,允许您:后者对于执行更复杂的操作(例如拆分为多个提交)很有用。
然后,运行 git rebase --continue,Git 将在修改后的提交之上重播后续更改。 可能会要求您修复一些合并冲突。
注意:
@
是HEAD
的简写,~
是指定提交之前的提交。阅读有关 Git 文档中的重写历史记录的更多信息。
不要害怕对
ProTip™ 进行变基: 不要害怕尝试重写历史记录的“危险”命令* — 默认情况下,Git 在 90 天内不会删除您的提交; 您可以在引用日志中找到它们:
* 但请注意
--hard
和--force
等选项 - 它们可能会丢弃数据。< br>* 此外,不要重写您正在协作的任何分支上的历史记录。
在许多系统上,
git rebase -i
将默认打开 Vim。 Vim 的工作方式与大多数现代文本编辑器不同,因此请查看如何使用 Vim 进行变基。 如果您想使用其他编辑器,请使用 git config --global core.editor your-favorite-text-editor 进行更改。Use the awesome interactive rebase:
Find the commit you want, change
pick
toe
(edit
), and save and close the file. Git will rewind to that commit, allowing you to either:git commit --amend
to make changes, orgit reset @~
to discard the last commit, but not the changes to the files (i.e. take you to the point you were at when you'd edited the files, but hadn't committed yet).The latter is useful for doing more complex stuff like splitting into multiple commits.
Then, run
git rebase --continue
, and Git will replay the subsequent changes on top of your modified commit. You may be asked to fix some merge conflicts.Note:
@
is shorthand forHEAD
, and~
is the commit before the specified commit.Read more about rewriting history in the Git docs.
Don't be afraid to rebase
ProTip™: Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:
* Watch out for options like
--hard
and--force
though — they can discard data.* Also, don't rewrite history on any branches you're collaborating on.
On many systems,
git rebase -i
will open up Vim by default. Vim doesn't work like most modern text editors, so take a look at how to rebase using Vim. If you'd rather use a different editor, change it withgit config --global core.editor your-favorite-text-editor
.当我需要修复以前的问题时,我经常使用与
--autosquash
交互的 rebase致力于更深入的历史。 它本质上加速了 ZelluX 的答案所说明的过程,并且当您需要编辑多个提交时特别方便。从文档中:
假设您有如下所示的历史记录:
并且您有想要修改到 Commit2 的更改,然后使用
以下方式提交您的更改,或者您可以使用提交-sha 而不是提交消息,因此
"fixup! e8adec4
甚至只是提交消息的前缀。之前对提交启动交互式变基
然后在编辑器打开
,其中所有 提交都已正确排序你需要做的就是保存并退出
Interactive rebase with
--autosquash
is something I frequently use when I need to fixup previous commits deeper in the history. It essentially speeds up the process that ZelluX's answer illustrates, and is especially handy when you have more than one commit you need to edit.From the documentation:
Assume you have a history that looks like this:
and you have changes that you want to amend to Commit2 then commit your changes using
alternatively you can use the commit-sha instead of the commit message, so
"fixup! e8adec4
or even just a prefix of the commit message.Then initiate an interactive rebase on the commit before
your editor will open with the commits already correctly ordered
all you need to do is save and exit
基于文档
修改旧提交或多次提交的消息messages
上面显示了当前分支上最近 3 次提交的列表,如果需要更多,请将 3 更改为其他内容。 该列表将类似于以下内容:
在要更改的每条提交消息之前将 pick 替换为 reword。 假设您更改了列表中的第二个提交,您的文件将如下所示:
保存并关闭提交列表文件,这将弹出一个新编辑器供您更改提交消息,更改提交消息并保存。
最后,强制推送修改后的提交。
Based on Documentation
Amending the message of older or multiple commit messages
The above displays a list of the last 3 commits on the current branch, change 3 to something else if you want more. The list will look similar to the following:
Replace pick with reword before each commit message you want to change. Let say you change the second commit in the list, your file will look like the following:
Save and close the commit list file, this will pop up a new editor for you to change your commit message, change the commit message and save.
Finally, force-push the amended commits.
运行:
$ git rebase --interactive commit_hash^
每个
^
表示您要编辑的提交数量,如果只有一个(您指定的提交哈希),则您只需添加一个^
即可。使用 Vim,您可以将要更改的提交的单词
pick
更改为reword
,然后保存并退出 (:wq
)。 然后 git 会提示您标记为 reword 的每个提交,以便您可以更改提交消息。您必须保存每条提交消息并退出 (
:wq
) 才能转到下一条提交消息如果您想退出而不应用更改,请按
:q!
编辑:要在
vim
中导航,您可以使用j
向上,k
向下,h
code> 向左走,l
向右走(所有这些都在NORMAL
模式下,按ESC
转到NORMAL模式)。
要编辑文本,请按
i
以便进入插入文本的INSERT
模式。按
ESC
返回到NORMAL
模式:)更新:这是来自 github 列表的一个很棒的链接 如何使用 git 撤消(几乎)任何内容
Run:
$ git rebase --interactive commit_hash^
each
^
indicates how many commits back you want to edit, if it's only one (the commit hash that you specified), then you just add one^
.Using Vim you change the words
pick
toreword
for the commits you want to change, save and quit(:wq
). Then git will prompt you with each commit that you marked as reword so you can change the commit message.Each commit message you have to save and quit(
:wq
) to go to the next commit messageIf you want to exit without applying the changes, press
:q!
EDIT: to navigate in
vim
you usej
to go up,k
to go down,h
to go left, andl
to go right( all this inNORMAL
mode, pressESC
to go toNORMAL
mode ).To edit a text, press
i
so that you enter theINSERT
mode, where you insert text.Press
ESC
to go back toNORMAL
mode :)UPDATE: Here's a great link from github listing How to undo (almost) anything with git
完全非交互式命令(1)
我只是想共享一个为此使用的别名。 它基于非交互式交互式变基。 要将其添加到您的 git,请运行此命令(解释如下):
或者,也可以处理未暂存文件的版本(通过存储然后取消存储它们):
此命令的最大优点是它 >无vim。
(1)考虑到 rebase 期间没有冲突,当然
用法
名称
amend-to
恕我直言似乎很合适。 将流程与--amend
进行比较:解释
git config --global alias. '!'
- 创建一个名为
的全局 git 别名,它将执行非 git 命令f() { <身体>; }; f
- 一个“匿名”bash 函数。SHA=`git rev-parse "$1"`;
- 将参数转换为 git revision,并将结果分配给变量SHA
git commit --fixup " $SHA"
-SHA
的修复提交。 请参阅git-commit
文档GIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^"
git rebase --interactive "$SHA^"
部分已被其他答案涵盖。--autosquash
与git commit --fixup
结合使用,请参阅git-rebase
文档了解更多信息GIT_SEQUENCE_EDITOR=true
使整个事情变得非交互式。 我从这篇博文学到了这个技巧< /a>.Completely non-interactive command(1)
I just thought I'd share an alias that I'm using for this. It's based on non-interactive interactive rebase. To add it to your git, run this command (explanation given below):
Or, a version that can also handle unstaged files (by stashing and then un-stashing them):
The biggest advantage of this command is the fact that it's no-vim.
(1)given that there are no conflicts during rebase, of course
Usage
The name
amend-to
seems appropriate IMHO. Compare the flow with--amend
:Explanation
git config --global alias.<NAME> '!<COMMAND>'
- creates a global git alias named<NAME>
that will execute non-git command<COMMAND>
f() { <BODY> }; f
- an "anonymous" bash function.SHA=`git rev-parse "$1"`;
- converts the argument to git revision, and assigns the result to variableSHA
git commit --fixup "$SHA"
- fixup-commit forSHA
. Seegit-commit
docsGIT_SEQUENCE_EDITOR=true git rebase --interactive --autosquash "$SHA^"
git rebase --interactive "$SHA^"
part has been covered by other answers.--autosquash
is what's used in conjunction withgit commit --fixup
, seegit-rebase
docs for more infoGIT_SEQUENCE_EDITOR=true
is what makes the whole thing non-interactive. This hack I learned from this blog post.如果由于某种原因您不喜欢交互式编辑器,您可以使用 git rebase --onto 。
假设您要修改
Commit1
。 首先,从之前Commit1
开始分支:其次,使用
cherry-pick
抓取Commit1
:现在,修改您的更改,创建
Commit1'
:最后,在隐藏任何其他更改后,将其余的提交移植到
master
之上新提交:
读取:“变基,到分支
amending
,Commit1
(不包含)和master
(包含)之间的所有提交”。 即 Commit2 和 Commit3,完全删除旧的 Commit1。 你可以直接挑选它们,但这种方法更容易。记得清理你的树枝!
If for some reason you don't like interactive editors, you can use
git rebase --onto
.Say you want to modify
Commit1
. First, branch from beforeCommit1
:Second, grab
Commit1
withcherry-pick
:Now, amend your changes, creating
Commit1'
:And finally, after having stashed any other changes, transplant the rest of your commits up to
master
on top of yournew commit:
Read: "rebase, onto the branch
amending
, all commits betweenCommit1
(non-inclusive) andmaster
(inclusive)". That is, Commit2 and Commit3, cutting the old Commit1 out entirely. You could just cherry-pick them, but this way is easier.Remember to clean up your branches!
git stash
+rebase
自动化当我需要多次修改旧提交以进行 Gerrit 审查时,我一直在这样做:
< a href="https://github.com/cirosantilli/dotfiles/blob/72584f5126e59b6ef6a6658210df4a8d38c78dfd/home/.bashrc#L1824" rel="noreferrer">GitHub 上游。
用法:
git add
git-amend-old $old_sha
我喜欢这个而不是
--autosquash
因为它不会压制其他不相关的修复。git stash
+rebase
automationFor when I need to modify an old commit a lot of times for Gerrit reviews, I've been doing:
GitHub upstream.
Usage:
git add
if already in repogit-amend-old $old_sha
I like this over
--autosquash
as it does not squash other unrelated fixups.自动交互式变基编辑,然后提交恢复,准备重做
我发现自己经常修复过去的提交,以至于我为它编写了一个脚本。
工作流程如下:
这将使您到达您要编辑的提交。
按照您希望的方式修复并暂存提交。
(您可能需要使用
git stash save
来保留您未提交的任何文件)使用
--amend
重做提交,例如:完成变基:
为了使上述工作正常进行,请将以下脚本放入
$PATH
git-commit-edit 的可执行文件中>:Automated interactive rebase edit followed by commit revert ready for a do-over
I found myself fixing a past commit frequently enough that I wrote a script for it.
Here's the workflow:
This will drop you at the commit you want to edit.
Fix and stage the commit as you wish it had been in the first place.
(You may want to use
git stash save
to keep any files you're not committing)Redo the commit with
--amend
, eg:Complete the rebase:
For the above to work, put the below script into an executable file called
git-commit-edit
somewhere in your$PATH
:最好的选择是使用“交互式变基命令”。
现在如何使用这个命令呢?
-i
代表“交互式”。 请注意,您可以在非交互模式下执行变基。 ex:HEAD
表示您当前的位置(也可以是分支名称或提交 SHA)。~n
表示“n beforeé”,因此HEAD~n
将是您当前所在的提交之前的“n”次提交的列表。git rebase 有不同的命令,例如:
pick
来保持提交原样。保留提交的内容,但更改提交消息
squash
:将此提交的更改合并到上一个提交(列表中位于其上方的提交)中注意:最好让 Git 与您的代码编辑器配合使用,以使事情变得更简单。 例如,如果您使用可视代码,您可以像这样添加 git config --global core.editor "code --wait" 。 或者您可以在 Google 中搜索如何将您喜欢的代码编辑器与 GIT 关联起来。
git rebase
示例我想更改我所做的最后 2 个提交,因此我像这样处理:
现在我使用 git rebase 来更改最后 2 条提交消息:
$git rebase -i HEAD~2
它打开代码编辑器并显示:
因为我想更改这 2 个提交的提交消息。 因此,我将输入
r
或reword
来代替pick
。 然后保存文件并关闭选项卡。请注意,
rebase
是在多步骤过程中执行的,因此下一步是更新消息。 另请注意,提交按时间倒序显示,因此最后一次提交显示在该提交中,第一个提交显示在第一行中,依此类推。更新消息:
更新第一条消息:
保存并关闭
编辑第二条消息
保存并关闭。
在变基结束时,您将收到类似这样的消息:
成功变基并更新了 refs/heads/documentation
这意味着您成功了。 您可以显示更改:我希望这可以帮助新用户:)。
The best option is to use "Interactive rebase command".
Now how to use this command?
-i
stand for "interactive". Note that you can perform a rebase in non-interactive mode. ex:HEAD
indicates your current location(can be also branch name or commit SHA). The~n
means "n beforeé, soHEAD~n
will be the list of "n" commits before the one you are currently on.git rebase
has different command like:p
orpick
to keep commit as it is.r
orreword
: to keep the commit's content but alter the commit message.s
orsquash
: to combine this commit's changes into the previous commit(the commit above it in the list).... etc.
Note: It's better to get Git working with your code editor to make things simpler. Like for example if you use visual code you can add like this
git config --global core.editor "code --wait"
. Or you can search in Google how to associate you preferred your code editor with GIT.Example of
git rebase
I wanted to change the last 2 commits I did so I process like this:
Now I use
git rebase
to change the 2 last commits messages:$git rebase -i HEAD~2
It opens the code editor and show this:
Since I want to change the commit message for this 2 commits. So I will type
r
orreword
in place ofpick
. Then Save the file and close the tab.Note that
rebase
is executed in a multi-step process so the next step is to update the messages. Note also that the commits are displayed in reverse chronological order so the last commit is displayed in that one and the first commit in the first line and so forth.Update the messages:
Update the first message:
save and close
Edit the second message
save and close.
You will get a message like this by the end of the rebase:
Successfully rebased and updated refs/heads/documentation
which means that you succeed. You can display the changes:I wish that may help the new users :).
更改最后一次提交:
不要修改公共提交
修改后的提交实际上是全新的提交,之前的提交将不再位于您当前的分支上。
例如,如果您想更改最后三个提交消息或该组中的任何提交消息,您可以将要编辑的最后一个提交的父级作为参数提供给 git rebase -i,即 HEAD~2 ^ 或 HEAD~3。 记住 ~3 可能更容易,因为您正在尝试编辑最后三个提交,但请记住,您实际上是在指定四个提交之前,即您要编辑的最后一个提交的父级:
了解更多
Changing the Last Commit:
Don’t amend public commits
Amended commits are actually entirely new commits and the previous commit will no longer be on your current branch.
For example, if you want to change the last three commit messages, or any of the commit messages in that group, you supply as an argument to git rebase -i the parent of the last commit you want to edit, which is HEAD~2^ or HEAD~3. It may be easier to remember the ~3 because you’re trying to edit the last three commits, but keep in mind that you’re actually designating four commits ago, the parent of the last commit you want to edit:
know more
采用这种方法(它可能与使用交互式变基完全相同),但对我来说这很简单。
注意:我提出这种方法是为了说明您可以做什么,而不是日常替代方案。 因为它有很多步骤(可能还有一些注意事项。)
假设您想要更改提交
0
并且您当前正在feature-branch
签出此提交并创建一个
快速分支
。 您还可以将功能分支克隆为恢复点(在开始之前)。您现在将看到类似这样的内容:
阶段发生变化,隐藏其他所有内容。
提交更改并检出回
feature-branch
您现在将得到如下内容:
将
feature-branch
重新设置为quick-branch
(解决任何冲突一路上)。 应用隐藏并删除quick-branch
。你最终会得到:
Git 不会在变基时重复(虽然我不能真正说到什么程度)0 提交。
注意:所有提交哈希值都是从我们最初打算更改的提交开始更改的。
Came to this approach (and it is probably exactly the same as using interactive rebase) but for me it's kind of straightforward.
Note: I present this approach for the sake of illustration of what you can do rather than an everyday alternative. Since it has many steps (and possibly some caveats.)
Say you want to change commit
0
and you are currently onfeature-branch
Checkout to this commit and create a
quick-branch
. You can also clone your feature branch as a recovery point (before starting).You will now have something like this:
Stage changes, stash everything else.
Commit changes and checkout back to
feature-branch
You will now have something like this:
Rebase
feature-branch
ontoquick-branch
(resolve any conflicts along the way). Apply stash and removequick-branch
.And you end up with:
Git will not duplicate (although I can't really say to what extent) the 0 commit when rebasing.
Note: all commit hashes are changed starting from the commit we originally intended to change.
要获取非交互式命令,请将包含此内容的脚本放入您的 PATH 中:
通过暂存更改(使用 git add )来使用它,然后运行 git fixup。 当然,如果发生冲突,它仍然会是互动的。
To get a non-interactive command, put a script with this content in your PATH:
Use it by staging your changes (with
git add
) and then rungit fixup <commit-to-modify>
. Of course, it will still be interactive if you get conflicts.我执行以下操作,包括更改本地提交的日期和时间:
~6
是要显示的提交历史记录的数量。pick
更改为edit
。Ctrl+O
保存,Ctrl+X
退出)git commit --amend --date="2022-09-02T19:10:04" -m "NEW_MSG"
git rebase --continue
最后,我验证更改,如果一切正常,我将执行
push
I do the following, including changing the date and time of local commits:
~6
is the amount of commit history to display.pick
toedit
the commits to be edited.Ctrl+O
to save andCtrl+X
to exit)git commit --amend --date="2022-09-02T19:10:04" -m "NEW_MSG"
git rebase --continue
Finally I validate the changes and if everything is ok, I do the
push
我解决了这个问题,
1)通过创建新的提交并进行我想要的更改。2
)我知道我需要与它合并哪个提交。 这是提交 3。
所以, git rebase -i HEAD~4 # 4 代表最近 4 次提交(这里提交 3 位于第四位)
3) 在交互式 rebase 中,最近提交将位于底部。 它看起来很相似,
4)如果你想与特定的合并,我们需要重新安排提交。 应该是这样,
重新排列后,您需要将
p
pick
替换为f
(fixup 将在没有提交消息的情况下合并) 或s
(squash 与提交消息合并可以在运行时更改),然后保存您的树。
现在与现有提交合并完成。
I solved this,
1) by creating new commit with changes i want..
2) i know which commit i need to merge with it. which is commit 3.
so,
git rebase -i HEAD~4
# 4 represents recent 4 commit (here commit 3 is in 4th place)3) in interactive rebase recent commit will located at bottom. it will looks alike,
4) here we need to rearrange commit if you want to merge with specific one. it should be like,
after rearrange you need to replace
p
pick
withf
(fixup will merge without commit message) ors
(squash merge with commit message can change in run time)and then save your tree.
now merge done with existing commit.
如果您只想重写旧的提交消息而不更改分支上的哈希值,您可以使用:
这将打开编辑器并显示如下内容:
修改消息并保存。
然后必须使用以下命令显式推送和获取修改后的引用:
If you only want to reword an old commit message without changing the hashes on the branch, you can use:
This opens the editor and displays something like this:
Modify the message and save.
The modified references will then have to be pushed and fetched explicitly with:
对我来说,这是为了从存储库中删除一些凭据。
我尝试了变基,并在尝试变基 - 继续时遇到了大量看似无关的冲突。
不要费心去尝试 rebase 自己,在 mac 上使用名为 BFG (brew install bfg) 的工具。
For me it was for removing some credentials from a repo.
I tried rebasing and ran into a ton of seemingly unrelated conflicts along the way when trying to rebase --continue.
Don't bother attempting to rebase yourself, use the tool called BFG (brew install bfg) on mac.
如果您还没有推送提交,那么您可以使用
git reset HEAD^[1,2,3,4...]
返回到之前的提交例如
糟糕,忘记添加 file2到第一次提交...
这会将 file2 添加到第一次提交。
If you haven't already pushed the commits then you can go back to a previous commit using
git reset HEAD^[1,2,3,4...]
For example
Oops, forgot to add file2 to the first commit...
This will add file2 to the first commit.
好吧,这个解决方案可能听起来很愚蠢,但在某些情况下可以拯救你。
我的一个朋友刚刚遇到不小心提交了一些巨大的文件(四个自动生成的文件,每个文件大小在 3GB 到 5GB 之间),然后在此基础上提交了一些额外的代码,然后才意识到< code>git push 不再工作了!
这些文件已在 .gitignore 中列出,但在重命名容器文件夹后,它们被暴露并提交了! 现在除此之外还有一些代码提交,但是
push
一直在运行(尝试上传 GB 的数据!),最终会因 Github 的文件大小限制。交互式变基或类似的问题是,他们需要处理这些巨大的文件,并且需要很长时间才能完成任何事情。 然而,在 CLI 中花费了近一个小时后,我们不确定这些文件(和增量)是否确实从历史记录中删除,或者只是不包含在当前提交中。 推送也不起作用,我的朋友真的被困住了。
所以,我想出的解决方案是:
~/Project-old
。~/Project
)。cp -r
将文件从~/Project-old
文件夹复制到~/Project
。~/Project
中的.git
文件夹。 这就是有问题的历史日志所在的地方!push
'ed 了。这个解决方案最大的问题是,它涉及手动复制一些文件,并且还将所有最近的提交合并为一个(显然带有一个新的提交哈希)。 B
最大的好处是,每一步都非常清晰,它非常适合大文件(以及敏感文件),并且不会在历史中留下任何痕迹!
Well, this solution might sound very silly, but can save you in certain conditions.
A friend of mine just ran into accidentally committing very some huge files (four auto-generated files ranging between 3GB to 5GB each) and then made some additional code commits on top of that before realizing the problem that
git push
wasn't working any longer!The files had been listed in
.gitignore
but after renaming the container folder, they got exposed and committed! And now there were a few more commits of the code on top of that, butpush
was running forever (trying to upload GB of data!) and finally would fail due to Github's file size limits.The problem with interactive rebase or anything similar was that they would deal with poking around these huge files and would take forever to do anything. Nevertheless, after spending almost an hour in the CLI, we weren't sure if the files (and deltas) are actually removed from the history or simply not included in the current commits. The push wasn't working either and my friend was really stuck.
So, the solution I came up with was:
~/Project-old
.~/Project
).cp -r
the files from~/Project-old
folder to~/Project
.mv
ed, and included in.gitignore
properly..git
folder in the recently-cloned~/Project
by the old one. That's where the logs of the problematic history lives!push
'ed.The biggest problem with this solution is, it deals with manual copying some files, and also it merges all the recent commits into one (obviously with a new commit-hash.) B
The big benefits are that, it is very clear in every step, it works great for huge files (as well as sensitive ones), and it doesn't leave any trace in history behind!
我遇到了同样的问题,这个:
x
,我的骑手git日志:
I had same problem and this this:
x
,My rider git log:
这是一个超级老的问题,但在搜索这个问题时仍然出现,所以我认为值得更新。 我的答案是 GitHub Desktop (obvs 仅适用于 github 存储库)。
今天,我能够通过转到我所在的存储库/分支的提交历史记录,然后一一执行它们来成功更新两个提交。 我还必须重新订购第二个,但这也很简单。 如果您不想冒险搞砸其他过程中的其中一个步骤,而只是想要一个干净的 UI,那么强烈建议您这样做。
This is a super old question, but it still comes up when searching for this issue, so I think it's worth updating. My answer is GitHub Desktop (which obvs only works for github repo's).
I was able to successfully update two commits today by going to the commit history for the repo/branch I'm in, and then doing them one by one. I had to also reorder the second one, but this was simple as well. Highly recommended if you don't want to risk screwing up one of the steps in the other procedures and just want a clean UI.
以下步骤帮助我们修改现有提交消息。
步骤 1. 键入 git rebase -i HEAD~N,其中 N 是要执行变基的提交数。 例如,如果您想更改第 4 个和第 5 个最新提交,您可以键入:
步骤 2. 该命令将在默认文本编辑器中显示最新的 X 次提交:
步骤 3. 移动到您想要的提交消息的行要更改并用 reword 替换 pick:
步骤 4:保存更改并关闭编辑器。
第 5 步:对于每个选定的提交,都会打开一个新的文本编辑器窗口。 更改提交消息,保存文件,然后关闭编辑器。
步骤 6:强制将更改推送到远程存储库:
Following steps help us to modify existing commit message.
Step 1. Type git rebase -i HEAD~N, where N is the number of commits to perform a rebase on. For example, if you want to change the 4th and the 5th latest commits, you would type:
Step 2.The command will display the latest X commits in your default text editor :
Step 3. Move to the lines of the commit message you want to change and replace pick with reword:
Step 4: Save the changes and close the editor.
Step 5: For each chosen commit, a new text editor window will open. Change the commit message, save the file, and close the editor.
Step 6: Force push the changes to the remote repository:
使用
git rebase
。 例如,要修改提交bbc643cd
,请运行:请注意命令末尾的波形符
~
,因为您需要在 < 的上一个提交之上重新应用提交代码>bbc643cd(即bbc643cd~
)。在默认编辑器中,将提及
bbc643cd
的行中的pick
修改为edit
。保存文件并退出。 git 会解释并自动执行文件中的命令。 您会发现自己处于之前刚刚创建提交
bbc643cd
的情况。此时,
bbc643cd
是您的最后一次提交,您可以 轻松修改。 进行更改,然后使用以下命令提交它们:之后,使用以下命令返回到之前的 HEAD 提交:
警告:请注意,这将更改该提交的 SHA-1 以及所有孩子——换句话说,这重写了从那时起的历史。 如果使用命令
git push --force
进行推送,则可以中断存储库执行此操作。Use
git rebase
. For example, to modify commitbbc643cd
, run:Please note the tilde
~
at the end of the command, because you need to reapply commits on top of the previous commit ofbbc643cd
(i.e.bbc643cd~
).In the default editor, modify
pick
toedit
in the line mentioningbbc643cd
.Save the file and exit. git will interpret and automatically execute the commands in the file. You will find yourself in the previous situation in which you just had created commit
bbc643cd
.At this point,
bbc643cd
is your last commit and you can easily amend it. Make your changes and then commit them with the command:After that, return back to the previous HEAD commit using:
WARNING: Note that this will change the SHA-1 of that commit as well as all children -- in other words, this rewrites the history from that point forward. You can break repos doing this if you push using the command
git push --force
.