代码之家  ›  专栏  ›  技术社区  ›  asmeurer

撤消git中部分未老化的更改

git
  •  140
  • asmeurer  · 技术社区  · 14 年前

    如何撤销git中未老化更改的一部分,但将其余更改保持为未老化?我得出的结论是:

    git commit --interactive
    # Choose the parts I want to delete
    # Commit the changes
    git stash
    git rebase -i master # (I am an ancestor of master)
    # Delete the line of the most recent commit
    git stash apply
    

    这是可行的,但如果有类似的 git commit --interactive 仅用于恢复更改。有更好的方法吗?

    7 回复  |  直到 14 年前
        1
  •  285
  •   Brian Campbell Dennis Williamson    14 年前

    git checkout -p ,它允许您从工作副本和索引之间的差异中选择要还原的单个大块。同样地 git add -p git reset -p 允许您从索引和头到背索引之间的差异中选择单个大块。

    $ git checkout -p file/to/partially/revert
    # or ...
    $ git checkout -p .
    

    如果您希望预先快照git存储库,以便在恢复这些更改之前保留这些更改,我希望执行以下操作:

    $ git stash; git stash apply
    

    [alias]
        checkpoint = !git stash; git stash apply
    

    如果您使用一个好的编辑器模式或插件,还原单个大块或线条会更容易,这可能会支持直接选择要还原的线条,如图所示 -p Magit ,这是一种对使用Git非常有帮助的Emacs模式。在Magit中,你可以跑步 magit-status ,查找要还原的更改的差异,选择要还原的行(如果要一次还原一个大块头而不是一次还原一行,则只需将光标放在要还原的大块头上),然后按 k

        2
  •  29
  •   Bence Kaulics    8 年前
    git diff > patchfile
    

    然后编辑修补文件并删除不希望撤消的零件,然后:

    patch -R < patchfile
    
        3
  •  5
  •   Drew Hoskins    14 年前

    你可以

    git checkout master -- path/to/file
    

    对于要重置的每个文件。

        4
  •  1
  •   Abizern    14 年前

    怎么样

    1. 通过索引中的更改退出受影响的文件
    2. 使用 git add -p 仅向索引添加所需的更改。
        5
  •  1
  •   Jonathan Leffler    14 年前

    当我运行“git status”时,它会显示:

    $ git status
    # On branch fr/fr.002
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #   modified:   makefile
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    $
    

    git checkout -- makefile
    
        6
  •  1
  •   Community Egal    7 年前

    Brian Campbell's answer

    $ git add -p
       ... select the hunks to keep
    $ git checkout -- .
    $ git reset HEAD .
    
        7
  •  0
  •   Andrew    14 年前

    你能行 git checkout 并为其命名要撤消的部分的名称。