如何编辑以前的git commit? [英] How do I edit a previous git commit?

查看:59
本文介绍了如何编辑以前的git commit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到我遗漏了一个应该添加到提交中的文件,例如5次提交.在提交消息中,我说该文件已包括在内,我不想做一个新的提交,文本为糟糕,忘记在提交#XXXXX中添加此文件"

编辑上一个提交以便添加文件的最佳方法是什么?

解决方案

提交修订,然后使用 git rebase --interactive 重新排列提交并将两个提交压缩在一起.有关详细信息,请参见 git书.>

请注意,如果这些提交已经被推送到某个位置,那么这样做是个坏主意,因为您将更改存储库的历史记录.

示例会话如下所示:

 %git初始化在/home/user/repo/.git/中初始化了空的Git存储库%echo"A行">a.txt%echo"A行">b.txt%git添加a.txt b.txt%git commit -m初始提交"[master(root-commit)c6329d0]初始提交更改2个文件,2个插入(+),0个删除(-)创建模式100644 a.txt创建模式100644 b.txt 

您未完成的提交:

 %echo另一行">>a.txt%git添加一个.txt%git commit -m重要更改"[master 0d28cfa]重要更改更改了1个文件,插入了1个(+),删除了0个(-) 

其他一些提交:

 %echo"Yet another line">>b.txt%git添加b.txt%git commit -m其他更改"[master 96a092d]其他更改更改了1个文件,插入了1个(+),删除了0个(-) 

请注意,您已经忘记了一些东西:

 %echo先前忘记的重要行">>a.txt%git添加一个.txt%git commit -m糟糕"[master 9dce889]糟糕更改了1个文件,插入了1个(+),删除了0个(-) 

使用 git rebase -i 修复历史记录:

 %git rebase -i HEAD〜3 

您将进入您选择的编辑器,其内容类似于以下内容:

  pick 0d28cfa重要更改选择96a092d其他更改选择9dce889糟糕 

更改它,以便将"oops"提交移至第一行,然后将 pick 更改为 squash (或仅更改 s )以进行组合它与前面的提交:

  pick 0d28cfa重要更改s 9dce889糟糕选择96a092d其他更改 

然后保存文件并退出编辑.这将弹出另一个编辑器,您可以在其中编辑合并提交的提交消息.看起来像这样:

 #这是2次提交的组合.#第一次提交的消息是:重要变化#这是第二条提交消息:哎呀 

根据需要进行更改,然后保存并退出.

最后,检查新提交确实是两个提交的组合:

 %git log -p HEAD〜2..HEAD〜1提交7a4c496956eb269c551bbf027db8b0f2320b65e4作者:用户名< user@host.tld>日期:2012年2月3日星期五22:57:31 +0100重要变化diff --git a/a.txt b/a.txt索引8d7158c..54df739 100644--- a/a.txt+++ b/a.txt@@ -1 +1,3 @@一条线+另一条线+重要的行之前被遗忘了 

I just realized that I left out a file that I was supposed to add to a commit like 5 commits back. In the commit message I said that the file was included, and I don't feel like doing a new commit with the text "Oops forgot to add this file in commit #XXXXX"

What's the best way to edit a previous commit so I can add the file?

解决方案

Commit your fix, then use git rebase --interactive to reorder your commits and squash the two commits together. See the git book for details.

Note that doing this is bad idea if those commits have been pushed somewehere already, since you will change the repository history.

An example session could look like this:

% 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

Your incomplete commit:

% 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(-)

Some other commit:

% 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(-)

Notice that you've forgotten somthing:

% 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(-)

Fix the history with git rebase -i:

% git rebase -i HEAD~3

You will be put into your editor of choice with contents similar to the following:

pick 0d28cfa Important changes
pick 96a092d Other changes
pick 9dce889 Oops

Change it so that the "oops" commit is moved one line above and change pick to squash (or just s) to combine it with the preceding commit:

pick 0d28cfa Important changes
s 9dce889 Oops
pick 96a092d Other changes

Then save the file and exit the edit. This will pop up another editor, where you can edit the commit message for the combined commit. It will look like this:

# This is a combination of 2 commits.
# The first commit's message is:

Important changes

# This is the 2nd commit message:

Oops

Change it as you feel is appropriate, then save and exit.

Finally, check that the new commit indeed is a combination of the two commits:

% 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

这篇关于如何编辑以前的git commit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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