我刚刚意识到我遗漏了一个文件,我本来应该添加到5提交的提交中.在提交消息中,我说该文件已包含在内,我不想做一个新的提交文本"哎呀忘了在提交#XXXXX中添加此文件"
编辑先前提交的最佳方法是什么,以便我可以添加文件?
tul*_*tul 15
您可以使用 来git commit --fixup <hash>进行专门标记的提交,该提交旨在与哈希为 的先前提交合并<hash>。这非常适合添加丢失的文件或修复拼写错误等。
一旦有了修复提交,您需要使用它git rebase --interactive --autosquash <starting-point>来将修复提交实际合并到<hash>提交中。变基<starting-point>的 应该是提交之前历史记录中的某个点<hash>(为了简单起见,您可以使用<hash>^)。
历史重写的常见注意事项适用,如果您已经在其他用户已从中提取的地方发布了分支,那么如果您使用重写的历史记录重新推送,通常会导致很多混乱和合并问题。在这些情况下,将更正作为新的提交推送会更简单。
注意:git config --global rebase.autosquash true默认情况下将打开自动压缩,这意味着您不再需要将该--autosquash选项传递给 rebase 交互式命令。这是一个很好的默认设置。
可以在这里找到自动挤压的一个很好的演练:https ://thoughtbot.com/blog/autosquashing-git-commits
提交您的修复,然后使用git rebase --interactive重新排序您的提交并将两个提交压缩在一起.有关详细信息,请参阅git书.
请注意,如果这些提交已经被推送过,那么这样做是个坏主意,因为您将更改存储库历史记录.
示例会话可能如下所示:
% git init
Initialized empty Git repository in /home/user/repo/.git/
% echo "A line" > a.txt
% echo "A line" > b.txt
% git add a.txt b.txt
% git commit -m "Initial commit"
[master (root-commit) c6329d0] Initial commit
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
Run Code Online (Sandbox Code Playgroud)
你的不完整提交:
% echo "Another line" >> a.txt
% git add a.txt
% git commit -m "Important changes"
[master 0d28cfa] Important changes
1 files changed, 1 insertions(+), 0 deletions(-)
Run Code Online (Sandbox Code Playgroud)
其他一些提交:
% echo "Yet another line" >> b.txt
% git add b.txt
% git commit -m "Other changes"
[master 96a092d] Other changes
1 files changed, 1 insertions(+), 0 deletions(-)
Run Code Online (Sandbox Code Playgroud)
请注意,你已经忘记了一些事情:
% echo "Important line forgotten previously" >> a.txt
% git add a.txt
% git commit -m "Oops"
[master 9dce889] Oops
1 files changed, 1 insertions(+), 0 deletions(-)
Run Code Online (Sandbox Code Playgroud)
修复历史记录git rebase -i:
% git rebase -i HEAD~3
Run Code Online (Sandbox Code Playgroud)
您将被置于您选择的编辑器中,其内容类似于以下内容:
pick 0d28cfa Important changes
pick 96a092d Other changes
pick 9dce889 Oops
Run Code Online (Sandbox Code Playgroud)
更改它以便将"oops"提交移到上面一行并更改pick为squash(或只是s)以将其与前面的提交组合:
pick 0d28cfa Important changes
s 9dce889 Oops
pick 96a092d Other changes
Run Code Online (Sandbox Code Playgroud)
然后保存文件并退出编辑.这将弹出另一个编辑器,您可以在其中编辑组合提交的提交消息.它看起来像这样:
# This is a combination of 2 commits.
# The first commit's message is:
Important changes
# This is the 2nd commit message:
Oops
Run Code Online (Sandbox Code Playgroud)
根据您的需要更改它,然后保存并退出.
最后,检查新提交确实是两个提交的组合:
% git log -p HEAD~2..HEAD~1
commit 7a4c496956eb269c551bbf027db8b0f2320b65e4
Author: User Name <user@host.tld>
Date: Fri Feb 3 22:57:31 2012 +0100
Important changes
diff --git a/a.txt b/a.txt
index 8d7158c..54df739 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,3 @@
A line
+Another line
+Important line forgotten previously
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1516 次 |
| 最近记录: |