如何将更改后的文件添加到Git中较旧的(而非最后一次)提交中 [英] How to add a changed file to an older (not last) commit in Git

查看:106
本文介绍了如何将更改后的文件添加到Git中较旧的(而非最后一次)提交中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去一小时内我已经改变了几件事,并且一步一步地提交了他们,但我刚刚意识到我忘了在某些提交前添加更改过的文件。



日志看起来像这样:

  GIT TidyUpRequests u:1 d:0> git log 
commit fc6734b6351f6c36a587dba6dbd9d5efa30c09ce
作者:David Klein<>
日期:星期二4月27日09:43:55 2010 +0200

主程序现在一次测试两个Web服务

提交8a2c6014c2b035e37aebd310a6393a1ecb39f463
作者: David Klein<>
日期:星期二4月27日09:43:27 2010 +0200

ISBNDBQueryHandler现在也使用XPath.fs中的XPath函数

提交06a504e277fd98d97eed4dad22dfa5933d81451f
作者:David Klein<>
日期:星期二4月27日09:30:34 2010 +0200

AmazonQueryHandler现在使用XPath.fs中定义的XPath Helper函数

commit a0865e28be35a3011d0b6091819ec32922dd2dd8< - - 更改文件应该在这里
作者:David Klein<>
日期:星期二4月27日09:29:53 2010 +0200

推导出一些常见的XPath操作

有什么想法?

解决方案

使用 git rebase 。具体来说:


  1. 使用 git stash 来存储您要添加的更改。 / li>
  2. 使用 git rebase -i HEAD〜10 (或任何您需要查看的足够远的地方)。 b
  3. 通过更改单词 pick 在编辑时标记提交问题( a0865 ... )进行编辑将行开始放入 edit 中。不要删除其他行,因为它会删除提交。注意: ^

  4. 保存rebase文件,git将退回到shell并等待您修复该提交。 b
  5. 使用> git stash pop

  6. 添加您的文件 git add

  7. 使用 git commit --amend 修改提交。

  8. 做一个 git rebase --continue ,它会重写其余的提交对新的一个。


  9. $ b
  10. 如果您标记了多个提交以进行修改,则从第2步开始重复。 ^ (如果您使用的是shell,那么您必须按 Insert 键进行编辑,然后 Esc 并键入:wq 保存并应用。


    I have changed several things over the last hour and committed them step by step, but I just realized I've forgot to add a changed file some commits ago.

    The Log looks like this:

    GIT TidyUpRequests u:1 d:0> git log 
    commit fc6734b6351f6c36a587dba6dbd9d5efa30c09ce 
    Author: David Klein <> 
    Date:   Tue Apr 27 09:43:55 2010 +0200
    
        The Main program now tests both Webservices at once
    
    commit 8a2c6014c2b035e37aebd310a6393a1ecb39f463 
    Author: David Klein <>
    Date:   Tue Apr 27 09:43:27 2010 +0200
    
        ISBNDBQueryHandler now uses the XPath functions from XPath.fs too
    
    commit 06a504e277fd98d97eed4dad22dfa5933d81451f 
    Author: David Klein <> 
    Date:   Tue Apr 27 09:30:34 2010 +0200
    
        AmazonQueryHandler now uses the XPath Helper functions defined in XPath.fs
    
    commit a0865e28be35a3011d0b6091819ec32922dd2dd8 <--- changed file should go here
    Author: David Klein <> 
    Date:   Tue Apr 27 09:29:53 2010 +0200
    
        Factored out some common XPath Operations
    

    Any ideas?

    解决方案

    Use git rebase. Specifically:

    1. Use git stash to store the changes you want to add.
    2. Use git rebase -i HEAD~10 (or whatever you need to see far enough back).
    3. Mark the commit in question (a0865...) for edit by changing the word pick at the start of the line into edit. Don't delete the other lines as that would delete the commits. Note: ^
    4. Save the rebase file, and git will drop back to the shell and wait for you to fix that commit.
    5. Pop the stash by using git stash pop
    6. Add your file with git add.
    7. Amend the commit with git commit --amend.
    8. Do a git rebase --continue which will rewrite the rest of your commits against the new one.
    9. Repeat from step 2 onwards if you have marked more than one commit for edit.

    ^ (If you are using the shell then you will have to hit the Insert key to edit, then Esc and type in :wq to save and apply.

    这篇关于如何将更改后的文件添加到Git中较旧的(而非最后一次)提交中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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