tags:

views:

68

answers:

2

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.

+5  A: 

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.

Alok
Thanks, Alok and everybody. I've tried this out, and it's definitely easier than what I was doing before.However, it still seems less convenient than the process of resolving conflicts. It seems like adding in this way should be at least as easy as merging.I still wonder if there is a way to "trick" Git into creating a conflict situation where the "<<<<" lines can be used instead of the diff format: +/-/ .
Chris J.
I agree. If there were a way to use any of the many existing merge tools to pick hunks to stage, would be a great improvement over the current commandline interface where you keep having to respond to the text prompts.
davr
A: 

In addition to git add --patch there's git add -i : which you could think of as one layer over git add --patch.

Tutorials on git add --patch and git add -i

RyanWilcox