Reputation: 6239
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.
Upvotes: 9
Views: 2618
Reputation: 13974
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
Upvotes: 1
Reputation: 96101
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.
Upvotes: 11