【问题标题】:Preserving only file additions from a series of Git commits?只保留来自一系列 Git 提交的文件添加?
【发布时间】:2014-01-16 03:05:17
【问题描述】:

A<-B<-C 成为 Git 提交的序列。

提交 BC 引入了 3 种类型的更改:

  1. 添加一些文件。
  2. 修改一些文件。
  3. 正在删除一些文件。

不幸的是,BC 中类型 2 和 3 的更改结果都不正确。因此我们需要推导出一个修改后的序列A<-B'<-C',其中B'C'只包含来自原始AB的文件添加,丢弃任何修改或删除。

如何做到这一点?

如果有帮助,我们绝对不需要 B'C' 作为单独的提交;我们可以只使用A<-D,其中D 包含在BC 中添加的每个文件。

【问题讨论】:

    标签: git version-control


    【解决方案1】:

    您可以尝试重置为 A,并添加所有新文件:

    # reset index, reset HEAD to A, preserve working tree.
    git reset A
    
    # Add only new files
    git add $(git ls-files -o --exclude-standard)
    
    # Make commit D (equals B and C new files)
    git commit -m "only new files from B and C"
    

    对于第二步(仅添加新文件),请参阅“Git add only all new files, not modified files”。


    OP Dun Pealcomments:

    C 删除了 B 引入的一些文件。因此 C 工作副本不包括 B 引入的所有文件。我想我可以一次迭代一个提交,并为每个提交执行此操作。

    是的,需要一种迭代方法:

    git reset --hard B
    git reset A
    git add $(git ls-files -o --exclude-standard)
    git commit -m "only new files from B"
    

    这会产生提交B'

    重复C,使用B'作为基础:

    git reset --hard C
    git reset B'
    git add $(git ls-files -o --exclude-standard)
    git commit -m "only new files from C"
    

    【讨论】:

    • 谢谢,但有一点复杂:C 删除了B 引入的一些文件。因此C 工作副本不包括B 引入的所有文件。我想我可以一次迭代一个提交,并为每个提交执行此操作。
    • @DunPeal 我同意。我已经编辑了答案,以了解所说的迭代方法是什么样的。
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2020-07-18
    • 2015-04-12
    相关资源
    最近更新 更多