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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With