开发者

git: patch only parts of older revision of a file

I commited a copy of "main.c" to my repository. I then deleted 5 whole functions to see what happens. I want 2 of those functions back. Can I do something like this:

$ git make-patch main.c --interactive > patch_to_add_back_functions.patch
#interactively choose the functions I want.
$ patch main.c patch_to_add_back_funcitons.patch

Is there a way to accomplish that? Either via a patch generation or via whatever would开发者_开发百科 be the most git way of thinking.


If you use git checkout -p you can selectively discard changes in your working tree - it's very useful for just the situation you describe, I've found. A couple of other notes might be of interest:

  • As a clarification, when I say "selectively discard changes", strictly speaking that should be "selectively change parts of the file back to their state in the index". However, if you haven't staged any changes since the last commit, the state in the index will be the same as in the last commit.

  • As with the other interactive -p options in git, you can press s to split the patch down further, or, if you need to discard changes at an even finer level, pressing e will let you edit the patch directly. (The latter can be rather confusing at first, however.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜