Can I interactively pick hunks from another git commit?

后端 未结 4 939
梦谈多话
梦谈多话 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:28

    Building on the great answer from Cameron Skinner, I will expand this a bit to include the case when you have a branch with multiple commits, perhaps merge commits, perhaps a lot of "noise" and you want to cherry-pick only very specific hunks from it. (which was exactly what happened to me today)

    Follow these steps:

    git checkout source-branch
    git checkout -b cherry-pick-branch # Gives you a new branch based on source-branch
    git reset --mixed master
    

    This will leave you with a branch where all changes are in the filesystem, but nothing has been commited/staged. Then use git add -p to selectively add which parts you want to stage, and then commit & push as normal.

提交回复
热议问题