Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I interactively pick hunks from another git commit?

Tags:

git

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 my working directory from (parts of) a commit. How can I do that ?

My use case is that I have several distinct features that are grouped together in a single commit and I'd like to test them one by one

Using cherry-pick -n is not really satisfactory, since it leaves me with the dirty job of removing all the unrequired code. I'd really just like to pick the selected changes I want to test.

like image 805
krosenvold Avatar asked May 03 '11 05:05

krosenvold


People also ask

How do I checkout to another commit?

Simply right-click on a commit from the central graph and select Checkout this commit from the context menu. Yes, it's that easy.

How do you cherry pick commit from another branch?

In the list of branches, click the branch that has the commit that you want to cherry-pick. Click History. Drag the commit that you want to cherry-pick to the Current Branch menu and drop the commit on the branch that you want to copy the commit to.

How can you stage only some changes in a file?

In the Commit window, select the file you want to partially commit, then select the text you want to commit in the right pane, then right-click on the selection and choose 'Stage selected lines' from the context menu.


2 Answers

Using cherry-pick -n is not really satisfactory, since it leaves me with the dirty job of removing all the unrequired code. I'd really just like to pick the selected changes I want to test.

The job may have been a dirty one before, but with the advent of git checkout --patch, you can now selectively discard changes, similar to git add -p for adding.

like image 102
Marc Mutz - mmutz Avatar answered Sep 23 '22 06:09

Marc Mutz - mmutz


You could use git reset --mixed HEAD^1 to revert the index, then pick the hunks you want with git add -i.

The reset will roll back the index to the previous commit (essentially un-committing whatever was the HEAD), but it won't touch the working tree. You can now stage the hunks you want, commit them and throw away the rest with a git reset --hard HEAD.

like image 37
Cameron Skinner Avatar answered Sep 19 '22 06:09

Cameron Skinner