Can I interactively pick hunks from another git commit?

后端 未结 4 1038
梦谈多话
梦谈多话 2021-02-01 16:50

I\'m looking for exactly the same behavior as

git add -i -p

But instead of composing a commit from my working directory, I\'d like to compose

4条回答
  •  生来不讨喜
    2021-02-01 17:23

    dirty job of removing all the unrequired code

    If this is not acceptable,

    git cherry-pick -n ...         # Starting from clean status
    git reset                      # unstage
    git add -p / -i […​]  # add hunks interactively
    git restore .                  # wipe the rest (at root dir)
    

    , for example if you want to merge into uncommited changes or don't want to spoil time stamps, then using git apply like this may be a cleaner option:

    git show  | tee _.patch
    edit _.patch                    # strip unwanted hunks
    git apply -3 _.patch
    

    (As long as git cherry-pick and git apply do not support -p directly)

    Note: Methods like git checkout / restore -p ... do not consistently revert hunks from a specific commit but pick parts from a certain file state - possibly loosing changes in other later commits.

提交回复
热议问题