【问题标题】:How do I edit a previous git commit? [duplicate]如何编辑以前的 git 提交? [复制]
【发布时间】:2012-02-24 13:55:27
【问题描述】:

我刚刚意识到我遗漏了一个应该添加到提交中的文件,例如 5 次提交。在提交消息中,我说该文件已包含在内,并且我不想使用文本“Oops forgot to add this file in commit #XXXXX”进行新的提交

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

【问题讨论】:

    标签: git


    【解决方案1】:

    提交您的修复,然后使用git rebase --interactive 重新排序您的提交并将两个提交压缩在一起。详情请见the git book

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

    示例会话可能如下所示:

    % 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
    

    您的未完成提交:

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

    其他一些提交:

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

    注意你忘记了什么:

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

    git rebase -i修复历史记录:

    % git rebase -i HEAD~3
    

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

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

    更改它,以便将“oops”提交移到上一行并将pick 更改为squash(或只是s)以将其与前面的提交结合起来:

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

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

    # This is a combination of 2 commits.
    # The first commit's message is:
    
    Important changes
    
    # This is the 2nd commit message:
    
    Oops
    

    根据自己的喜好进行更改,然后保存并退出。

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

    % 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
    

    【讨论】:

    • 我希望得到更彻底的解释,甚至可能是一个演练,但我想这是一个起点。
    • 我添加了一个例子,希望对你有帮助!
    【解决方案2】:

    您可以使用git commit --fixup &lt;hash&gt; 进行特殊标记的提交,该提交旨在与哈希为&lt;hash&gt; 的先前提交合并。这是添加丢失文件或修复拼写错误等的理想选择。

    一旦你有了修正提交,你需要使用git rebase --interactive --autosquash &lt;starting-point&gt; 将修正提交实际合并到&lt;hash&gt; 提交中。 rebase 的&lt;starting-point&gt; 应该是&lt;hash&gt; 提交之前的某个历史点(为简单起见,您可以使用&lt;hash&gt;^)。

    历史重写的常见警告适用,如果您已经在其他用户已撤出的地方发布了您的分支,如果您重新推送重写历史,通常会导致很多混乱和合并问题。在这些情况下,将更正作为新提交推送更简单。

    注意:git config --global rebase.autosquash true 默认会开启自动压缩,这意味着您不再需要将--autosquash 选项传递给 rebase 交互式命令。这是一个很好的默认设置。

    可以在这里找到关于自动压缩的一个很好的演练:https://thoughtbot.com/blog/autosquashing-git-commits

    【讨论】:

      【解决方案3】:

      为了做一个它做一个git squash

      // X is the number of commits you wish to edit
      git rebase -i HEAD~X
      

      压缩提交后 - 选择 e 或 'r' 进行编辑。

      为最新提交选择选择以保留它。


      另一种选择是使用过滤器分支

      这是获取参数的方法,您可以更新它们并使用新值而不是旧值重新提交。

      在这个示例中,我更改了电子邮件,但同样适用于消息。

      git filter-branch --commit-filter '
          if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
          then
                  GIT_COMMITTER_NAME="<New Name>";
                  GIT_AUTHOR_NAME="<New Name>";
                  GIT_COMMITTER_EMAIL="<New Email>";
                  GIT_AUTHOR_EMAIL="<New Email>";
                  git commit-tree "$@";
          else
                  git commit-tree "$@";
          fi' HEAD `
      

      【讨论】:

        猜你喜欢
        • 2014-12-25
        • 2011-04-25
        • 2011-11-27
        • 2011-11-30
        • 2016-01-26
        • 1970-01-01
        • 1970-01-01
        • 2018-11-10
        • 2012-03-20
        相关资源
        最近更新 更多