Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way in Git to "cherry pick" uncommitted changes to an individual file?

Tags:

git

If I have a bunch of uncommitted changes to a single file in my working directory in Git, what's the easiest way to selectively pick and choose among these changes?

My current way is to "git diff" the file, and then manually review the diff alongside the actual file. If I want to restore bits of old code, I can copy and paste from the diff to the file.

It would be a lot easier in some cases if one could instead view and edit the changes in a single file. For example, is there a Git command one could use to convert the working file with changes to a conflict-resolution-like format -- i.e. with "<<<<" symbols around the uncommitted changes? This way one could pick and choose changes without having to cut and paste from one file to another.

I'm not sure how to accomplish this since it's not a three-way (merge) situation. Rather, it's a two-way situation, with one set of changes to one file (so there are no conflicts). Thanks.

like image 460
cjerdonek Avatar asked Jan 31 '10 08:01

cjerdonek


People also ask

How do you do cherry pick without commit?

The --no-commit option will execute the cherry pick but instead of making a new commit it will move the contents of the target commit into the working directory of the current branch.

What is cherry pick as a single commit?

Cherry picking in Git means to choose a commit from one branch and apply it onto another. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.


1 Answers

You can select which hunks to add in a commit by git add --patch. This will ask you for each hunk in a file's changes if you want to add that changeset. If a particular hunk is too big for you, you can split it even further. There is another option to git-add, --interactive, which lets you work with multiple files.

For more, see git-add man page, or this post from a google search.

like image 52
Alok Singhal Avatar answered Sep 23 '22 05:09

Alok Singhal